Where to start First steps track · 15 stages
Linux Labs

apt / dpkg — package management

4 min Ubuntu 24.04 · Debian

Installing, upgrading, and removing packages on Debian/Ubuntu; apt and dpkg.

Cheat sheet

10 commands Printable version
  • sudo apt update && sudo apt upgrade refresh the index, then install upgrades
  • sudo apt install nginx install a package
  • sudo apt purge nginx remove a package together with its config
  • sudo apt autoremove --purge remove orphaned dependencies
  • apt-cache policy nginx package versions and which repo they come from
  • dpkg -L nginx which files a package brought in
  • dpkg -S /usr/sbin/nginx which package owns a file
  • sudo apt --fix-broken install install missing dependencies after a failure
  • sudo dpkg --configure -a finish configuring a broken dpkg run
  • sudo apt-mark hold nginx pin a package's version

Overview

On Debian/Ubuntu, packages are managed by:

  • apt — the high-level manager (repositories, dependencies, upgrades);
  • dpkg — the low-level one (a single .deb file, no dependency resolution).

Repository sources live in /etc/apt/sources.list and /etc/apt/sources.list.d/ (the newer format is *.sources, deb822).

Options

14

apt is built around subcommands (apt install, apt remove, …) rather than flags of a single command; below are the main subcommands and flags. The full list is in man apt, man dpkg.

Option What it does Example
update Refresh the package index from repositories; does not install anything by itself. sudo apt update
upgrade Install available upgrades without removing packages. sudo apt upgrade
install Install a package (and its dependencies). sudo apt install nginx
remove Remove a package, leaving its configuration files in /etc. sudo apt remove nginx
-l List installed packages matching a name pattern. dpkg -l | grep nginx
-L Show which files belong to an installed package. dpkg -L nginx
purge Remove a package together with its configuration files. dangerous sudo apt purge nginx
autoremove Remove packages that were pulled in as dependencies and are no longer needed. dangerous sudo apt autoremove --purge
-y --yes Do not ask for confirmation; for scripts and automation. dangerous sudo apt install -y nginx
--fix-broken Install missing dependencies after a broken install. sudo apt --fix-broken install
-S Show which package owns a given file. dpkg -S /usr/sbin/nginx
-i Install a local .deb file directly, without resolving dependencies. sudo dpkg -i package.deb
full-upgrade Upgrade while allowing conflicting packages to be removed — riskier than upgrade. dangerous sudo apt full-upgrade
--configure -a Finish configuring all packages left unpacked after a failure. sudo dpkg --configure -a

Nothing found. Try a shorter query.

Recipes

2

apt

bash
sudo apt update                   # refresh the package INDEX (does not install upgrades)
sudo apt upgrade                  # install available upgrades
sudo apt full-upgrade             # + remove conflicting packages (riskier)
sudo apt install nginx            # install
sudo apt install nginx=1.24\*     # a specific version
sudo apt install --reinstall nginx
sudo apt remove nginx             # remove (config files stay)
sudo apt purge nginx              # remove together with config files
sudo apt autoremove --purge       # remove orphaned dependencies
apt search keyword ; apt show nginx ; apt list --installed
apt-cache policy nginx            # available versions and which repo they come from
sudo apt-mark hold nginx          # pin a version (unhold to release it)
sudo apt download nginx           # download the .deb without installing

dpkg and repositories

bash
dpkg -l | grep nginx              # is it installed
dpkg -L nginx                     # which files the package brought in
dpkg -S /usr/sbin/nginx           # which package owns a file
sudo dpkg -i package.deb          # install a local .deb (then apt -f install)
dpkg --get-selections             # a list to carry over to another machine

# add a third-party repository (modern way, with a key):
curl -fsSL https://repo.example.com/key.gpg | sudo gpg --dearmor -o /etc/apt/keyrings/example.gpg
echo "deb [signed-by=/etc/apt/keyrings/example.gpg] https://repo.example.com stable main" \
  | sudo tee /etc/apt/sources.list.d/example.list
sudo apt update

Troubleshooting

5

"Could not get lock /var/lib/dpkg/lock"

apt/unattended-upgrades is already running. Wait, or find the process (ps aux | grep -E 'apt|dpkg', see processes). Do not kill dpkg mid-way.

dpkg was interrupted, "broken packages"

bash
sudo dpkg --configure -a          # finish configuring what was interrupted
sudo apt --fix-broken install     # install the missing dependencies

apt update: NO_PUBKEY / 404 / repository changed

A broken or stale source in sources.list.d/. Fix the URL or add the key (signed-by=, not the deprecated apt-key). If the Suite changed, use --allow-releaseinfo-change.

A specific version / rollback

apt-cache policy PKG shows what's available; apt install pkg=version installs it; apt-mark hold pins it. Without internet (as on a lab VM), use a proxy/mirror (apt-cacher-ng).

Free up space used by the cache

sudo apt clean (the whole cache) / autoclean (only stale files); autoremove --purge (see disk-usage).

Terms

7
package
A unit of files and metadata tracked by a package manager. Also appears in: unattended-upgrades: security updates with no one at the keyboard, snap, Capabilities: root split into parts, AppArmor and 34 more
package repository
A source of packages and their indexes, accessible over a network or locally. Also appears in: GPG
package index
Metadata about known packages and versions; a local copy can become outdated.
package dependency
A package requirement on another package, sometimes with a version constraint.
installation candidate
The version selected by APT using current information and policy; not necessarily already installed.
package architecture
The target package architecture; all means architecture-independent content, not support for every OS.
conffile
A configuration file explicitly registered by a package for dpkg handling of local modifications.

See also

  • systemctl — Managing systemd services — start/stop/restart/status/enable/reload.
Loading…