Skip to content
Visual explainer 2h to learn 4 steps

apt & dpkg on Ubuntu

Manage packages and sources on Debian-family systems.

By Invicly Tech, , in Linux Administration (RHCSA-style). About 9 minutes to read.

Topic 12 of 42

In short

Debian and Ubuntu use dpkg for individual .deb packages and apt for repositories and dependencies. apt update refreshes package indexes and apt upgrade installs newer versions. Third-party sources go in /etc/apt/sources.list.d with their own signing key.

Explained simply

Ubuntu and Debian use .deb packages. Like RPMs, each one holds a program's files plus a list of what it needs. The idea is the same as on RHEL; only the tool names and some file locations differ.

dpkg is the low-level tool. It installs one .deb file, removes packages and answers questions about what is installed. Like rpm, it does not download anything or fetch dependencies.

apt is the tool you use every day. It reads a list of sources (repositories), keeps a local copy of their package lists, and handles downloads and dependencies. You run apt update to refresh those lists, then apt install, apt upgrade or apt remove.

The most common beginner mistake is to forget apt update. apt then works with an old list and may try to download versions that no longer exist on the server. Updating the list first, then installing, avoids most strange errors.

Press Play on a diagram to walk through it one step at a time.

CompareSame task, two families
RHEL (dnf / rpm)
  • dnf check-update
  • dnf install nginx
  • rpm -qa | grep nginx
  • rpm -ql nginx
Ubuntu (apt / dpkg)
  • apt update
  • apt install nginx
  • dpkg -l | grep nginx
  • dpkg -L nginx
CycleLife of a package on Ubuntu
  1. 1
    apt update

    Refresh the indexes

  2. 2
    apt install

    Status becomes ii

  3. 3
    apt upgrade

    Newer version from the same sources

  4. 4
    apt remove

    Status rc, config files kept

  5. 5
    apt purge

    Config gone, package forgotten

  6. Back to step 1

In depth

apt, apt-get and dpkg: who does what

apt is the friendly front end for people: coloured output, progress bars and short commands. Its output format may change between releases, so scripts should use apt-get and apt-cache, which are stable. Under both sits dpkg, which actually unpacks files, runs package scripts and records the state.

apt update downloads package indexes into /var/lib/apt/lists. apt upgrade installs newer versions but never removes a package. apt full-upgrade (the same as apt-get dist-upgrade) may also remove packages when that is needed to finish the upgrade. apt install pkg=1.2.3-1 installs a specific version, and apt-mark hold pkg stops a package from being upgraded until you run apt-mark unhold.

LayersThe Debian package stack
  1. 1
    apt

    Interactive commands for people

  2. 2
    apt-get and apt-cache

    Stable commands for scripts

  3. 3
    Sources and indexes

    sources.list.d and /var/lib/apt/lists

  4. 4
    dpkg

    Unpacks .deb files and runs package scripts

  5. 5
    dpkg database

    /var/lib/dpkg/status records every package

Sources, components and pockets

Ubuntu 24.04 stores its own sources in /etc/apt/sources.list.d/ubuntu.sources using the newer deb822 format, with lines like Types, URIs, Suites, Components and Signed-By. Older releases use the one-line format in /etc/apt/sources.list, such as deb http://archive.ubuntu.com/ubuntu jammy main. apt understands both.

Components split the archive by support level: main (supported by Canonical), restricted (proprietary drivers), universe (community maintained) and multiverse (licence limits). Suites, also called pockets, separate the release from its updates: noble, noble-updates, noble-security and noble-backports for 24.04.

apt-cache policy nginx shows which versions each source offers and which one apt will choose, which is the first command to run when the wrong version gets installed.

  • Security fixes arrive through the -security pocket
  • add-apt-repository universe enables a component
  • apt-cache policy shows priorities and candidate versions

Adding a third-party source safely

The old apt-key command added keys to one global keyring, which meant a key for one vendor was trusted for every repository. It is deprecated. The safe pattern today is: download the vendor's key into /etc/apt/keyrings, check its fingerprint, and reference it with Signed-By in that vendor's source file only. Then a key can only vouch for packages from its own repository.

Keep third-party sources few and well known. A repository can ship any package, including a newer version of something important like openssl, and apt will happily upgrade to it if its version is higher. Read the vendor's instructions carefully, prefer https URLs, and remove sources you no longer use.

PPAs (Personal Package Archives on Launchpad) are third-party sources too, even though add-apt-repository ppa:name/ppa makes them look built in.

FlowTrusting one vendor repository
  1. 1
    Download the key

    Into /etc/apt/keyrings

  2. 2
    Check the fingerprint

    gpg --show-keys against the vendor's site

  3. 3
    Write a .sources file

    Signed-By points at that key only

  4. 4
    apt update

    Indexes verified with that key

  5. 5
    apt install

    Only this repo trusts this key

dpkg states, logs and automatic updates

dpkg -l shows two letters before each package. ii means installed and fine. rc means removed, but config files are still there; apt purge clears them. A third column or letters other than i point to a half-installed package. If an install was interrupted, sudo dpkg --configure -a usually finishes it, followed by sudo apt -f install to repair dependencies.

