Lesson 5 of 8 in Linux and the shell
Package management with apt and dnf
A package manager installs software from a repository, resolves the other packages it depends on, and records every file it puts on the system. apt is used on Debian and Ubuntu, and dnf on Fedora, RHEL and similar systems.
Draft. This lesson is written and waiting for a person to run every command and review it. It is safe to learn from, but check the official documentation for anything you depend on.
Read these first: Users, groups, permissions and sudo
The problem this solves
Installing a program by hand means finding the right download, copying files into the right folders and tracking what else it needs. Updating or removing it later means remembering all of that.
A package manager does this from a signed list of software maintained by your distribution. One command installs a program with its libraries, and one command updates or removes it cleanly.
It is also the safest way to get security fixes. When a library is patched, the update reaches every program that uses it, because they all share the same package.
How it works
- Data
- Control
- Runs code
apt update copies the repository's list into a local index, apt install reads that index, downloads the package, and hands it to dpkg to unpack the files.
- Repository to Local index: 1 apt update
- Local index to apt: 2 install: look up
- Repository to apt: 3 download package
- apt to dpkg: 4 hand over
- dpkg to Your system: 5 place the files
A repository is a server that holds packages plus an index describing every package, its version and what it depends on. The index is cryptographically signed so your system can check it came from the distribution.
apt update only refreshes your local copy of the index. It installs nothing. A fresh machine or container often has an empty index, which is why installing before updating fails.
apt install <name> looks the package up in the index and works out its dependencies. It downloads everything needed and passes it to dpkg, the low-level tool that unpacks the files and records them in a database. dnf follows the same shape, with rpm doing the low-level part.
Because the database knows every file, you can ask which package owns a file or list what a package installed. That makes removal clean and helps when a file shows up that you do not recognise.
Try it yourself
The read-only steps work on a Debian or Ubuntu machine. To practise installing without changing your own system, use the throwaway container in step 3. Fedora and RHEL equivalents are in the notes.
- 1See which packages are installed
apt list --installed 2>/dev/null | head -n 5 dpkg -l | wc -l
On Fedora or RHEL use
dnf list --installed | headandrpm -qa | wc -l. - 2Find which package owns a file, and list its files
dpkg -S /usr/bin/ls dpkg -L coreutils | head -n 8
The first command prints the owning package, which is
coreutilsforls. On Fedora or RHEL userpm -qf /usr/bin/lsandrpm -ql coreutils | head. - 3Install and remove a package in a throwaway container
docker run --rm -it ubuntu:24.04 bash # inside the container: apt update apt install -y tree tree --version apt remove -y tree exit
Needs Docker. The container is deleted when you exit, so nothing touches your own machine. A fresh container has no index, so
apt updatehas to come first. - 4Preview what an install would do
apt-cache policy curl apt -s install curl
apt-cache policyshows installed and candidate versions and which repository they come from.-ssimulates the install and changes nothing. On Fedora usednf info curl.
What breaks, and where to look
E: Unable to locate package <name>
- WHY IT HAPPENS
- The index does not list the package. The usual causes are a missing
apt updateon a fresh machine, a mistyped name, or software that is not in your enabled repositories. - CHECK FIRST
- Run
sudo apt update, thenapt-cache policy <name>orapt search <name>. If it is still missing, check the exact package name for your distribution release.
E: Could not get lock /var/lib/dpkg/lock-frontend. It is held by process 1234
- WHY IT HAPPENS
- Another package operation is running, often automatic updates in the background after boot. Only one tool may change the package database at a time.
- CHECK FIRST
- Run
ps -p 1234 -o pid,cmdto see what holds it, and wait for it to finish. Do not delete the lock files by hand.
E: Unable to correct problems, you have held broken packages.
- WHY IT HAPPENS
- A package needs a dependency version that your enabled repositories do not offer. This often follows adding a third-party repository or mixing repositories from different releases.
- CHECK FIRST
- Run
apt -s install <name>for a dry run, andapt-cache policy <name>to see which versions and sources exist. Remove the extra repository that disagrees with the rest.
When not to use this
- Distribution packages can be older than what the project currently ships. If you need a specific newer version of a language or tool, a version manager or an official container image is usually cleaner than a third-party repository.
- In scripts and Dockerfiles use
apt-get, which keeps a stable interface.aptis meant for interactive use and prints a warning when its output is parsed.
Check yourself
What does apt update do, and what does it not do?
It refreshes the local index of available packages. It does not install or upgrade any software.
How do you find which package a file came from?
Use dpkg -S <path> on Debian and Ubuntu, or rpm -qf <path> on Fedora and RHEL.
Why is a package manager safer than downloading a program by hand?
It installs from a signed repository, resolves dependencies, records every file, and delivers security fixes through the same update path.
Keep going
Lesson 12 of 92 in the DevOps path