Package management with apt
apt is how you install software on Debian-based systems. It handles downloading, dependency resolution, installation, and removal. Understanding it well means you spend less time fighting package conflicts.
apt vs apt-get
apt is the user-friendly command. apt-get is the older, lower-level command. Use apt for interactive use, apt-get in scripts (its output format is more stable).
Common operations:
sudo apt update # refresh package lists
sudo apt upgrade # upgrade all packages
sudo apt install nginx # install a package
sudo apt remove nginx # remove (keep config)
sudo apt purge nginx # remove including config
sudo apt autoremove # remove unused dependencies
Always run apt update before apt install. If your package lists are stale, apt will fail to find packages that exist.
Searching for packages
apt search nginx # search by name/description
apt show nginx # show package details
apt list --installed # list installed packages
apt list --upgradable # list packages with updates
Find which package provides a file:
dpkg -S /usr/bin/curl
apt-file search /usr/bin/curl # requires apt-file package
Repositories
Package sources are in /etc/apt/sources.list and /etc/apt/sources.list.d/.
A typical entry:
deb http://archive.ubuntu.com/ubuntu focal main restricted universe multiverse
deb http://archive.ubuntu.com/ubuntu focal-updates main restricted universe multiverse
deb http://security.ubuntu.com/ubuntu focal-security main restricted universe multiverse
Components:
main— officially supported, free softwarerestricted— officially supported, non-free driversuniverse— community-maintained, free softwaremultiverse— non-free software
Adding a PPA
Personal Package Archives provide newer or third-party packages:
sudo add-apt-repository ppa:ondrej/php
sudo apt update
sudo apt install php8.1
Remove a PPA:
sudo add-apt-repository --remove ppa:ondrej/php
PPAs are third-party. Trust them accordingly.
Adding a GPG key and repository manually
For software that provides their own repo:
curl -fsSL https://example.com/gpg.key | sudo gpg --dearmor -o /etc/apt/trusted.gpg.d/example.gpg
echo "deb https://repo.example.com/apt stable main" | sudo tee /etc/apt/sources.list.d/example.list
sudo apt update
Package holds
Prevent a package from being upgraded:
sudo apt-mark hold nginx
Release the hold:
sudo apt-mark unhold nginx
Check held packages:
apt-mark showhold
Useful when you’ve pinned a specific version and don’t want automatic updates to change it.
Version pinning
Install a specific version:
apt show -a nginx # show available versions
sudo apt install nginx=1.18.0-0ubuntu1
For more control, create preferences in /etc/apt/preferences.d/:
Package: nginx
Pin: version 1.18*
Pin-Priority: 1001
Priority above 1000 forces downgrades. Below 1000 prevents upgrades.
dpkg: low-level operations
dpkg handles individual .deb files without dependency resolution:
sudo dpkg -i package.deb # install (may fail on dependencies)
sudo apt install -f # fix broken dependencies
sudo dpkg -r package # remove
sudo dpkg -l # list installed packages
dpkg -i followed by apt install -f is a common pattern for installing local .deb files with dependencies.
Cleaning up
Remove cached package files:
sudo apt clean # remove all cached packages
sudo apt autoclean # remove only outdated cached packages
Remove packages installed as dependencies that are no longer needed:
sudo apt autoremove
Check disk usage of the package cache:
du -sh /var/cache/apt/archives/
Common mistakes
Not running apt update first. Your local package index is stale. apt can’t find packages that the repositories have.
Using apt-get when apt works. For interactive use, apt has better output (progress bars, color). Use apt-get in scripts where output stability matters.
Mixing repositories carelessly. Adding repos for the wrong Ubuntu version causes dependency conflicts. Match the repo to your release.
Forgetting autoremove. Over time, unused dependencies pile up. Run apt autoremove periodically.
Remarks
Package management on Ubuntu is straightforward once you understand the workflow: update lists, search, install, clean up. The apt command handles 95% of what you need. When things get complicated — version conflicts, held packages, third-party repos — dpkg and apt pinning give you finer control. Start with apt and reach for dpkg when you need to go deeper.
Related Posts
Vim survival guide for Linux admins
The minimum vim knowledge you need to survive on a Linux server. Editing config files without wanting to throw your keyboard.
Linux memory management: free, vmstat, and /proc
Understanding how Linux uses memory. Reading free correctly, using vmstat, and what /proc/meminfo actually tells you.
tmux: terminal multiplexing made simple
Using tmux to manage terminal sessions. Panes, windows, and why your SSH sessions should survive disconnects.