There is no history undo as in dnf, but there are logs: /var/log/apt/history.log records each apt command and what changed, and /var/log/dpkg.log records every package step.

Ubuntu Server ships unattended-upgrades, which installs security updates daily. Its settings are in /etc/apt/apt.conf.d/50unattended-upgrades and 20auto-upgrades, and it logs to /var/log/unattended-upgrades/. When an update needs a reboot, the file /var/run/reboot-required appears.

  • dpkg -s nginx shows status, version and dependencies
  • dpkg -c file.deb lists the files inside a package file
  • apt autoremove removes dependencies nothing needs any more

Learn it in 4 short steps

About 30m each. Track them with a timer in your plan.

  1. 1
    Update indexes and upgrade with apt~30m
  2. 2
    Install and remove packages~30m
  3. 3
    Query packages with dpkg -l and -L~30m
  4. 4
    Add a third-party source safely~30m

Key points

  • Always run apt update before installing so indexes are current.
  • apt remove keeps config files; apt purge deletes them too.
  • unattended-upgrades applies security updates automatically.
  • Store each third-party key in /etc/apt/keyrings and reference it with signed-by.

Key terms

Tap a card to flip it. 0/5 known

Examples

bash
sudo apt update && sudo apt upgrade -y
sudo apt install -y nginx
dpkg -L nginx | head
dpkg -S /usr/sbin/nginx        # which package owns this file
apt list --upgradable
sudo apt purge -y nginx && sudo apt autoremove -y

Everyday apt and dpkg commands on Ubuntu Server.

Add a vendor repository the modern way

You need packages from a vendor at repo.example.com. Step 1: create the keyrings directory and download the vendor's key into it. Step 2: show the key's fingerprint and compare it with the one on the vendor's website. Step 3: write a deb822 source file whose Signed-By names that key. Step 4: update and check that apt now sees the vendor's version before installing.

bash
sudo install -m 0755 -d /etc/apt/keyrings
curl -fsSL https://repo.example.com/key.gpg | sudo gpg --dearmor -o /etc/apt/keyrings/example.gpg
gpg --show-keys /etc/apt/keyrings/example.gpg
sudo tee /etc/apt/sources.list.d/example.sources > /dev/null <<'EOF'
Types: deb
URIs: https://repo.example.com/apt
Suites: stable
Components: main
Signed-By: /etc/apt/keyrings/example.gpg
EOF
sudo apt update
apt-cache policy example-tool

Recover from an interrupted upgrade

An SSH session dropped in the middle of apt upgrade. Now every apt command complains that dpkg was interrupted. Step 1: let dpkg finish configuring whatever it had unpacked. Step 2: let apt fix broken dependencies. Step 3: check for packages that are not in the ii state. Step 4: read the end of the apt history log to see what the upgrade was doing. Running long upgrades inside tmux avoids this next time.

bash
sudo dpkg --configure -a
sudo apt -f install
dpkg -l | awk 'NR>5 && $1 != "ii"'
tail -20 /var/log/apt/history.log
sudo apt upgrade

Quick reference

  • apt update refreshes indexes; apt upgrade installs newer versions; neither removes packages
  • apt full-upgrade may remove packages to complete an upgrade
  • dpkg -S /path names the package that owns a file; dpkg -L PACKAGE lists its files
  • apt install ./file.deb installs a local file and its dependencies
  • apt-mark hold PACKAGE stops upgrades of that package
  • apt-cache policy PACKAGE shows available versions and which one apt will pick
  • dpkg -l shows ii for installed and rc for removed with config left
  • /var/run/reboot-required exists when an update needs a reboot

Quick quiz

Pick an answer to see if you're right and why.

Question 1

1dpkg -l shows rc next to a package. What does that mean?

Question 2

2Why is Signed-By with a key in /etc/apt/keyrings safer than the old apt-key add?

Question 3

3Which command should a shell script use instead of apt for installing packages?

Question 4

4You run apt install nginx right after building a new VM and get 404 Not Found errors. What is the most likely fix?

0/4 answered

Practice exercises

Try each one yourself first. Use the hint if you get stuck, then compare with the solution.

  1. Exercise 1easy

    Install the tree package, list the files it installed, and find which package owns /usr/bin/tree.

  2. Exercise 2medium

    Install nginx, stop it from being upgraded, confirm the hold, then remove the hold and fully purge nginx including its config files.

  3. Exercise 3hard

    Using only the apt logs, list every package that the most recent apt command installed or upgraded, one per line.

Check yourself

Answer in your head, reveal, then be honest with yourself.

Feel confident about this topic?

Frequently asked questions

Should I use apt or apt-get?
Use apt when typing commands yourself. Use apt-get and apt-cache in scripts, because their output format is kept stable.
Does apt upgrade install a new Ubuntu release?
No. It only updates packages within your current release. Moving to a new release is done with do-release-upgrade.
What is a PPA and is it safe?
A PPA is a third-party repository hosted on Launchpad. It is as safe as the person who maintains it, so only add PPAs you have a good reason to trust.
How do I see which updates are waiting?
Run sudo apt update and then apt list --upgradable.

Go deeper

ShareXLinkedInWhatsApp