Linux

Package Management: apt, dnf, pacman

·2 min read

Three package managers cover most Linux systems in production: apt on Debian and Ubuntu, dnf on Fedora and RHEL family, pacman on Arch and derivatives. They solve the same problem with different vocabulary. Knowing the rosetta stone saves time on every box that isn't your daily driver.

The common operations

Operationaptdnfpacman
Refresh metadataapt updatednf check-updatepacman -Sy
Upgrade allapt upgradednf upgradepacman -Syu
Installapt install foodnf install foopacman -S foo
Removeapt remove foodnf remove foopacman -R foo
Searchapt search foodnf search foopacman -Ss foo
Show infoapt show foodnf info foopacman -Si foo
List installedapt list --installeddnf list installedpacman -Q
Which pkg owns filedpkg -S /pathdnf provides /pathpacman -Qo /path

Repository configuration

apt reads /etc/apt/sources.list and files in /etc/apt/sources.list.d/. Each line specifies a URL, suite, and components. Third-party repos add a file to sources.list.d/ and a signing key under /etc/apt/keyrings/.

dnf reads /etc/yum.repos.d/*.repo, INI-format. Each repo has its own file with URL, gpgcheck, enabled.

pacman reads /etc/pacman.conf for repos and /etc/pacman.d/mirrorlist for the URL list.

The lock file

All three serialize package operations behind a lock file. If you see "Could not get lock /var/lib/dpkg/lock" or similar, another process (often unattended-upgrades, packagekit, or a leftover apt session) is running. Don't rm the lock — find the process:

sudo lsof /var/lib/dpkg/lock-frontend       # apt
sudo lsof /var/lib/rpm/.rpm.lock            # dnf
sudo lsof /var/lib/pacman/db.lck            # pacman

Holding versions

Pinning a package to a specific version, or preventing an upgrade:

# apt
apt-mark hold nginx

# dnf
dnf versionlock add nginx-1.24.0

# pacman (edit /etc/pacman.conf)
IgnorePkg = nginx

Clean up

apt autoremove           # remove orphaned dependencies
apt clean                # wipe /var/cache/apt/archives

dnf autoremove
dnf clean all

pacman -Rns $(pacman -Qtdq)    # orphaned deps
paccache -r                     # keep last 3 versions

When things break

Half-installed package on Debian: dpkg --configure -a. Broken dependencies: apt install -f. RPM database corruption: rpm --rebuilddb. pacman keyring issues: pacman-key --refresh-keys.

Related