Package Managers
On Linux you rarely download installers from websites. Instead a package manager installs software from the distribution's repositories: curated, signed collections of thousands of packages. It resolves dependencies (other packages a program needs), installs updates for everything with one command, and removes software cleanly.
Note: Installing packages needs root privileges and network access, so the commands in this lesson are shown with sample output. The runnable examples show how scripts detect and reason about packages.
The big families
| Distribution | Package format | Low-level tool | High-level tool |
|---|---|---|---|
| Debian, Ubuntu, Mint | .deb |
dpkg |
apt |
| Fedora, RHEL, Rocky, Alma | .rpm |
rpm |
dnf (formerly yum) |
| Arch, Manjaro | .pkg.tar.zst |
pacman |
|
| Alpine (containers) | .apk |
apk |
|
| openSUSE | .rpm |
rpm |
zypper |
| macOS (not Linux) | brew (Homebrew) |
The low-level tool installs a single package file; the high-level tool downloads from repositories and handles dependencies.
apt (Debian, Ubuntu)
sudo apt update # refresh the list of available packages
sudo apt upgrade # install all available updates
sudo apt install nginx jq # install packages (and their dependencies)
sudo apt remove nginx # remove (keeps config files)
sudo apt purge nginx # remove including config files
sudo apt autoremove # remove dependencies nobody needs any more
apt search "json processor" # search package descriptions
apt show jq # details about a package
apt list --installed | wc -l # count installed packages
dpkg -L jq # list the files a package installed
dpkg -S /usr/bin/jq # which package owns this file?Sample output of an install:
$ sudo apt install jq
Reading package lists... Done
Building dependency tree... Done
The following additional packages will be installed:
libjq1 libonig5
The following NEW packages will be installed:
jq libjq1 libonig5
0 upgraded, 3 newly installed, 0 to remove and 2 not upgraded.
Need to get 383 kB of archives.
After this operation, 1,079 kB of additional disk space will be used.
Do you want to continue? [Y/n] yapt update only refreshes the catalogue; it doesn't install anything. Forgetting it is why apt install sometimes can't find a package that exists.
dnf, pacman and apk
| Task | dnf (Fedora/RHEL) | pacman (Arch) | apk (Alpine) |
|---|---|---|---|
| Refresh + upgrade | sudo dnf upgrade |
sudo pacman -Syu |
apk update && apk upgrade |
| Install | sudo dnf install jq |
sudo pacman -S jq |
apk add jq |
| Remove | sudo dnf remove jq |
sudo pacman -R jq |
apk del jq |
| Search | dnf search jq |
pacman -Ss jq |
apk search jq |
| Owner of a file | rpm -qf /usr/bin/jq |
pacman -Qo /usr/bin/jq |
apk info --who-owns /usr/bin/jq |
In Dockerfiles you'll see these with flags that keep images small: apt-get install -y --no-install-recommends ..., apk add --no-cache ....
Which package manager is here?
Install scripts that support several distributions detect the available tool with command -v:
detect_pm() {
local pm
for pm in apt-get dnf pacman zypper apk brew; do
if command -v "$pm" > /dev/null; then echo "$pm"; return 0; fi
done
echo "unknown"; return 1
}
pm=$(detect_pm)
echo "package manager: $pm"
case $pm in
apt-get) echo "install with: sudo apt-get install -y jq" ;;
dnf) echo "install with: sudo dnf install -y jq" ;;
apk) echo "install with: apk add --no-cache jq" ;;
*) echo "install jq manually" ;;
esacpackage manager: apk
install with: apk add --no-cache jq
This sandbox is Alpine Linux, so apk is found. Reading /etc/os-release is the other common approach (. /etc/os-release; echo "$ID").
Is a tool installed?
Checking for the command is usually better than asking the package database, because package names differ between distributions (python3 vs python, fd-find vs fd):
for tool in git jq tar zip python3 docker; do
if command -v "$tool" > /dev/null; then
printf '%-8s installed\n' "$tool"
else
printf '%-8s missing\n' "$tool"
fi
donegit installed
jq installed
tar installed
zip installed
python3 missing
docker missing
Versions and holding upgrades
Package versions look like 1.7.1-3ubuntu0.2: the upstream version, then the distribution's revision. sort -V compares them correctly, which scripts use to check for a minimum version:
version_ge() { [ "$(printf '%s\n' "$2" "$1" | sort -V | head -n 1)" = "$2" ]; }
for installed in 1.6 1.7.1 1.10.0; do
if version_ge "$installed" "1.7"; then echo "$installed >= 1.7: ok"; else echo "$installed < 1.7: upgrade needed"; fi
done1.6 < 1.7: upgrade needed
1.7.1 >= 1.7: ok
1.10.0 >= 1.7: ok
To stop a package from being upgraded: sudo apt-mark hold nginx (Debian/Ubuntu) or dnf versionlock add nginx.
Other ways to install software
| Method | Notes |
|---|---|
| Snap, Flatpak | Sandboxed apps bundled with their dependencies, distribution-independent |
| Language managers | pip, npm, cargo, gem for libraries and dev tools (prefer virtual environments over sudo pip) |
| Containers | docker run an image instead of installing |
| From source | ./configure && make && sudo make install into /usr/local |
| A downloaded binary | Put it in ~/.local/bin or /usr/local/bin |
Test yourself
What does sudo apt update do?
- Installs all updates
- Refreshes the list of available packages
- Updates apt itself only
- Removes old packages
Which command installs htop on Fedora?
- sudo apt install htop
- sudo dnf install htop
- sudo pacman -S htop
- apk add htop
Why is command -v tool a good way for a script to check whether a tool exists?
- It installs the tool
- It works on every distribution regardless of package names
- It shows the package version
- It needs root