apt and dpkg
Debian and Ubuntu package management: dpkg underneath, apt on top, upgrade versus full-upgrade, holds and apt-cache policy, finding files, and deb822 sources.
apt and dpkg
The player loads from youtube-nocookie.com when you press play. Nothing reaches YouTube before that.
Not started
What you'll learn
- dpkg installs, removes and queries individual .deb files and keeps the database in /var/lib/dpkg. apt sits on top: it fetches indexes, resolves dependencies and downloads what dpkg needs.
- apt update refreshes the package lists; it installs nothing. apt upgrade installs newer versions but never removes a package; apt full-upgrade may remove packages to complete the upgrade, which release upgrades need.
- Use apt interactively and apt-get or apt-cache in scripts. apt's own man page says its behaviour may change between versions.
- dpkg -L lists a package's files and dpkg -S finds which package owns a file. apt-cache policy shows which version would be installed and from which source; apt-mark hold freezes a package.
- Sources now live in /etc/apt/sources.list.d/*.sources in deb822 format, each stanza naming its own Signed-By key. The one-line sources.list format is deprecated, though not to be removed before 2029.
Two layers
On Debian and Ubuntu, package management is two tools stacked.
dpkg is the low layer. It knows how to unpack one .deb, run its install scripts, record every file it put down, and remove them again. It keeps its database in /var/lib/dpkg. It does not download anything and does not resolve dependencies: give it a .deb whose dependencies are missing and it refuses.
APT is the high layer. It reads the configured sources, downloads their signed indexes, works out which packages and versions satisfy a request, as the previous lesson described, downloads them, and hands them to dpkg in the right order. The apt command is its friendly front end; apt-get and apt-cache are the older, script-stable ones.
The everyday cycle
sudo apt update # refresh package lists from every source
apt list --upgradable # what would change
sudo apt upgrade # install newer versions, never remove anything
sudo apt full-upgrade # allow removals when an upgrade needs them
sudo apt install nginx
sudo apt remove nginx # keep its config files
sudo apt purge nginx # remove config files too
sudo apt autoremove # remove dependencies nothing needs any more
These are written from the apt(8) man page for APT 3.0.4, the version in Debian 13; I had no Debian machine to run them on.
The distinction between the two upgrades matters. The man page says upgrade installs available upgrades and “new packages will be installed if required to satisfy dependencies, but existing packages will never be removed”. If an upgrade would require removing something, that package is skipped, and apt reports it as kept back. full-upgrade “will remove currently installed packages if this is needed to upgrade the system as a whole”. Moving to a new Debian release needs full-upgrade. Read its plan before you answer yes.
autoremove deserves the same care. The man page warns you should “check that the list does not include applications you have grown to like even though they were once installed just as a dependency”.
apt in scripts
The apt command changes defaults for humans: progress bars, colours, a different output format. Its man page is blunt that it “may change behavior between versions” and that scripts should prefer apt-get and apt-cache, which “keep backward compatibility as much as possible”. In automation, write apt-get -y install ..., typically with DEBIAN_FRONTEND=noninteractive so no configuration dialog waits for a keyboard.
Asking dpkg questions
dpkg’s database answers “what is installed and where did this file come from”. dpkg(1) lists -L to “list files installed to your system from package-name” and -S to “search for a filename from installed packages”. dpkg -l lists installed packages.
The database is plain files, so I could answer the same questions on the downloaded Debian 13 image without running dpkg. Each package has a file list in /var/lib/dpkg/info/<package>.list:
$ grep -l '^/usr/bin/ls$' var/lib/dpkg/info/*.list
var/lib/dpkg/info/coreutils.list
$ wc -l < var/lib/dpkg/info/coreutils.list
463
$ awk '/^Package: coreutils$/,/^$/' var/lib/dpkg/status | grep -E '^(Status|Essential|Version)'
Essential: yes
Status: install ok installed
Version: 9.7-3
That is what dpkg -S /usr/bin/ls and dpkg -L coreutils would report, done by hand. Essential: yes marks packages the system cannot work without; apt asks for an explicit confirmation phrase before removing one.
Which version will I get? Policy and holds
When several sources offer a package, APT picks by priority, then by version. apt_preferences(5) gives the defaults: 100 for the installed version, 500 for versions in normal sources, 990 for a configured target release, 1 for archives marked NotAutomatic such as experimental. apt-cache policy nginx prints the installed version, the candidate apt would install, and every available version with its source and priority. It is the first command to run when “apt installed the wrong version”. Changing priorities with pin files is covered in the third-party repositories lesson.
To freeze one package, apt-mark hold marks it, per apt-mark(8), to “prevent the package from being automatically installed, upgraded or removed”; apt-mark unhold releases it and apt-mark showhold lists holds. Useful for a database you upgrade on your own schedule, and dangerous if forgotten, because a held package also stops receiving security fixes.
Sources in deb822 format
Repositories are configured in /etc/apt/sources.list.d/. The old one-line format looked like deb http://deb.debian.org/debian trixie main. Newer systems use the deb822 format in .sources files. This is the real /etc/apt/sources.list.d/debian.sources from the Debian 13 image, minus its snapshot comments:
Types: deb
URIs: http://deb.debian.org/debian
Suites: trixie trixie-updates
Components: main
Signed-By: /usr/share/keyrings/debian-archive-keyring.pgp
Types: deb
URIs: http://deb.debian.org/debian-security
Suites: trixie-security
Components: main
Signed-By: /usr/share/keyrings/debian-archive-keyring.pgp
Ubuntu 24.04’s image uses the same format in ubuntu.sources, with Components: main universe restricted multiverse. Each stanza names the key that must have signed its index, with Signed-By. sources.list(5) says a stanza can be switched off with Enabled: no, and states that the one-line format “is deprecated and may eventually be removed, but not before 2029”. New files should be .sources.
Two more sources to know about: trixie-updates carries stable updates that cannot wait for a point release, and debian-security carries security fixes. A server missing the security line gets no security updates at all.
About the video
The embed is Learn Linux TV’s “Linux Crash Course - The apt Command”, February 2021, from “Understanding APT” to the end of “Managing repositories” (about 13 minutes), skipping the sponsor segment. Jay LaCroix covers updating the index, installing, removing, searching, upgrading and managing repositories on a live system. It is from 2021, so it shows one-line sources.list entries rather than deb822, and it does not cover dpkg -L/-S, apt-cache policy or holds, which this lesson adds. I judged it from the chapters and description, and English from the original English audio track. Andrea Borman’s video in deeper walks through the deb822 switch.
What was run: reading the downloaded Debian and Ubuntu images’ dpkg database and sources files on a Mac with grep, wc and awk. Commands and behaviour are from Debian trixie’s apt(8), apt-cache(8), apt-mark(8), apt_preferences(5), sources.list(5) and dpkg(1), checked 2026-11-28.
Check yourself
You ran apt update and nothing changed on the system. Is that expected?
Yes. update only downloads fresh package lists from the configured sources. You need apt upgrade or full-upgrade to install newer versions.
apt upgrade reports some packages 'kept back'. What does that mean and what will install them?
Their new versions need a package to be removed or a conflict resolved, which upgrade never does. apt full-upgrade will, after you review what it plans to remove; a held package stays back either way.
How do you find which package installed /usr/bin/ls, and list everything that package installed?
dpkg -S /usr/bin/ls shows the owning package (coreutils), and dpkg -L coreutils lists its files.
Why should an automation script call apt-get rather than apt?
apt is designed for interactive use and its output and defaults may change between versions. apt-get and apt-cache keep a stable interface for scripts.
Go deeper
Notes
Notes are saved to your account.
Everything else on this page works without one — progress and bookmarks keep themselves in this browser and come with you when you sign in. Notes are the one thing worth not trusting to a browser you might clear.
Your notes could not be loaded.