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.
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.
- dnf check-update
- dnf install nginx
- rpm -qa | grep nginx
- rpm -ql nginx
- apt update
- apt install nginx
- dpkg -l | grep nginx
- dpkg -L nginx
- 1apt update
Refresh the indexes
- 2apt install
Status becomes ii
- 3apt upgrade
Newer version from the same sources
- 4apt remove
Status rc, config files kept
- 5apt purge
Config gone, package forgotten
- 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.
- 1apt
Interactive commands for people
- 2apt-get and apt-cache
Stable commands for scripts
- 3Sources and indexes
sources.list.d and /var/lib/apt/lists
- 4dpkg
Unpacks .deb files and runs package scripts
- 5dpkg 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.
- 1Download the key
Into /etc/apt/keyrings
- 2Check the fingerprint
gpg --show-keys against the vendor's site
- 3Write a .sources file
Signed-By points at that key only
- 4apt update
Indexes verified with that key
- 5apt 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.
- 1Update indexes and upgrade with apt~30m
- 2Install and remove packages~30m
- 3Query packages with dpkg -l and -L~30m
- 4Add 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
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 -yEveryday 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.
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-toolRecover 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.
sudo dpkg --configure -a
sudo apt -f install
dpkg -l | awk 'NR>5 && $1 != "ii"'
tail -20 /var/log/apt/history.log
sudo apt upgradeQuick 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.
Practice exercises
Try each one yourself first. Use the hint if you get stuck, then compare with the solution.
- Exercise 1easy
Install the tree package, list the files it installed, and find which package owns /usr/bin/tree.
- Exercise 2medium
Install nginx, stop it from being upgraded, confirm the hold, then remove the hold and fully purge nginx including its config files.
- 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.