Stackvora.tech

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 update

    This 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-update

    This 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 use sudo dnf makecache or sudo 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 upgrade

    Performs 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 upgrade

    This command is more comprehensive than apt upgrade, functioning similarly to apt full-upgrade. It upgrades all installed packages, resolves dependencies, removes obsolete packages, and installs new dependencies as required. dnf update is an alias for dnf 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 --installed shows all installed packages. apt show [package_name] provides detailed information about a specific package (version, size, dependencies).
  • DNF: dnf list installed displays installed packages. dnf info [package_name] provides detailed information.

Cleaning Up Unused Dependencies and Cache

  • Apt:
    sudo apt autoremove

    Removes packages that were automatically installed to satisfy dependencies for other packages but are no longer needed.

    sudo apt clean

    Clears out the local repository of downloaded package files (.deb files) from /var/cache/apt/archives/.

  • DNF:
    sudo dnf autoremove

    Removes packages that are no longer required as dependencies.

    sudo dnf clean all

    Removes 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.list and 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 using sudo add-apt-repository which handles adding the line and importing the GPG key.
  • DNF: Repositories are defined in .repo files 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) or sudo apt --fix-broken install often resolves these. DNF is generally better at resolving dependencies proactively, but if issues arise, sudo dnf distro-sync can 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 .asc files in /etc/apt/trusted.gpg.d/. For DNF, ensure the gpgkey URL in the .repo file is correct and the key is installed in /etc/pki/rpm-gpg/.
  • Repository Unavailability: If apt update or dnf check-update fail due to network errors or repository URLs, double-check your sources.list or .repo files 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 editing sources.list to use closer mirrors can help. DNF automatically selects fast mirrors, but issues could indicate network problems or overloaded mirrors.

Back to the knowledge base · Ask the AI assistant