Introduction / Why This Is Useful
APT (Advanced Package Tool) is the standard package manager for Debian-based distributions (including Ubuntu, Linux Mint, and others). It simplifies the installation, update, and removal of software by automatically resolving dependencies between packages. In this guide, you'll master the basic APT commands that cover 90% of everyday tasks. After completing it, you'll be able to confidently manage packages via the terminal without relying on graphical utilities.
Prerequisites / Preparation
Before you begin, ensure that:
- You have access to a terminal (Ctrl+Alt+T or via the applications menu).
- You have superuser privileges (you can use
sudo). Most APT commands require elevated privileges. - Your system is connected to the internet for downloading packages and updating repository lists.
- APT is already installed (by default in Debian/Ubuntu). To check the version:
apt --version
Basic APT Commands
Update package lists: apt update
Before installing or updating packages, always synchronize your local cache with the repositories. This ensures you get the latest versions and information about new packages.
sudo apt update
What happens:
APT downloads package lists from the sources specified in /etc/apt/sources.list and /etc/apt/sources.list.d/. Without this step, your system won't know about new versions or packages.
Example output:
Hit:1 http://archive.ubuntu.com/ubuntu focal InRelease
Hit:2 http://archive.ubuntu.com/ubuntu focal-updates InRelease
...
Reading package lists... Done
💡 Tip: Run
apt updatebefore any install/update operation. To automate this, add it to cron once a day.
Install packages: apt install
Install one or multiple packages:
sudo apt install <package_name1> <package_name2>
Example: installing the nano text editor and curl utility:
sudo apt install nano curl
APT automatically:
- Downloads the packages and all necessary dependencies.
- Asks for confirmation (press
YorEnter). - Installs the packages on your system.
Install a specific version (if multiple are available):
sudo apt install <package_name>=<version>
Upgrade packages: apt upgrade and apt full-upgrade
apt upgrade— upgrades all installed packages to the latest available versions without removing old packages or installing new dependencies. It's safe but may leave obsolete dependencies.apt full-upgrade(orapt-get dist-upgrade) — more aggressive: removes obsolete packages and installs new dependencies if necessary to complete an upgrade. Use this ifupgradecannot complete the update.
Example:
sudo apt upgrade
The system will show a list of packages to be upgraded and ask for confirmation.
⚠️ Important: Always run
apt updatefirst, otherwiseupgradewon't find new versions.
Remove packages: apt remove and apt purge
apt remove <package>— removes the package's binary files but preserves configuration files (in/etc/and elsewhere). Useful if you plan to reinstall.apt purge <package>— complete removal, including configuration files. Equivalent toapt removeplus cleanup of settings.
Example removing nano:
sudo apt remove nano
For complete removal:
sudo apt purge nano
Remove packages that were installed as dependencies but are no longer needed:
sudo apt autoremove
This command removes "orphaned" packages (those that have no reverse dependencies).
Search for packages: apt search
Searches the local cache (after apt update) for packages by keyword:
apt search <keyword>
Example: find packages related to Python:
apt search python3
The output includes the package name and a short description.
Search for an exact package name (if you know part of it):
apt list | grep <part_of_name>
View package information: apt show
Shows detailed information about an installed or available package: version, size, dependencies, description, homepage.
apt show <package_name>
Example:
apt show curl
Clean the cache: apt clean and apt autoclean
APT stores downloaded .deb files in a cache (typically /var/cache/apt/archives/). Over time, this can occupy gigabytes of space.
sudo apt clean— removes all cache files.sudo apt autoclean— removes only obsolete files (those for which there is no longer a corresponding version in the repositories).
Recommendation: periodically run apt autoclean to save space.
Additional useful commands
- List installed packages:
apt list --installed
You can filter withgrep:apt list --installed | grep python. - Check if a specific package is upgradable:
apt list --upgradable | grep <package> - View repository sources:
cat /etc/apt/sources.list ls /etc/apt/sources.list.d/
Editing:sudo nano /etc/apt/sources.list(be careful!).
Verifying the Result
After running commands, ensure the operation was successful:
- Exit code: In the terminal,
echo $?should show0(success). Any non-zero value indicates an error. - Error messages: Read the APT output. Common errors:
E: Unable to locate package <package>— package not found. Check the name or add a repository.E: Could not open lock file— process conflict (see "Possible Issues" section).
- Check package installation:
ordpkg -l | grep <package_name>apt list --installed | grep <package_name> - Run the package: If it's an executable, try running it (e.g.,
nano --version).
Possible Issues
Permission error
Symptom: E: Could not open lock file /var/lib/dpkg/lock-frontend or E: Unable to acquire the dpkg frontend lock.
Cause: Another process (e.g., Software Center, another terminal with apt) is already using the package manager.
Solution:
- Wait 1-2 minutes for the other process to finish.
- Find and kill the process:
sudo killall apt apt-get - As a last resort, reboot the system.
Package not found
Symptom: E: Unable to locate package <name>.
Cause:
- Incorrect package name.
- Repository containing the package is not added or enabled.
- You didn't run
apt updateafter adding the repository. Solution: - Check the name via
apt search <part_of_name>. - Check repositories:
cat /etc/apt/sources.list. - Add the repository (e.g., for Universe in Ubuntu:
sudo add-apt-repository universe) and runsudo apt update.
Not enough disk space
Symptom: E: You don't have enough free space in /var/cache/apt/archives/.
Solution:
- Clean the cache:
sudo apt clean. - Remove old kernels or unnecessary files.
- Expand the partition (if possible).
Network errors during update
Symptom: Failed to fetch http://... or Could not resolve.
Solution:
- Check your internet connection.
- Check repository availability (might be a temporary server issue).
- If using a proxy, configure APT to work with it (file
/etc/apt/apt.conf.d/proxy).
Package stuck in "not fully installed" state
Symptom: When trying to install/remove a package, APT reports a "broken" state. Solution:
sudo apt --fix-broken install
This command attempts to fix dependencies and complete interrupted operations.
Different APT versions in scripts
Note: If writing scripts, use apt-get and apt-cache instead of apt, as their output is more stable and intended for machine processing. For interactive use, apt is more convenient thanks to colored output and a progress bar.