Linux › Section P · Compiling software from source › Topic P.2
Installation practice and package managers
Most software on a Linux system should arrive through the distribution's package manager, which resolves dependencies, checks signatures and delivers security fixes. Knowing those tools well, and knowing when a source build or a self-contained app format is the better choice, keeps a system tidy and safe.
What a package manager does
A package is an archive of a program's files plus metadata: its name, version, a description, the other packages it depends on, and scripts to run on install or removal. A package manager installs packages, records every file each one owns, resolves dependencies, upgrades and removes them cleanly. Packages come from repositories, servers run by the distribution that publish signed package lists. There are two layers. Low-level tools work on single package files: dpkg for .deb files on Debian and Ubuntu, rpm for .rpm files on Fedora and RHEL. High-level tools fetch from repositories and solve dependencies: apt and dnf (Arch uses pacman, Alpine apk). Both families need root, so commands below run with sudo.
apt on Debian and Ubuntu
sudo apt update refreshes the package lists from the repositories; it installs nothing. sudo apt upgrade then installs newer versions of installed packages, and sudo apt full-upgrade also allows removals when dependencies change. sudo apt install htop installs a package and its dependencies; sudo apt remove htop removes it but keeps configuration files, while sudo apt purge htop removes those too. sudo apt autoremove removes dependencies nothing needs any more. apt search editor and apt show htop find and describe packages, and apt list --installed lists what is present. Search and show need no sudo, because they only read.
dnf on Fedora and RHEL
dnf combines refreshing and upgrading: sudo dnf upgrade fetches current metadata and upgrades everything, and dnf check-update only lists what is pending. sudo dnf install htop and sudo dnf remove htop install and remove; sudo dnf autoremove clears unneeded dependencies. dnf search and dnf info find and describe packages, and dnf list --installed lists installed ones. dnf provides answers a question apt cannot without a helper: which package supplies a file, as in dnf provides '*/zlib.h'. dnf history lists past transactions, and dnf history undo can reverse one. Current Fedora releases use dnf5, which keeps the same commands.
dpkg and rpm: single packages
The low-level tools are for querying and for package files you downloaded. On Debian systems, dpkg -l lists installed packages, dpkg -L htop lists the files a package installed, and dpkg -S /usr/bin/htop names the package owning a file. sudo dpkg -i file.deb installs a file but does not fetch missing dependencies, so prefer sudo apt install ./file.deb, which does. On RPM systems, rpm -qa lists installed packages, rpm -ql htop its files, rpm -qf /usr/bin/htop the owner of a file, and rpm -qi htop its details. rpm -V htop verifies installed files against the package database, revealing changed or missing files.
Worked example: one task on both families
Install, inspect and remove the htop process viewer. 1. Refresh: sudo apt update on Debian; dnf does this itself. 2. Find it: apt search htop or dnf search htop. 3. Install: sudo apt install htop or sudo dnf install htop; each shows the dependencies it will add and asks to confirm. 4. Inspect: dpkg -L htop or rpm -ql htop lists its files, including /usr/bin/htop. 5. Confirm ownership the other way: dpkg -S /usr/bin/htop or rpm -qf /usr/bin/htop. 6. Remove: sudo apt remove htop or sudo dnf remove htop, then autoremove.
Repositories and trust
Repositories are configured in files. Debian and Ubuntu use /etc/apt/sources.list.d/, now often in the newer .sources (deb822) format, with a Signed-By field naming the repository's key; Fedora and RHEL use .repo files in /etc/yum.repos.d/ with gpgcheck=1. Package managers verify those signatures, so a tampered package is rejected. Adding a third-party repository, such as an Ubuntu PPA, RPM Fusion for Fedora or EPEL for RHEL, means trusting its maintainers with root-level access, because package scripts run as root. Add only sources you trust, and let tools such as unattended-upgrades on Debian or dnf-automatic on Fedora apply security updates promptly.
Snap, Flatpak and AppImage
Universal formats bundle an application with its libraries so one build runs on many distributions, usually in a sandbox. Snap, from Canonical, is built into Ubuntu: snap install, snap list and snap refresh manage snaps, which update automatically. Flatpak, favoured by Fedora and many desktops, usually installs from the Flathub repository: flatpak install flathub followed by an application ID, flatpak run, and flatpak update. AppImage packs an application into one executable file with no installation. These formats suit desktop applications that need newer versions; system tools and servers are better served by native packages. Language tools such as pip and npm belong in per-project environments.
When to build it yourself
"Build systems, source packages, and GNU Autoconf" showed how; the harder question is when. Building yourself gives control over options and versions, a clearer understanding of the software, and an installation that is easy to copy or back up. It also costs time, and a self-built package never upgrades itself, which matters most for network-facing software that needs prompt security fixes. Worse, it can overwrite files from a distribution package of the same software, or be overwritten by one later. So first check whether a distribution package, a backport or a universal package exists, and build from source only for a clear reason.
Where to install
The default prefix /usr/local exists for locally built software: package managers and distribution upgrades leave it alone, and /usr/local/bin is normally early in PATH. It suits a few small programs, but after many installs nobody remembers which file came from where. /opt suits self-contained products, each in its own /opt/name directory. A tidy middle way gives each package its own prefix, such as /usr/local/pkg/tool-1.4, and links its programs into /usr/local/bin, a scheme GNU Stow automates. Better still, turn the build into a real package: checkinstall wraps make install into a .deb, and rpmbuild with a spec file produces an .rpm.
Worked example: finding a missing header's package
A source build stops with zlib.h: No such file or directory. 1. Recognise the cause: the header belongs to a library's development package, which is not installed. 2. On Debian or Ubuntu, install the file-search helper and refresh its index: sudo apt install apt-file, then sudo apt-file update. 3. Search: apt-file search zlib.h points to zlib1g-dev. 4. On Fedora or RHEL, ask dnf directly: dnf provides '*/zlib.h' points to the zlib development package, installable as zlib-devel. 5. Install it: sudo apt install zlib1g-dev or sudo dnf install zlib-devel. 6. Rerun configure, which now finds both the header and zlib's pkg-config file. The pattern repeats: -dev on Debian, -devel on Fedora.
Common mistakes to avoid
Running apt upgrade without apt update first, so nothing new is found. Using dpkg -i and leaving dependencies broken instead of apt install ./file.deb. Expecting apt remove to delete configuration files, which needs purge. Adding a random repository or piping a downloaded script into sudo bash without checking its source. Mixing a source build of a program into /usr with the distribution's copy. Forgetting that self-built and AppImage software receive no security updates unless you fetch them. Running sudo pip install into the system Python on a distribution that manages it.
Key takeaways
Packages bundle files, metadata, dependencies and scripts; repositories publish them signed. apt (update, upgrade, install, remove, purge, autoremove, search, show) and dpkg (-l, -L, -S, -i) serve Debian and Ubuntu; dnf (upgrade, install, remove, search, info, provides, history) and rpm (-qa, -ql, -qf, -qi, -V) serve Fedora and RHEL. Trust repositories deliberately. Snap, Flatpak and AppImage suit desktop apps. Build from source only for a reason, install under /usr/local, /opt or per-package prefixes, and prefer turning builds into packages.
Flashcards
Flashcard
Program files plus metadata, dependencies and install scripts
Test your understanding
Question 1 of 5
On Ubuntu, sudo apt upgrade reports nothing to upgrade, although a fix was released yesterday. What was most likely skipped?
Question 2 of 5
Which command shows which installed package owns /usr/bin/htop on a Fedora system?
Question 3 of 5
A downloaded tool.deb fails with dpkg -i because two libraries are missing. What is the better approach?
Question 4 of 5
Why is adding a third-party repository a security decision?
Question 5 of 5
You built a network server from source into /usr/local. What disadvantage must you plan for?
