dnf command in Linux with Examples

Introduction

Before we discuss dnf command in Linux, let's briefly understand - What is dnf command ?

dnf(Dandified Yum) is a package manager tool used in Linux operating systems, such as Fedora and CentOS. It simplifies software installation, removal, and updates.

dnf combines the functionalities of yum and adds improvements like faster dependency resolution and robust performance. With its user-friendly commands, dnf aids in managing software packages efficiently in Linux systems

In this tutorial, you will understand dnf command in Linux. We will also address a few FAQs on dnf command in Linux.

How Do I Install and Use the DNF Command on Linux?

Linux distributions such as Fedora has pre-installed the dnf command. Use dnf --version to confirm this. Use the package manager for your distribution to install it if it isn't already pre-installed: sudo [yum/apt] install dnf.

Installing dnf with apt

Using the Advanced Package Tool (APT), you can install dnf if you're running a Debian-based distribution, like Ubuntu. Here's how to do it:

sudo apt update
sudo apt install dnf

Your system will install dnf after updating its package lists as a result of executing these commands.

Installing dnf with yum

dnf is pre-installed on CentOS and other yum-based distributions. If not, though, you can install it with the yum command that follows:

sudo yum install dnf

dnf will be installed on your yum-based Linux distribution by using this command.

Installing dnf with zypper

Using the zypper package manager, you can install dnf if you're running an openSUSE distribution. Here's how to do it:

sudo zypper install dnf

Your openSUSE system will install dnf after executing this command.

Installing DNF from Source Code

Installing DNF from source code is a little more complicated, but it offers you more control over how it is installed. This is how you do it:

# Clone the DNF repository

git clone https://github.com/rpm-software-management/dnf.git

# Navigate to the cloned repository

cd dnf

# Install the necessary build dependencies

sudo dnf builddep dnf.spec

# Build and install DNF

./autogen.sh && make && sudo make install

Installing Different Versions of DNF

From Source Code

You must check out the designated version tag before building and installing the specific version of DNF that you want to install from source code. For example:

# Clone the DNF repository

git clone https://github.com/rpm-software-management/dnf.git

# Navigate to the cloned repository

cd dnf

# Checkout the specific version

git checkout [version-tag]

# Install the necessary build dependencies

sudo dnf builddep dnf.spec

# Build and install DNF

./autogen.sh && make && sudo make install

Using Package Managers

The version number of dnf can be specified in the install command if you want to install a particular version using a package manager like apt or yum. For example:

Using apt

sudo apt-get install dnf=[version-number]

Using yum

sudo yum install dnf-[version-number]

Basic dnf Usage and Verification

Using dnf

An example of using dnf to install a package is provided here:

dnf install [package-name]

Verifying dnf Installation

You can check dnf's version to make sure it's installed correctly:

dnf --version

This command verifies that dnf is installed correctly by displaying the installed version of the program.

Commonly used commands in the dnf package manager tool

Commands Description
Search This command helps us to search the package in the repository , Example: dnf search [package-name]
install This command installs the package in your computer system, Example: dnf install [package-name]
info This command returns the information about the package, Example: dnf info [package-name]
list This command is used to display the list of the packages of certain criteria, Example: dnf list installed
remove This command removes the installed package from the computer system, Example: sudo dnf remove [package-name]
upgrade This command is used to upgrade all packages, Example: sudo dnf upgrade
history This command shows the installed and removed history of the packages, Example: dnf history
repolist This command displays all the available repositories, Example: sudo dnf repolist
deplist This command shows the dependencies of the package, Example: dnf deplist [package-name]

Troubleshooting dnf Installation and Usage

Although dnf installation and use are generally simple, you may run into a few common problems or difficulties. Here are a few, along with some optimization best practices and their solutions.

Error: dnf Command Not Found

It indicates that dnf is either not installed on your computer or is not in your system's PATH if you receive an error message stating "dnf command not found". As demonstrated in the preceding sections, you can install dnf using the package manager for your distribution. The following command can be used to add dnf to your PATH if it is installed but not there already:

export PATH=$PATH:/path/to/dnf

The actual path to the dnf binary should be substituted for /path/to/dnf.

Error: Failed to Synchronize Cache for Repo

This error indicates a connection issue between dnf and the repository servers. The servers might be unavailable or there might be a problem with the network. Changing to an alternate repository or repairing your network connection are two options you can try. You can use the following command to clear the dnf cache if the error continues:

dnf clean all

Any enabled repository's cached files will be cleared by running this command.

Expanding dnf Usage to Larger Scripts and Projects

The dnf command can be an effective tool when working on bigger scripts and projects, in addition to being helpful for managing packages on a single Linux system. DNF can help you optimize your workflow, whether you're managing a complicated project with numerous dependencies or automating software installation on numerous systems.

Automating Software Installation with dnf

Here's an example of how to automate the installation of numerous packages using DNF in a shell script:

#!/bin/bash

# Define an array of packages to install
packages=(package1 package2 package3)

# Loop through the array and install each package
for package in ${packages[@]}; do
  dnf install $package -y
done

This script defines an array of packages to install, installs each package using dnf, and then loops through the array. When setting up several systems, this can save a great deal of time and effort.

Managing Project Dependencies with dnf

With multiple dependencies in your project, dnf can help make sure all necessary packages are installed. This shell script installs the package if it isn't already installed and checks to see if it is:

#!/bin/bash

# Define the package to check
package=package-name

# Check if the package is installed
if ! dnf list installed $package >/dev/null 2>&1; then
  # If not installed, install the package
  dnf install $package -y
fi

Using dnf list installed, this script first determines whether a package is installed. It installs the package using dnf if it is not already installed. Making sure the dependencies for your project are met can benefit from this.

FAQs on dnf command in Linux

Does dnf handle dependencies automatically? 

dnf manages dependencies automatically when installing or updating software, ensuring required components are included.

Can packages be downgraded using dnf? 

dnf allows users to downgrade software packages to previous versions if needed.

How can I clear dnf cache? 

You can clear the cache used by dnf to free up disk space and remove unnecessary package files.

Are there plugins available for extending dnf functionality? 

dnf supports plugins that offer additional features and capabilities for package management tasks.

Is dnf faster than the older yum package manager?

dnf is known for its improved speed and performance compared to the older yum package manager due to enhanced dependency resolution and optimizations.

How does dnf optimize package management processes on Linux compared to traditional methods? 

dnf enhances package management efficiency through faster execution speeds, streamlined dependency handling, and comprehensive repository management capabilities.

What distinguishes dnf from traditional package managers in Linux?

dnf in Linux, popular in Fedora and CentOS, introduces advanced features such as improved dependency resolution and performance boosts.

Conclusion

We hope this tutorial helped you understand how to use dnf command in Linux.

If you have any queries, please leave a comment below, and we’ll be happy to respond to them for sure.