Package managers
On Linux you rarely download installers from websites. You ask the package manager for software by name, and it downloads it from the distribution's trusted repositories, installs everything it depends on, and keeps it all updated. One command updates every program on the machine.
Which one does my system use?
It depends on the distribution family:
| Family | Distributions | Package manager | Package format |
|---|---|---|---|
| Debian | Ubuntu, Debian, Linux Mint | apt |
.deb |
| Red Hat | RHEL, Fedora, Rocky, AlmaLinux, Amazon Linux | dnf (older: yum) |
.rpm |
| Alpine | Alpine (tiny, popular in Docker images) | apk |
.apk |
| Arch | Arch, Manjaro | pacman |
.pkg.tar.zst |
grep -E "^(NAME|VERSION|ID_LIKE)=" /etc/os-release
command -v apt dnf apk pacmanNAME="Ubuntu" VERSION="24.04.5 LTS (Noble Numbat)" ID_LIKE=debian /usr/bin/apt
This lesson uses apt, the most common on servers; the others work the same way with different words (there's a table at the end).
Updating the package list
apt keeps a local list of every available package. Refresh it before installing anything:
sudo apt-get update 2>&1 | tail -1Reading package lists...
apt update only downloads the list; it doesn't change any software.
Searching and inspecting
apt-cache search --names-only "^htop$"
apt-cache policy htop | head -3
apt-cache depends jq | grep Dependshtop - interactive processes viewer htop: Installed: (none) Candidate: 3.3.0-4build1 Depends: libc6 Depends: libjq1
policy shows the installed and available versions; depends shows what else a package needs.
Installing and removing
sudo DEBIAN_FRONTEND=noninteractive apt-get install -y htop > /dev/null 2>&1
command -v htop
dpkg -s htop | grep -E "^(Status|Version)"
dpkg -L htop | grep bin//usr/bin/htop Status: install ok installed Version: 3.3.0-4build1 /usr/bin/htop
| Command | Does |
|---|---|
sudo apt install nginx |
Install (and its dependencies). Add -y to skip the "Do you want to continue?" question in scripts |
sudo apt remove nginx |
Remove the program, keep its config files |
sudo apt purge nginx |
Remove it and its config |
sudo apt autoremove |
Remove dependencies nothing needs any more |
apt list --installed |
Everything installed |
dpkg -L pkg |
Files a package installed |
dpkg -S /path/file |
Which package a file belongs to |
dpkg -S /usr/bin/curl
apt list --installed 2>/dev/null | wc -lcurl: /usr/bin/curl 244
💡
aptis the friendly command for humans. In scripts and Dockerfiles, useapt-getandapt-cache: their output format is stable, andaptprints a warning when used in scripts.
Upgrading
sudo apt update && sudo apt upgrade # upgrade everything that's installed
sudo apt full-upgrade # also allow removing packages to complete upgrades
sudo apt install --only-upgrade openssl # upgrade one packageOn servers, security updates are so important that Ubuntu can install them automatically:
sudo apt install unattended-upgrades
sudo dpkg-reconfigure --priority=low unattended-upgradesWhen the kernel or a core library is updated, the change only takes effect after a reboot. Ubuntu creates /var/run/reboot-required to tell you so.
Where packages come from
Repositories are listed in /etc/apt/sources.list.d/. Every package is signed, and apt refuses packages whose signature doesn't match a trusted key:
ls /etc/apt/sources.list.d/
grep -E "^(URIs|Suites|Components):" /etc/apt/sources.list.d/ubuntu.sources | head -3ubuntu.sources URIs: http://ports.ubuntu.com/ubuntu-ports/ Suites: noble noble-updates noble-backports Components: main universe restricted multiverse
Ubuntu's components are main (supported by Canonical), universe (community-maintained), restricted and multiverse (non-free licences).
Adding a vendor's repository
The versions in your distribution's repository can be a year or two old. For newer versions of tools like Docker, Node.js or PostgreSQL, vendors publish their own repositories. The steps are always the same: download their signing key, add their repository, update, install. For example, Docker's official instructions:
sudo install -m 0755 -d /etc/apt/keyrings
sudo curl -fsSL https://download.docker.com/linux/ubuntu/gpg -o /etc/apt/keyrings/docker.asc
echo "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/docker.asc] \
https://download.docker.com/linux/ubuntu $(. /etc/os-release && echo "$VERSION_CODENAME") stable" \
| sudo tee /etc/apt/sources.list.d/docker.list
sudo apt update && sudo apt install docker-ce⚠️ Adding a repository means trusting that vendor with root access to your machine: their packages install as root. Only add repositories from the official vendor's documentation. Be very careful with
curl … | sudo bashinstallers for the same reason. Read the script first.
Installing a single .deb file
sudo apt install ./some-tool_1.2.3_amd64.deb # the ./ makes apt treat it as a file and fetch dependenciesOther ways to install software
| Method | Example | Notes |
|---|---|---|
| Snap | sudo snap install code --classic |
Ubuntu's sandboxed app packages; auto-updating |
| Flatpak | flatpak install flathub org.gimp.GIMP |
Desktop apps on any distribution |
| Language package managers | pip, npm, cargo, go install |
For libraries and developer tools. Use them per project or per user, not with sudo |
| Static binaries | Download into /usr/local/bin |
Common for Go tools like kubectl and terraform |
| Docker | docker run postgres:18 |
Run software without installing it on the host at all |
The same tasks, other distributions
| Task | apt (Ubuntu) | dnf (RHEL/Fedora) | apk (Alpine) |
|---|---|---|---|
| Refresh the list | apt update |
dnf check-update |
apk update |
| Install | apt install nginx |
dnf install nginx |
apk add nginx |
| Remove | apt remove nginx |
dnf remove nginx |
apk del nginx |
| Upgrade all | apt upgrade |
dnf upgrade |
apk upgrade |
| Search | apt search word |
dnf search word |
apk search word |
| Which package owns a file | dpkg -S /usr/bin/x |
rpm -qf /usr/bin/x |
apk info --who-owns /usr/bin/x |
In a Dockerfile, install and clean up in one RUN so the package lists don't bloat the image:
RUN apt-get update \
&& apt-get install -y --no-install-recommends curl ca-certificates \
&& rm -rf /var/lib/apt/lists/*Practice
- Find out which package owns
/usr/bin/ssh. - Install
tree, run it on your home folder, then remove it withpurgeandautoremove. - Check whether a newer version of
opensslis available withapt-cache policy openssl. - Write the Alpine version of the Dockerfile
RUNline above.
1.What does sudo apt update do?
2.Which command installs a package on Fedora or RHEL?
3.What's the difference between apt remove and apt purge?
4.Why is adding a third-party repository a security decision?