Skip to main content

Linux Package Management: APT and DNF

·968 words·5 mins
Linux Learning Lab
Author
Linux Learning Lab
Writing about code, tools, and workflows.
linux-beginner - This article is part of a series.
Part 2: This Article

What is a Package Manager?
#

A package manager handles installing, updating, and removing software on your system. Instead of downloading binaries manually, you tell the package manager what you want and it:

  1. Finds the software in a repository (a trusted server hosting packages)
  2. Resolves dependencies (other packages the software needs)
  3. Downloads and installs everything in the right place
  4. Tracks what’s installed so it can update or remove it later

Every major Linux distribution ships with a package manager. The two most common are:

Distribution FamilyPackage ManagerPackage Format
Debian, Ubuntu, Mintapt.deb
RHEL, Fedora, Rocky, AlmaLinuxdnf.rpm

Repositories
#

Repositories are remote servers that host packages. Your system is configured to trust specific repos and pull software from them.

Viewing configured repositories
#

Actionapt (Debian/Ubuntu)dnf (RHEL/Fedora)
List reposcat /etc/apt/sources.listdnf repolist
List all (including disabled)grep -r ^deb /etc/apt/sources.list.d/dnf repolist --all

Adding a repository
#

apt:

# Add a PPA (Ubuntu)
sudo add-apt-repository ppa:example/repo

# Add a third-party repo manually
echo "deb https://packages.example.com/ubuntu jammy main" | sudo tee /etc/apt/sources.list.d/example.list
sudo apt update

dnf:

# Add a repo from a URL
sudo dnf config-manager --add-repo https://packages.example.com/fedora/example.repo

# Or install a repo package
sudo dnf install https://packages.example.com/example-release.rpm

Core Operations
#

Update package lists / metadata
#

Before installing software, sync your local package database with the repos:

Actionaptdnf
Refresh package metadatasudo apt updatesudo dnf check-update

apt requires this as a separate step. dnf refreshes metadata automatically when needed but you can check manually.

Install a package
#

Actionaptdnf
Installsudo apt install nginxsudo dnf install nginx
Install without promptssudo apt install -y nginxsudo dnf install -y nginx
Install multiple packagessudo apt install nginx curl gitsudo dnf install nginx curl git

Remove a package
#

Actionaptdnf
Remove (keep config)sudo apt remove nginxsudo dnf remove nginx
Remove + config filessudo apt purge nginxsudo dnf remove nginx (removes configs by default)
Remove unused dependenciessudo apt autoremovesudo dnf autoremove

Upgrade installed packages
#

Actionaptdnf
Upgrade all packagessudo apt upgradesudo dnf upgrade
Upgrade with dependency changessudo apt full-upgradesudo dnf distro-sync

Searching for Packages
#

Actionaptdnf
Search by name/descriptionapt search nginxdnf search nginx
Show package detailsapt show nginxdnf info nginx
Find which package provides a fileapt-file search /usr/bin/htopdnf provides /usr/bin/htop
List installed packagesapt list --installeddnf list installed

Working with Package Files
#

Sometimes you need to install a package file directly (downloaded from a website, for example):

Actionaptdnf
Install a local package filesudo apt install ./package.debsudo dnf install ./package.rpm

Both will resolve dependencies from your configured repos automatically.

Package Information and History
#

What files does a package install?
#

Actionaptdnf
List files in installed packagedpkg -L nginxrpm -ql nginx
List files in a package filedpkg -c package.debrpm -qlp package.rpm

Which package owns a file?
#

Actionaptdnf
Find owning packagedpkg -S /usr/bin/curlrpm -qf /usr/bin/curl

Transaction history
#

dnf keeps a log of everything it’s done:

# View history
dnf history

# Undo the last transaction
sudo dnf history undo last

apt logs to /var/log/apt/history.log:

cat /var/log/apt/history.log

Holding / Locking Package Versions
#

Prevent a package from being upgraded:

apt:

# Hold a package
sudo apt-mark hold nginx

# Release the hold
sudo apt-mark unhold nginx

# View held packages
apt-mark showhold

dnf:

# Install the versionlock plugin if needed
sudo dnf install dnf-plugins-core

# Lock a package
sudo dnf versionlock add nginx

# Remove the lock
sudo dnf versionlock delete nginx

# List locks
dnf versionlock list

Cleaning Up
#

Actionaptdnf
Remove cached package filessudo apt cleansudo dnf clean all
Remove only outdated cachessudo apt autocleansudo dnf clean packages
Remove orphaned dependenciessudo apt autoremovesudo dnf autoremove

Understanding Dependencies
#

When you install a package, the package manager pulls in everything it needs:

# See what a package depends on
apt depends nginx
dnf deplist nginx

# See what depends on a package (reverse dependencies)
apt rdepends nginx
dnf repoquery --whatrequires nginx

Dependency conflicts are rare with standard repos but can happen when mixing third-party repositories. If you hit a conflict:

  1. Read the error — it usually names the conflicting packages
  2. Check if you have third-party repos that overlap with official ones
  3. Prefer the official repo version unless you have a specific reason not to

Key Differences Between apt and dnf
#

Aspectaptdnf
Metadata refreshManual (apt update)Automatic
Transaction rollbackNot built-indnf history undo
Config file handling on removeKept unless purgeRemoved with package
Module/stream conceptNoYes (dnf modules for versioned software)
Default inDebian, UbuntuFedora, RHEL 8+, Rocky, Alma

Other Package Managers
#

You’ll encounter these on other distributions:

ManagerDistributionNotes
zypperopenSUSE, SLESSimilar to dnf in capability
pacmanArch, ManjaroRolling release, very fast
apkAlpineMinimal, used heavily in containers
snapUbuntu (cross-distro)Sandboxed, auto-updating
flatpakCross-distroSandboxed desktop applications

The concepts are the same across all of them — repos, dependencies, install, remove, update. Only the commands differ.

Best Practices
#

  • Run apt update before installing anything on Debian/Ubuntu — stale metadata causes “package not found” errors
  • Use autoremove periodically to clean up orphaned dependencies
  • Prefer official repos over third-party ones — they’re tested against your distribution
  • Don’t mix packages from different distribution versions — a package built for Ubuntu 24.04 may break on 22.04
  • Use version locks when you need stability on a specific package version (databases, runtimes)
  • Check dnf history or /var/log/apt/history.log when troubleshooting — knowing what changed helps isolate problems
  • Keep your system updated — sudo apt upgrade or sudo dnf upgrade regularly patches security vulnerabilities
linux-beginner - This article is part of a series.
Part 2: This Article