Featured image for a Linux topic.

A package manager makes it simple to install GNU/Linux applications on a local computer. Before package management became commonplace, installing applications was a tedious, error-prone undertaking. The ease a package manager brings to installing an application on a Linux computer has been a major factor contributing to the widespread adoption of Linux as a mainstream operating system for both business and home users.

Package management under Linux is divided, however. Two major systems co-exist:

  • The APT package management system for Debian and its many derivatives, notably Ubuntu. Packages are marked with the .deb suffix and are managed through the apt command-line interface (CLI).
  • The RPM package management system for Red Hat Enterprise Linux and its derivatives, notably Fedora and CentOS Stream. Packages are marked with the .rpm suffix and are managed through the dnf CLI.

This article is for developers who are currently using Debian-based systems and are familiar with APT, but want to start using a system in the Red Hat Enterprise Linux family. The article explains the similarities and differences between APT and RPM. I show how to execute specific, commonplace package management tasks using each system.

Understanding apt, dnf, and yum

Debian users are accustomed to managing their packages via the apt command. Switching to the current RPM tool, dnf, is the topic of this article.

You might also have seen references to a yum command. Both dnf and yum are command-line utilities that work with RPM packages. Red Hat originally released and depended on yum, which is an acronym for Yellowdog Updater, Modified. dnf, an abbreviation for dandified yum, is the follow-up technology—based on yum, as the name implies.

Today, dnf is the default package management utility for Red Hat Enterprise Linux, Fedora, and CentOS Stream, and has been so since Fedora 22, CentOS 8, and Red Hat Enterprise Linux 8, respectively. yum has been deprecated as the default package manager in the Red Hat family of distributions, so while yum commands currently work, it's best to use just dnf.

Understanding package discovery and installation

The pattern for finding and installing a Linux package is essentially the same whether you're using apt or dnf.

When you execute a command to install a package, the package manager looks at configuration files on the local machine to determine the location of a repository that has a given package on the internet. Then the installation command downloads the package along with its dependencies from the internet. Finally, the package manager installs and configures the application on the local machine. Figure 1 illustrates the process.

Figure 1: A package manager gets information from the local machine to retrieve a package from a repository.

Although the basic installation process is similar for both the RPM and APT package managers, there are distinctions when it comes to implementation. Besides the commands used, there's a bit of difference in the way these commands consult files to find and install packages.

When you invoke a package manager's installation command, the package manager first looks to see whether the package of interest is present and already installed. APT looks in the /var/cache/apt/archives directory for the presence of the package's .deb file. Under RPM, the package manager inspects the directories in /var/cache/dnf/ for a package's .rpm file.

If the package's .deb or .rpm file is not present, the installer inspects files that describe the locations of repositories on the internet. In some cases, these files can report the packages that are stored in a particular repository as well. Under APT, the information is stored in the file /etc/apt/sources.list or in .list files in the directory /etc/apt/sources.list.d. Under RPM, repository details are stored in .xml files or compressed .solvx files in the cache directory /var/cache/dnf/. Also, general information about a repository is stored in .repo files in the directory /etc/yum.repos.d.

After discovering information about a package and repository, the installer goes to a remote repository to find and install the package. In some cases, the installer might have to go to a number of remote repositories looking for a package. Should the installer not find a package of interest, the package manager reports an error.

Thus, the overall pattern for package discovery and installation for APT and RPM is similar. The difference is in the CLI tools used. The structure and contents of the file system used by the package installer on the local computer differ as well.

Most users migrating from Debian's apt to RPM's dnf never have to concern themselves with the difference between the internals of the APT and RPM package managers. Low-level operations have been abstracted away by the CLI tools.

Executing commonplace commands

In most cases, the main concern of developers migrating from apt to dnf is installing, removing, and updating packages, which we'll cover in this section. But first, I'll show you how to add a repository to search and how to list packages and known repositories.

Adding a repository for the package manager to search

To install a package on your computer, the CLI tool needs to know where the repositories containing the packages are. Typically, when you first set up your computer, whether it's running an operating system from the Debian family or the Red Hat Enterprise Linux family, information about the basic repositories that host the usual packages for the given operating system is included in the OS by default. However, there might be times when you need to search other repositories. This section shows the commands for adding information about a repository to a local computer.

To add information about a repository to a computer running Debian, Ubuntu, etc., enter:

# add-apt-repository <repository identification information>

For example, the following command installs information for the MongoDB database on a  Debian machine:

# sudo add-apt-repository 'deb [arch=amd64] https://repo.mongodb.org/apt/ubuntu bionic/mongodb-org/4.0 multiverse'

Note: Commands that change system software must be entered as the superuser (root). Hence, these commands are prefixed with sudo. I show the command prompt # as a reminder that you must be running as root.

To add information about a repository to a computer running Red Hat Enterprise Linux, Fedora, or CentOS Stream, enter:

# dnf config-manager --add-repo <repo_url>

The following example installs the information about a mirror repository for CentOS 9 on the local machine:

# sudo dnf config-manager --add-repo="https://mirror.aarnet.edu.au/pub/centos/9"

Updating the repository

You also want to routinely make sure that current packages on the host computer are up to date. To update existing packages on Debian, run the following command:

# sudo apt update

To update existing packages on a computer in the Red Hat Enterprise Linux family, execute the following command:

# sudo dnf update

Installing an application

As mentioned previously, the pattern for installing packages on a host computer is similar in Debian and machines in the Red Hat Enterprise Linux family.

To install a package on a Debian machine, enter:

# sudo apt install <package_name>

The following example installs the jq utility for parsing and filtering JSON files on a Debian machine:

# sudo apt install jq

