Linux Package Managers: apt, dnf and Friends
Your team needs the nginx web server on a new Ubuntu machine. By hand, you would download it, hunt down the libraries it needs and repeat that for every security fix. Instead, you type one command. This lesson explains how Linux package managers work: apt and dnf, where their software comes from, and how to use them safely on a server.
What a package manager does
A package is one archive file holding a program's files plus a description: its name, version, summary and the other packages it needs. Debian and Ubuntu packages end in .deb; Red Hat family packages end in .rpm.
Those other packages are dependencies. Many are libraries, code that several programs share. For example, curl, the command-line download tool, uses the library libcurl4, so installing curl installs libcurl4 too.
A package manager installs, upgrades and removes packages for you. Compared with doing it by hand, it gives you:
- Checked sources: every download is checked against the index, your distribution's signed list of available packages.
- Dependencies handled: it works out everything a program needs and installs it in order.
- One command to install, upgrade or remove, because it records which files each package put where.
A hash is a short fingerprint computed from a file's contents: change one byte and the hash changes (hashing explained). In the flowchart, notice step 5: apt compares each download's hash with the signed index before installing anything.
Step 7 differs by family: on Red Hat family systems, you usually start the service yourself with systemd (lesson 12).
Two families: apt with .deb, dnf with .rpm
A distribution is the Linux kernel plus tools (lesson 2). Distributions form families that share a package format, and you will meet two most often.
Debian and Ubuntu use .deb packages. apt (Advanced Package Tool) downloads them and works out dependencies, then hands them to dpkg, the low-level tool that unpacks packages and records what is installed.
Red Hat Enterprise Linux (RHEL), Fedora and Amazon Linux use .rpm packages. There, dnf does apt's job, on top of the low-level rpm tool. Its predecessor, yum, still runs Amazon Linux 2; on RHEL 8 and later, typing yum runs dnf.
The families do not mix: dnf cannot install a .deb. To check a server's family, look at the ID line in /etc/os-release, such as ID=debian or ID=fedora.
Everyday commands, side by side
Both tools use nearly the same verbs. Commands that change what is installed write to system folders, so they need root: on a server, put sudo in front (lesson 7). Searching and showing details only read, so they do not.
In the table, notice the first row: apt knows only what its local copy of the index tells it, so you refresh that copy before anything else.
apt is made for people; scripts should use apt-get and apt-cache, which stay stable between versions. apt remove keeps a package's configuration files, and apt purge deletes them too.
Here is the index refresh on a Debian 12 server. apt names each file it fetches: Get means downloaded, Hit means unchanged since last time.
sudo apt update
You should see something like this (trimmed; sizes are illustrative):
Get:1 http://deb.debian.org/debian bookworm InRelease [151 kB]
Get:4 http://deb.debian.org/debian bookworm/main amd64 Packages [8,793 kB]
Fetched 9,048 kB in 2s (4,524 kB/s)
Reading package lists... Done
All packages are up to date.
The last line may instead say how many packages can be upgraded. The usual mistake is leaving out sudo: apt stops with Could not open lock file and Permission denied. If another process holds the lock (often automatic updates), wait for it, and never delete the lock file.
Repositories, the index and signing keys
A repository is a collection of packages on a web server, plus index files describing them, run by your distribution or by a vendor such as Docker. Mirrors are copies of a repository on servers around the world, so downloads come from somewhere near you.
The index lists every package with its version, dependencies and hash. apt update saves it in /var/lib/apt/lists/, and later apt commands read that copy. If your copy is weeks old, apt may ask a mirror for a version it has since deleted, and the download fails with 404 Not Found.
On top sits the Release file (InRelease when the signature is inside it), listing the hash of every index file. The distribution signs it: it computes a signature with its private key, a secret only it holds, and anyone with the matching public key can check it. Signature, index hash and package hash form an unbroken chain.
Your server's list of repositories lives in /etc/apt/sources.list.d/. On Debian 12, print it with:
cat /etc/apt/sources.list.d/debian.sources
The first entry looks like this (trimmed):
Types: deb
URIs: http://deb.debian.org/debian
Suites: bookworm bookworm-updates
Components: main
Signed-By: /usr/share/keyrings/debian-archive-keyring.gpg
URIs is the repository, Suites the release and its update channels (bookworm is Debian 12's code name), Components the section of the archive, and Signed-By the public key file. If cat says No such file or directory, look for ubuntu.sources on Ubuntu or /etc/apt/sources.list on older systems.
Plain http is fine for integrity, since a changed file breaks a hash or the signature, but not private: others can see what you fetch.
dnf keeps repositories as .repo files in /etc/yum.repos.d/: baseurl or metalink says where packages come from, and gpgcheck=1 checks each package's signature against the key in gpgkey. Any new repository means trusting its key's owner, so take keys only from the vendor's official instructions.
Good practice on servers
Three habits prevent most package surprises. First, refresh the index with sudo apt update right before installing or upgrading.
Second, read what will change. Before touching anything, apt prints its plan and waits for your answer:
sudo apt install nginx
On a fresh Debian 12 server, the plan ends like this (trimmed; counts and sizes are illustrative):
The following NEW packages will be installed:
… nginx nginx-common …
0 upgraded, 9 newly installed, 0 to remove and 0 not upgraded.
Need to get 2,345 kB of archives.
After this operation, 6,789 kB of additional disk space will be used.
Do you want to continue? [Y/n]
Answer y to go ahead, or n, and apt prints Abort. and changes nothing. Stop and think whenever the plan lists packages under will be REMOVED: apt upgrade never removes any, but apt full-upgrade may. Keep -y, which answers yes for you, for scripts whose changes you know. Unable to locate package means a typo or a stale index.
Third, hold versions in production, where the real service runs (production environments). Upgrades there should happen when you choose, after testing elsewhere. A hold stops apt from automatically installing, upgrading or removing a package:
sudo apt-mark hold nginx
apt confirms with one line:
nginx set on hold.
apt-mark showhold lists holds, and sudo apt-mark unhold nginx prints Canceled hold on nginx. To install an exact version, write nginx= and a version copied from a Version line of apt-cache show nginx. On dnf, sudo dnf versionlock add nginx locks the installed version (dnf 4 needs the python3-dnf-plugin-versionlock package first).
Check yourself
A new server's sudo apt install htop fails with Unable to locate package htop, though Debian has htop. Why?
apt searches only its local copy of the index, which is empty or stale on a new machine. Run sudo apt update, then install again.
Your server fetches packages over plain http from a university's mirror. Why can't the mirror slip you a modified nginx?
The changed file's hash would not match the index, and changing the index breaks the Release signature, which needs Debian's private key. apt refuses the file.
You hold nginx in production so routine upgrades skip it. What risk grows the longer the hold stays?
The hold blocks security fixes too, so nginx keeps a version with known holes. Review holds and upgrade on purpose, after testing.