Introduction / Why This Is Needed
APT (Advanced Package Tool) is the standard and most powerful software management tool for Debian-based distributions such as Ubuntu. It automatically resolves dependencies, ensures system integrity, and allows you to work with thousands of packages from official repositories.
In this guide, you will gain practical skills for working with APT via the command line. You will be able to confidently install, update, and remove software, as well as keep your system clean—an essential skill for any Ubuntu administrator or advanced user.
Requirements / Preparation
- Operating System: Ubuntu 20.04 LTS or newer, or any derivative distribution (Linux Mint, Pop!_OS, elementary OS).
- Access Permissions: Most operations (installation, update, removal) require superuser privileges (
sudo). - Internet Connection: Required to download package information and the packages themselves.
- Basic Knowledge: Comfort with the terminal (opening, navigation, simple commands).
Basic Package Operations
Step 1: Update the Package Cache (apt update)
Before any installation or update operation, always update the local package list cache. This forces APT to fetch the latest information about versions and new packages from the configured repositories.
sudo apt update
What happens: APT contacts the servers specified in /etc/apt/sources.list and the /etc/apt/sources.list.d/ directory, downloads Packages.gz files, and updates its local database. Without this step, the system will work with outdated data.
⚠️ Important:
apt updatedoes not update the packages installed on your system. It only updates the list of available versions for installation.
Step 2: Search and Install a Package (apt search / apt install)
Searching for a Package
If you don't know the exact package name, use keyword search in descriptions and names:
apt search <keyword>
Example: apt search video editor will show packages where the words "video" and "editor" appear in the description or name.
Installing a Package
To install, use the install command. APT automatically calculates and installs all necessary dependencies.
sudo apt install <package_name1> <package_name2>
Example: sudo apt install vlc git will install the VLC media player and the Git version control system.
💡 Tip: You can specify multiple packages in a single command. APT processes them all in one run.
Step 3: Updating and Removing Software
Updating Packages
- Update a specific package:
sudo apt install <package_name>
If a newer version of this package exists in the repositories (afterapt update), theinstallcommand will upgrade it. - Update all packages with updates:
sudo apt upgrade
This command upgrades all installed packages to the latest available versions, without removing any packages or installing new ones (except new dependencies). - Full system upgrade (including removal/installation):
sudo apt full-upgrade
Similar toupgrade, but more aggressive. It may remove packages if necessary to satisfy new dependencies. Use with caution on production servers.
Removing a Package
- Remove a package, keeping configuration files:
sudo apt remove <package_name>
Configurations will remain in the system (/etc/), allowing you to preserve settings if you reinstall later. - Purge a package (including configs):
sudo apt purge <package_name>
Or (more modern syntax):sudo apt remove --purge <package_name>
This completely removes all files belonging to the package, including configuration files.
Step 4: System Cleanup and Freeing Up Space
Over time, the APT cache (/var/cache/apt/archives/) accumulates .deb files from already installed packages. "Orphaned" dependencies may also remain after removals.
Clean the Download Cache
Removes all .deb files from the cache, except the very latest download of each package.
sudo apt clean
This is the most radical and space-freeing method. The next time you install a package, its .deb file will need to be downloaded again.
Clean Obsolete Cache Files
Removes only those .deb files from the cache for which there is no corresponding package version in the repositories anymore (i.e., obsolete versions).
sudo apt autoclean
A safer option than clean.
Remove Unnecessary Dependencies
Automatically finds and suggests removing packages that were installed automatically as dependencies but are no longer needed by any remaining installed packages.
sudo apt autoremove
Recommended practice: Periodically (once a month) run sudo apt update && sudo apt upgrade && sudo apt autoremove.
Verifying the Result
- Check package status: Find out if a package is installed and its version.
apt list --installed | grep <package_name>
Or for a specific package:apt show <package_name> - Check for available updates: After
apt update, see which packages can be upgraded.apt list --upgradable - Check operation history: APT maintains a log at
/var/log/apt/history.log. You can view it to see which packages were installed, upgraded, or removed.less /var/log/apt/history.log
Possible Issues
Error: "E: Could not get lock /var/lib/dpkg/lock-frontend"
Symptom: The apt command is interrupted with a lock error.
Cause: Another package manager process (e.g., apt, apt-get, dpkg, unattended-upgrades) is already running.
Solution:
- Wait 1-2 minutes if, for example, a background update is in progress.
- Find and terminate the process (carefully!):
sudo killall apt apt-get dpkg - If the problem persists, check if
unattended-upgradesis running. You can temporarily stop the service:sudo systemctl stop unattended-upgrades.
Error: "E: Unable to locate package "
Symptom: APT cannot find a package with the specified name. Cause:
- You did not run
sudo apt updateafter adding a new repository. - The package is in a repository that is not enabled by default (e.g.,
universe,multiverse). - The package has a different name (search via
apt search). Solution: - Run
sudo apt update. - Enable missing repositories via
sudo add-apt-repository universe(ormultiverse,restricted). - Clarify the exact package name.
Error: "E: dpkg was interrupted, you must manually run 'sudo dpkg --configure -a'"
Symptom: The installation/removal process was interrupted (e.g., due to power loss or forced terminal closure). Solution: Run the recovery command; it will complete the interrupted package configuration.
sudo dpkg --configure -a
After it completes successfully, try your apt command again.