1. Overview

The dnf command is a powerful package manager for RPM-based Linux distributions such as Fedora, CentOS, and Red Hat Enterprise Linux (RHEL). Moreover, it’s considered to be the successor to yum, offering improved performance, better dependency management, and a more user-friendly interface.

In this tutorial, we’ll walk through the basics of the dnf command and provide examples to help users and Linux administrators enhance their skills in managing packages on Linux.

2. Exploring Simple dnf Usage

First, let’s understand what is dnf. dnf stands for Dandified Yum. In addition, it enables us to install, update, and remove packages on RPM-based distributions. Furthermore, the dnf command resolves dependencies and installs the required packages alongside the main package.

2.1. Installing Packages Using dnf

To install a package using dnf, we use the dnf install command followed by the package name. Let’s explore an example:

$ sudo dnf install htop
Last metadata expiration check: 0:05:23 ago on Wed May 22 10:45:23 2024.
Dependencies resolved.
================================================================================
 Package          Arch             Version                  Repository     Size
================================================================================
Installing:
 htop             x86_64           2.2.0-6.el8              baseos         103 k
Transaction Summary
================================================================================
Install  1 Package
Total download size: 103 k
Installed size: 297 k
Is this ok [y/N]: y
Downloading Packages:
htop-2.2.0-6.el8.x86_64.rpm                         112 kB/s | 103 kB     00:00
--------------------------------------------------------------------------------
Total                                              112 kB/s | 103 kB     00:00
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                        1/1
  Installing       : htop-2.2.0-6.el8.x86_64                                 1/1
  Verifying        : htop-2.2.0-6.el8.x86_64                                 1/1
Installed:
  htop-2.2.0-6.el8.x86_64
Complete!

Next, let’s break down the command above:

  • sudo enables us to execute the command with superuser privileges
  • dnf install tells dnf to install the specified package
  • htop is the name of the package we want to install

According to the output, we can see useful information regarding the referenced package, such as package version, download size, repository, and CPU architecture.

2.2. Updating Packages

In some scenarios, it’s mandatory to update certain packages. Even if an update isn’t mandatory, updating our installed packages to the latest released patches is a best practice.

There are a few reasons why we may want to update a package in Linux, such as:

  • maintaining system security
  • stability
  • performance

Regular updates ensure that the latest security patches are applied, protecting the system from vulnerabilities and potential exploits. Additionally, updates often include performance improvements and bug fixes, enhancing the overall efficiency and reliability of the software.

By keeping packages current, users can take advantage of new features and improvements, ensuring compatibility with other software and systems. Particularly, this practice not only extends the lifespan of the Linux environment but also contributes to a smoother and more secure computing experience.

Let’s have a look at how to update all packages using dnf:

$ sudo dnf update

Next, let’s see an example of how to upgrade a certain package using dnf:

$ sudo dnf update htop
Last metadata expiration check: 0:05:43 ago on Wed May 22 10:45:23 2024.
Dependencies resolved.
================================================================================
 Package          Arch             Version                  Repository     Size
================================================================================
Upgrading:
 htop             x86_64           2.2.0-7.el8              updates        104 k
Transaction Summary
================================================================================
Upgrade  1 Package
Total download size: 104 k
Is this ok [y/N]: y
Downloading Packages:
htop-2.2.0-7.el8.x86_64.rpm                         114 kB/s | 104 kB     00:00
--------------------------------------------------------------------------------
Total                                              114 kB/s | 104 kB     00:00
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                        1/1
  Upgrading        : htop-2.2.0-7.el8.x86_64                                 1/2
  Cleanup          : htop-2.2.0-6.el8.x86_64                                 2/2
  Verifying        : htop-2.2.0-7.el8.x86_64                                 1/2
  Verifying        : htop-2.2.0-6.el8.x86_64                                 2/2
Upgraded:
  htop-2.2.0-7.el8.x86_64
Complete!

This snippet shows the upgrade of htop from version 2.2.0-6.e18.x86_64 to the latest version, which at the time of writing is 2.2.0-7.e18.x86_64.

2.3. Removing Packages

Since we can install or update a package, we can certainly remove a package that we no longer need. In essence, to remove an installed package, we use the dnf remove command followed by the package name:

$ sudo dnf remove htop
Dependencies resolved.
================================================================================
 Package          Arch             Version                  Repository     Size
================================================================================
Removing:
 htop             x86_64           2.2.0-7.el8              @updates       297 k
Transaction Summary
================================================================================
Remove  1 Package
Freed space: 297 k
Is this ok [y/N]: y
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                        1/1
  Erasing          : htop-2.2.0-7.el8.x86_64                                 1/1
  Verifying        : htop-2.2.0-7.el8.x86_64                                 1/1
Removed:
  htop-2.2.0-7.el8.x86_64
Complete!

From the output, we confirm the removal of the previously updated htop package.

2.4. Listing and Searching for Packages

The search option of the dnf command comes in handy when we’re unsure of the exact name of the package we want to install.

