Software Package Management: apt, dnf, apk

K
Kai··4 min read

So far we've used the software that ships with the system. This article covers how to add software — through the package manager. This is the right way to install software on Linux: not downloading random files, but pulling from the distro's official package repository, with dependencies handled automatically.

What a package manager does

When you install a piece of software, it usually needs other libraries (dependencies). The package manager handles that: it knows what a package needs, downloads and installs them too, and tracks everything installed so you can later remove it cleanly.

It works with two things:

  • Repository (repo): the package store on the network run by the distro.
  • Package index: the list of packages + versions available in the repo, downloaded to the machine. This is why there's a "refresh the list" step before installing (more on this below).

Each distro family has its own manager

The package manager differs by distro family — it's the biggest difference between distros (Article 0):

  • apt — the Debian family: Debian, Ubuntu, Mint. .deb packages.
  • dnf (successor to yum) — the Red Hat family: Fedora, RHEL, CentOS, Rocky. .rpm packages.
  • apk — Alpine (very popular in Docker containers because it's lightweight).
  • pacman — Arch, Manjaro.

The commands differ but the idea is identical. An equivalence table for the common operations:

Operation              apt (Ubuntu)          dnf (Fedora)      apk (Alpine)
──────────────────────────────────────────────────────────────────────────
Refresh the list       apt update            dnf check-update  apk update
Install a package      apt install <pkg>     dnf install <pkg> apk add <pkg>
Remove a package       apt remove <pkg>      dnf remove <pkg>  apk del <pkg>
Upgrade everything     apt upgrade           dnf upgrade       apk upgrade
Search for a package   apt search <term>     dnf search <term> apk search <term>
Show package info      apt show <pkg>        dnf info <pkg>    apk info <pkg>

Learn one and the others are just looking up the command name.

apt: used on Ubuntu/Debian

Since our lab is Ubuntu, let's go deep on apt. (On a real server you usually need sudo before these commands — Article 12; in a container you're root so you don't.)

Refresh the package list first — the step beginners often forget:

apt update

Install a package:

apt install jq
... jq-1.7 ...

(jq is a JSON processing tool — we've used it a lot in earlier series.) Remove:

apt remove jq          # remove the package
apt autoremove         # remove dependencies no longer needed by anything

Search and view info:

apt search json        # find packages related to "json"
apt show jq            # version, description, dependencies

View installed packages (the low-level dpkg command):

dpkg -l                # list every installed package
dpkg -l | grep nginx   # which packages relate to nginx

An important distinction: update ≠ upgrade

This is the most common misunderstanding with apt:

  • apt update — only refreshes the package list (downloads the latest package index from the repo). It does not install or upgrade anything. It just tells apt "what packages/versions currently exist".
  • apt upgradeupgrades the installed packages to the latest version the (just-updated) list knows about.

So the order for upgrading the system is always:

apt update && apt upgrade

update first to learn what's new, then upgrade to install it. Run apt upgrade while forgetting update and apt still uses the old list, never seeing the new versions. Remember: update = refresh the list, upgrade = upgrade the software.

Why you often see apt-get update && apt-get install -y ... in one line in a Dockerfile (Article 5 of the Docker series): because the base image ships without a package index (to stay lightweight), so you have to update to download the list before you install. The -y flag auto-answers "yes" to the confirmation prompts (needed for automated runs).

apt (newer, friendlier for users) and apt-get (older, stable for scripts) are nearly equivalent for the basic commands. In scripts you should use apt-get because its interface is more stable.

Installing software outside the repo

Sometimes the software you need isn't in the default repo. A few ways (in order of preference for safety/convenience):

  • Add the vendor's official repo then apt install as usual — the cleanest way, still gets automatic updates.
  • Download a .deb package then apt install ./file.deb — manual install, but apt still manages it.
  • Snap/Flatpak — package formats that bundle dependencies, available across many distros.

Avoid downloading and running unfamiliar binaries directly if there's an option via the repo — repos are signed and vetted, which is safer.

🧹 Cleanup

apt remove -y jq 2>/dev/null
apt autoremove -y 2>/dev/null
apt clean        # delete the cache of downloaded packages (reclaim disk)

Wrap-up

Install software on Linux through the package manager: apt (Ubuntu/Debian), dnf (Fedora/RHEL), apk (Alpine) — different commands but the same idea (install/remove/search/upgrade from a repo, handling dependencies automatically). With apt, remember well that update refreshes the list ≠ upgrade upgrades the software, and the standard pattern apt update && apt upgrade. Prefer installing via the official repo over downloading unfamiliar binaries.

Software installed — but who gets to install and run what? Article 12 moves to user management: users, groups, and sudo — administrative privileges on Linux.