Skip to main content
RunBook Academy

LinuxXI · Package Managementapt and dpkg

apt and dpkg — Debian-family package management

Foundation⏱ ~12 minbashaptdpkgapt-cache

What you'll learn

  • Update, upgrade, install, and remove packages with apt
  • Query installed packages and their contents
  • Read apt logs and understand upgrade outcomes
  • Configure additional repositories safely

Prerequisites

Verified against Ubuntu 24.04 LTS · Debian 12 (Bookworm) · RHEL 9.x · Rocky Linux 9.x · AlmaLinux 9.x · Linux kernel 6.1 LTS / 6.6 LTS · systemd 255+ · OpenSSH 8.7p1 (RHEL 9) / 9.6p1 (Ubuntu 24.04) · nftables 1.0.x · chrony 4.x · Pacemaker 2.1.x · Corosync 3.1.x · 2026-08-09

Not yet marked complete on this device.

apt is the high-level interface to the Debian package system. dpkg is the low-level tool. Most production work uses apt; dpkg is for queries and the rare case where apt fails.

The day-to-day commands

Configuration changeapt update + upgrade
$ sudo apt update; sudo apt upgrade
Hit:1 http://archive.ubuntu.com/ubuntu noble InRelease\n...\nReading package lists... Done\nBuilding dependency tree... Done\n...\n0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

Illustrative output

upgrade never removes; full-upgrade may

apt upgrade will not remove an installed package. When an upgrade cannot complete without a removal, apt skips it and reports it as kept back:

The following packages have been kept back:
  linux-generic linux-headers-generic linux-image-generic
0 upgraded, 0 newly installed, 0 to remove and 3 not upgraded.

apt full-upgrade does the same job but is permitted to remove packages to upgrade the system as a whole. It is the same operation as the older apt-get dist-upgrade.

CommandRemoves packages?The risk it carries
apt upgradeNeverSilently skips updates, including security fixes
apt full-upgradeYes, when neededMay remove a package you needed, unattended
Read-only / Safeapt search / show
$ apt search web-server | head; apt show nginx
Sorting... Done
nginx/noble 1.24.0-2ubuntu7 amd64
small, powerful, scalable web/proxy server
...

Illustrative output

Configuration changeinstall / remove / purge
$ sudo apt install nginx; sudo apt remove nginx; sudo apt purge nginx
...

Illustrative output

Querying installed packages

Read-only / Safedpkg queries
$ dpkg -l 'nginx*'; dpkg -L nginx; dpkg -S /etc/nginx/nginx.conf
...

Illustrative output

Read-only / Safeinstalled count / holds
$ apt list --installed 2>/dev/null | wc -l; apt-mark showhold
...

Illustrative output

Reading apt logs

Read-only / Safeapt history
$ tail -30 /var/log/apt/history.log
Start-Date: 2026-08-09 12:00:01
Commandline: apt upgrade -y
Upgrade: openssh-server:amd64 (1:9.6p1-3ubuntu2, 1:9.6p1-3ubuntu3)
End-Date: 2026-08-09 12:00:30
Start-Date: 2026-08-09 11:00:00
...

Illustrative output

Read-only / Safeapt term log
$ tail -50 /var/log/apt/term.log; ls -l /var/log/apt/
...

Illustrative output

Configuring repositories

Read-only / Safesources.list
$ cat /etc/apt/sources.list; ls /etc/apt/sources.list.d/
# Ubuntu sources have moved to /etc/apt/sources.list.d/ubuntu.sources
docker.sources  ubuntu.sources

Illustrative output

The deb822 .sources format

Debian 12 and Ubuntu 24.04 ship the archive as a deb822 stanza, not as one-line deb entries. One key field is new and matters: Signed-By names the only key permitted to sign this repository, instead of accepting any key in the global trust store.

Read-only / Safeubuntu.sources
$ cat /etc/apt/sources.list.d/ubuntu.sources
Types: deb
URIs: http://archive.ubuntu.com/ubuntu/
Suites: noble noble-updates noble-backports
Components: main restricted universe multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg

Types: deb
URIs: http://security.ubuntu.com/ubuntu/
Suites: noble-security
Components: main restricted universe multiverse
Signed-By: /usr/share/keyrings/ubuntu-archive-keyring.gpg

Illustrative output

The one-line format still works and older hosts still use it, so any inventory must read both:

grep -rhE '^(deb|deb-src) |^(Types|URIs|Suites|Components|Signed-By):' \
     /etc/apt/sources.list /etc/apt/sources.list.d/ 2>/dev/null
apt-cache policy   # what apt actually resolved

Adding a repository safely

Configuration changeadd-apt-repository
$ sudo add-apt-repository 'deb https://example.com/repo stable main'; sudo apt update
...

Illustrative output

Knowledge check

Knowledge check · 3 questions

  1. Q1. What does apt update do?

  2. Q2. apt remove leaves the package configuration files behind; only apt purge deletes them.

  3. Q3. Which of the following are correct apt practices? Select all that apply.

Passing score: 75%. Answers are checked in this browser.