Let’s understand how we can search for packages related to htop using the dnf search command:

$ dnf search htop
Last metadata expiration check: 0:06:12 ago on Wed May 22 10:45:23 2024.
========================== Name & Summary Matched: htop ==========================
htop.x86_64 : Interactive process viewer

Moreover, we can utilize the list command to check for the already installed packages on our system:

$ sudo dnf list --installed
Last metadata expiration check: 0:06:32 ago on Wed May 22 10:45:23 2024.
Installed Packages
bash.x86_64               5.0.17-2.el8          @anaconda
coreutils.x86_64          8.30-8.el8            @anaconda
htop.x86_64               2.2.0-7.el8           @updates

As we can see, the command lists all the packages already installed with their current versions.

Finally, if we’re interested in getting detailed information for a certain package, we’ll use the info command:

$ dnf info htop
Last metadata expiration check: 0:06:52 ago on Wed May 22 10:45:23 2024.
Installed Packages
Name         : htop
Version      : 2.2.0
Release      : 7.el8
Architecture : x86_64
Size         : 297 k
Source       : htop-2.2.0-7.el8.src.rpm
Repository   : @System
From repo    : updates
Summary      : Interactive process viewer
URL          : http://hisham.hm/htop/
License      : GPLv2+
Description  : htop is an interactive text-mode process viewer for Unix systems. It aims to be a better 'top' and requires ncurses.

This command provides basic information about the package, including name, version, release, and description.

3. Advanced dnf Usage

Now that we’ve covered the basic and simple use cases of dnf, let’s explore more advanced options.

3.1. Enabling and Disabling Repositories

Repositories are sources from which dnf can download packages. In addition, We can enable or disable repositories temporarily or permanently.

Let’s check how to do so with a real-life example:

$ sudo dnf --enablerepo=epel install htop
$ sudo dnf --disablerepo=epel install htop

In the first command, we enable the EPEL repository and then use dnf to install the htop package from that repository.

3.2. Cleaning Up with dnf

Over time, the package metadata or cache can become outdated. Accordingly, we can clean up the cache using the dnf clean command:

$ sudo dnf clean all
$ sudo dnf clean metadata
29 files removed

The first line is a more comprehensive cleaning command. It removes all temporary files downloaded by dnf for package management, including:

  • package cache
  • metadata cache
  • temporary data

On the other hand, the second command clears only the metadata cache. Precisely, it removes the cached information about packages, versions, and dependencies downloaded from repositories.

3.3. Handling Downgrades

Similar to upgrades, we have the option to downgrade certain packages. Although it’s not very common to downgrade a package, it’s sometimes required for various reasons.

Here, let’s use the dnf command to downgrade a specific package:

$ sudo dnf downgrade htop
Last metadata expiration check: 0:07:32 ago on Wed May 22 10:45:23 2024.
Dependencies resolved.
================================================================================
 Package          Arch             Version                  Repository     Size
================================================================================
Downgrading:
 htop             x86_64           2.2.0-6.el8              baseos         103 
...

Now, we notice that the htop package version is back to 2.2.0-6.e18.

3.4. Unneeded Packages

When we uninstall a package, its dependencies might not be removed automatically. The dnf autoremove command removes packages that were installed as dependencies but are no longer needed by any installed programs:

$ sudo dnf autoremove
Dependencies resolved.
================================================================================
 Package              Arch         Version                  Repository     Size
================================================================================
Removing:
 libtool-ltdl         x86_64       2.4.6-25.el8             @baseos        52 k
...
Transaction Summary
================================================================================
Remove  2 Packages
Freed space: 104 k
Is this ok [y/N]: y
...
Complete!

On the contrary, sometimes, we may want to exclude certain packages from being installed, updated, or removed. Accordingly, we can use the –exclude option to achieve this:

$ sudo dnf upgrade --exclude=htop
Last metadata expiration check: 0:09:32 ago on Wed May 22 10:45:23 2024.
Dependencies resolved.
Nothing to do.
Complete!

Consequently, this upgrades all packages except for htop.

3.5. Checking Possible Updates

In certain cases, we might need to check for possible updates, even though we might not have a solid idea of which packages require an update. For that, we can check all possible updates with the check-update command:

$ sudo dnf check-update
Last metadata expiration check: 0:07:52 ago on Wed May 22 10:45:23 2024.
Updated Packages
htop.x86_64             2.2.0-8.el8                 updates             104 k

The output resembles a possible update of the htop package to 2.2.0-8.e18 version.

4. Conclusion

In this article, we understood that the dnf command is an important tool for managing software packages on RPM-based Linux distributions. We provided an overview of the most commonly used dnf commands along with examples to help users get started and enhance their ability to manage Linux systems effectively.

By understanding and utilizing the various dnf commands, we can efficiently install, update, remove, and manage software on our system.

Subscribe
Notify of
guest
0 Comments
Inline Feedbacks
View all comments