Apt vs Dnf Quick Reference
A detailed reference comparing Apt and Dnf package managers, including core commands, configuration, repository management, and common troubleshooting tips for Debian/Ubuntu and RHEL/Fedora systems.
Managing software packages is a fundamental task for any Linux system administrator or developer. Two of the most prevalent package management systems are Advanced Package Tool (Apt), primarily used in Debian, Ubuntu, and their derivatives, and DNF (Dandified YUM), the successor to YUM, predominant in Fedora, CentOS (8+), and RHEL (8+).
While their underlying mechanisms differ, they share a common goal: simplifying software installation, updates, and removal. This reference provides a direct comparison of common tasks, highlighting command equivalents, configuration specifics, and nuances for both Apt and DNF.
Core Package Operations
The most frequent interactions with a package manager involve synchronizing repository data, installing, removing, and upgrading packages. Below is a direct comparison of these core commands.
Updating Package Indexes
Before any package operations, it's crucial to update the local cache of available packages from configured repositories. This ensures you're working with the latest metadata.
- Apt (Debian/Ubuntu):
sudo apt updateThis command downloads new package lists from the repositories and updates the
/var/lib/apt/lists/directory. It does not install or upgrade any packages. - DNF (RHEL/Fedora):
sudo dnf check-updateThis command checks if there are any updates available for installed packages. It doesn't download metadata explicitly like
apt update, but rather implicitly refreshes the cache if needed during the check. To explicitly refresh metadata, you might usesudo dnf makecacheorsudo dnf update --refresh.
Upgrading All Installed Packages
After updating the package index, upgrading brings all installed packages to their latest available versions.
- Apt:
sudo apt upgradePerforms a standard upgrade, installing newer versions of packages. It will not remove existing packages or install new packages to satisfy dependencies unless explicitly told to. For more aggressive upgrades, use
sudo apt full-upgrade, which handles dependency changes, removing old packages and installing new ones as needed. - DNF:
sudo dnf upgradeThis command is more comprehensive than
apt upgrade, functioning similarly toapt full-upgrade. It upgrades all installed packages, resolves dependencies, removes obsolete packages, and installs new dependencies as required.dnf updateis an alias fordnf upgrade.
Installing Packages
To install a specific software package.
- Apt:
sudo apt install [package_name]Installs the specified package and its dependencies. You can install multiple packages by listing them space-separated:
sudo apt install nginx php-fpm. - DNF:
sudo dnf install [package_name]Installs the specified package and its dependencies. Multiple packages can be installed simultaneously:
sudo dnf install httpd mariadb-server.
Removing Packages
To uninstall software.
- Apt:
sudo apt remove [package_name]Removes the package but leaves configuration files behind. To remove the package and its configuration files, use
sudo apt purge [package_name]. - DNF:
sudo dnf remove [package_name]Removes the package. DNF also removes any dependencies that are no longer needed by other installed packages. Configuration files are typically left behind, usually in
/etc/.
Searching for Packages
When you don't know the exact package name or want to find related software.
- Apt:
apt search [search_term]Searches for packages whose names or descriptions match the search term. By default, it searches cached package lists.
- DNF:
dnf search [search_term]Searches for packages matching the term in their names, summaries, and descriptions across enabled repositories.
Advanced Package Management
Beyond basic operations, both Apt and DNF offer powerful features for managing dependencies, cleaning up, and handling repository configurations.
Listing Installed Packages and Information
- Apt:
apt list --installedshows all installed packages.apt show [package_name]provides detailed information about a specific package (version, size, dependencies). - DNF:
dnf list installeddisplays installed packages.dnf info [package_name]provides detailed information.
Cleaning Up Unused Dependencies and Cache
- Apt:
sudo apt autoremoveRemoves packages that were automatically installed to satisfy dependencies for other packages but are no longer needed.
sudo apt cleanClears out the local repository of downloaded package files (
.debfiles) from/var/cache/apt/archives/. - DNF:
sudo dnf autoremoveRemoves packages that are no longer required as dependencies.
sudo dnf clean allRemoves all cached repository metadata, package files, and downloaded transaction information from
/var/cache/dnf/.
Managing Repositories
Repositories are crucial for where your package manager sources its software.
- Apt: Repositories are defined in
/etc/apt/sources.listand files within/etc/apt/sources.list.d/. Each line typically specifies the repository URL, distribution, and components (e.g., main, universe). Example entry:deb http://archive.ubuntu.com/ubuntu/ focal main restricted universe multiverse. New repositories are often added usingsudo add-apt-repositorywhich handles adding the line and importing the GPG key. - DNF: Repositories are defined in
.repofiles located in/etc/yum.repos.d/. Each file contains one or more[repository_id]sections.
# Example DNF repository file: /etc/yum.repos.d/epel.repo
[epel]
name=Extra Packages for Enterprise Linux $releasever - $basearch
metalink=https://mirrors.fedoraproject.org/metalink?repo=epel-$releasever&arch=$basearch
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-$releasever
To enable/disable a DNF repository temporarily: sudo dnf --enablerepo=[repo_id] install [package] or sudo dnf --disablerepo=[repo_id] update. To permanently enable/disable, edit the enabled=1 or enabled=0 line in the respective .repo file.
Common Pitfalls and Troubleshooting
- Broken Dependencies: Both managers can encounter dependency issues. For Apt,
sudo apt install -f(fix-broken) orsudo apt --fix-broken installoften resolves these. DNF is generally better at resolving dependencies proactively, but if issues arise,sudo dnf distro-synccan sometimes help by synchronizing installed packages with the current available versions. - GPG Key Errors: If you get errors about missing or invalid GPG keys, it means the package manager cannot verify the authenticity of packages from a repository. For Apt, you'll need to import the correct GPG key, often with
sudo apt-key adv --keyserver keyserver.ubuntu.com --recv-keys [KEY_ID]or by placing.ascfiles in/etc/apt/trusted.gpg.d/. For DNF, ensure thegpgkeyURL in the.repofile is correct and the key is installed in/etc/pki/rpm-gpg/. - Repository Unavailability: If
apt updateordnf check-updatefail due to network errors or repository URLs, double-check yoursources.listor.repofiles for typos or outdated URLs. Firewalls or proxy settings can also interfere. - Conflicting Packages: Sometimes packages from different repositories can conflict. DNF handles this more robustly, often suggesting solutions. Apt may require manual intervention to remove the conflicting package or hold a specific version using
sudo apt-mark hold [package_name]. - Slow Operations: If updates or installs are slow, check your mirror lists. For Apt, tools like
netselect-apt(though less common now) or manually editingsources.listto use closer mirrors can help. DNF automatically selects fast mirrors, but issues could indicate network problems or overloaded mirrors.