To install a package on a machine in the Red Hat Enterprise Linux family, enter:

# sudo dnf install <package_name>

The following example installs the jq utility on a machine in the Red Hat Enterprise Linux family:

# sudo dnf install jq

Removing an application

The pattern for removing an application is similar for systems based on Debian and on Red Hat Enterprise Linux. The difference is the CLI tool used.

To remove a package on a Debian machine, enter:

# sudo apt remove <package_name>

The following example removes the jq utility from a Debian machine:

# sudo apt remove jq

To remove a package on Red Hat Enterprise Linux, enter:

# sudo dnf remove <package_name>

The following example removes the jq utility from a Red Hat Enterprise Linux machine:

# sudo dnf remove jq

Getting a list of packages installed on a host computer

Listing the packages installed on a local machine can furnish useful information, particularly for auditing and system management.

To list all the packages installed on a machine running Debian, enter:

$ apt list

The following example shows you how to use that command in conjunction with the grep command to filter the results using a regular expression. The regular expression in this example saves only the lines that start with the characters git:

$ apt list | grep '^git'

The following output shows a partial list of the results:

git-annex-remote-rclone/focal,focal 0.6-1 all
git-annex/focal 8.20200226-1 amd64
git-build-recipe/focal,focal 0.3.6 all
git-buildpackage-rpm/focal,focal 0.9.19 all
git-buildpackage/focal,focal 0.9.19 all
git-cola/focal,focal 3.6-1 all
git-crecord/focal,focal 20190217~git-1 all

To list the packages installed on a Red Hat Enterprise Linux machine, enter:

$ dnf list installed

The following example shows how to use that command to get a list of packages installed on a Red Hat Enterprise Linux machine and then pick out lines that start with git:

$ dnf list installed | grep '^git'

The command produces the following output:

git.x86_64                       2.35.3-1.fc35                      @updates
git-core.x86_64                  2.35.3-1.fc35                      @updates
git-core-doc.noarch              2.35.3-1.fc35                      @updates

Getting a list of repositories known to a host computer

Debian and the Red Hat Enterprise Linux family have different methods for listing repositories known to a given local computer.

Under a default installation of Debian, no single command has the logic to report known repositories. Instead, you have to finesse existing commands.

One way to list known repositories is to use the apt-cache policy command to return the known repositories, as shown in the following example:

$ apt-cache policy |grep http |awk '{print $2 " " $3}' |sort -u

We have seen grep already. The awk command that follows in the pipeline selects the second and third words of each line. The full command produces the following output:

http://dl.google.com/linux/chrome/deb stable/main
http://ppa.launchpad.net/ansible/ansible/ubuntu focal/main
http://security.ubuntu.com/ubuntu focal-security/main
http://security.ubuntu.com/ubuntu focal-security/multiverse
http://security.ubuntu.com/ubuntu focal-security/restricted
http://security.ubuntu.com/ubuntu focal-security/universe
http://us.archive.ubuntu.com/ubuntu focal-backports/main
http://us.archive.ubuntu.com/ubuntu focal-backports/universe
http://us.archive.ubuntu.com/ubuntu focal/main
http://us.archive.ubuntu.com/ubuntu focal/multiverse
http://us.archive.ubuntu.com/ubuntu focal/restricted
http://us.archive.ubuntu.com/ubuntu focal/universe
http://us.archive.ubuntu.com/ubuntu focal-updates/main
http://us.archive.ubuntu.com/ubuntu focal-updates/multiverse
http://us.archive.ubuntu.com/ubuntu focal-updates/restricted
http://us.archive.ubuntu.com/ubuntu focal-updates/universe

Another way to get a list of repositories is to inspect the entries that start with the characters deb in the etc/apt/sources.list files:

# sudo grep -rhE ^deb /etc/apt/sources.list*

The command produces the following output:

deb http://us.archive.ubuntu.com/ubuntu/ focal main restricted
deb http://us.archive.ubuntu.com/ubuntu/ focal-updates main restricted
deb http://us.archive.ubuntu.com/ubuntu/ focal universe
deb http://us.archive.ubuntu.com/ubuntu/ focal-updates universe
deb http://us.archive.ubuntu.com/ubuntu/ focal multiverse

Things are easier when using dnf in the Red Hat Enterprise Linux family. The dnf repolist command lists the repositories known to the local machine.

The following example shows the result of running the dnf repolist command. By default the command displays the two columns, repo id and repo name:

$ dnf repolist

The command produces the following output:

repo id                                   repo name
fedora                                    Fedora 35 - x86_64
fedora-cisco-openh264                     Fedora 35 openh264 (From Cisco) - x86_64
fedora-modular                            Fedora Modular 35 - x86_64
updates                                   Fedora 35 - x86_64 - Updates
updates-modular                           Fedora Modular 35 - x86_64 - Updates

A powerful and simple package management system

Moving from Debian or Ubuntu to Red Hat Enterprise Linux, Fedora, or CentOS Stream requires some adjustment when it comes to working at the command line, but the transition can be easy.

The patterns for installing and removing applications are surprisingly similar, yet the command line tools are different. Ubuntu/Debian uses apt. The Red Hat Enterprise Linux family uses dnf. Both command line tools support similar subcommands, which is most evident with apt install and dnf install.

The techniques for keeping track of known repositories differ between operating systems. Fortunately for those moving to the Red Hat Enterprise Linux family, listing repositories is a lot easier because it involves only the dnf repolist command. Listing repositories under Debian requires more work.

Learning the details of a new technology takes time. When transitioning from apt to dnf, you'll have to anticipate a learning curve. But the learning curve is not steep and you'll be up and running in no time.