How to Use DNF Command in Linux

Linux provides various package managers to help users manage software installations, updates, and removals. One of the popular package managers used in many Linux distributions is DNF (Dandified Yum). DNF is a powerful command-line tool that simplifies package management tasks and provides enhanced features compared to its predecessor, Yum. This article will explore how to use the DNF command in Linux effectively.

Updating the DNF Package Cache

Before embarking on any package-related tasks with DNF, ensuring that your package cache is up to date is imperative. The package cache acts as a repository of valuable information regarding available software packages and their corresponding dependencies. To ensure your package cache is current, execute the following command:

sudo dnf update

Searching for Packages

DNF simplifies the process of locating packages based on their name or description. By leveraging DNF’s search functionality, you can swiftly find the software you require. To conduct a package search, utilize the following command:

sudo dnf search <package_name>

Replace <package_name> with the name or relevant keywords associated with the desired package. DNF will provide you with a concise list of packages matching your search criteria.

Installing Packages

Installing packages using DNF is a streamlined process. Merely execute the following command:

sudo dnf install <package_name>

Ensure that you substitute <package_name> with the specific name of the package you wish to install. DNF seamlessly handles all necessary dependencies, guaranteeing the smooth installation of the requisite software components on your system.

Removing Packages

When a package is no longer needed, DNF facilitates its removal with remarkable ease. To eliminate a package, utilize the following command:

sudo dnf remove <package_name>

Replace <package_name> with the name of the package you intend to remove. DNF automatically manages the removal of any dependencies that are no longer essential.

Updating Packages

Maintaining up-to-date packages is crucial for ensuring optimal performance and security. DNF simplifies the process of updating your installed packages with the following command:

sudo dnf upgrade

Executing this command prompts DNF to check for any newer versions of the installed packages and upgrades them accordingly.

Managing Repositories

DNF empowers users to manage repositories effectively. Repositories serve as collections of software packages, and you can enable or disable them, add new repositories, or remove existing ones. To enable a repository, execute the following command:

sudo dnf config-manager --set-enabled <repository_name>

Substitute <repository_name> with the name of the specific repository you wish to enable. Conversely, you can employ the --set-disabled option to disable a repository.

Cleaning the Package Cache

Over time, the package cache can consume a substantial amount of disk space. To mitigate this issue and eliminate any extraneous or obsolete cached packages, execute the following command:

sudo dnf clean all

DNF will diligently clean the package cache, reclaiming valuable disk space and optimizing your system’s performance.

Verifying Installed Packages

DNF offers a valuable command for verifying the integrity of installed packages. By executing the following command, you can ascertain the integrity and authenticity of the installed packages:

sudo dnf verify

DNF meticulously compares the installed files against their respective package metadata and promptly reports any discrepancies or inconsistencies, ensuring the reliability of your software packages.

Viewing Package Information

DNF provides a comprehensive command to display detailed information about a specific package, including its description, version, size, and more. Execute the following command to access this valuable information:

sudo dnf info <package_name>

Replace <package_name> with the name of the package you wish to explore. DNF will present you with an extensive overview of the package’s attributes and characteristics.

DNF Command Examples

Here are additional command examples to further expand your understanding of DNF and its capabilities:

Upgrade all installed packages and refresh repository metadata

To ensure you have the latest versions of all installed packages and updated repository information, use the following command:

sudo dnf upgrade --refresh

This command simultaneously upgrades all installed packages on your system and refreshes the metadata of the repositories, providing you with the most recent package versions and availability.

List available updates for installed packages

To view a list of updates available for the packages installed on your system, execute the following command:

sudo dnf list updates

This command retrieves information about any available updates for the installed packages, enabling you to keep your system updated with the latest versions and security patches.

Example output with DNF Listed Updates on Fedora Linux:

DNF List available updates for installed packages

Downgrade a package to a specific version

If you encounter compatibility issues or prefer to use a specific version of a package, you can easily downgrade it using the following command:

sudo dnf downgrade <package_name>

Replace <package_name> with the name of the package you want to downgrade. DNF will search for the specified package and install the requested version, replacing the current one if necessary.

Download a package without installing it

Sometimes, you may want to download a package without immediately installing it. This can be useful for offline installations or when you want to transfer the package to another system. To achieve this, use the following command:

sudo dnf download <package_name>

Replace <package_name> with the name of the package you wish to download. DNF will download the package and store it in the current directory, allowing you to install it at a later time.

Check if a specific package is installed

To verify whether a particular package is already installed on your system, utilize the following command:

sudo dnf list installed <package_name>

Replace <package_name> with the name of the package you want to check. DNF will search the list of installed packages and provide relevant information about the package if it is found.

Reinstall a package without removing its configuration files

If you encounter issues with a package or suspect that its files may have become corrupted, you can reinstall it without removing its configuration files. Execute the following command:

sudo dnf reinstall <package_name>

Replace <package_name> with the name of the package you wish to reinstall. DNF will reinstall the package while preserving any existing configuration files.

Display the size of installed packages

To obtain a comprehensive overview of the sizes of the installed packages on your system, use the following command:

sudo dnf repoquery --installed --queryformat '%{size} %{name}\n' | sort -nr

Executing this command prompts DNF to retrieve the sizes of the installed packages and present them in descending order. This information can be useful for identifying large packages that consume significant disk space.

Show the transaction history of package installations, updates, and removals

To access the transaction history of package installations, updates, and removals performed using DNF, execute the following command:

sudo dnf history

This command presents a chronological record of all the DNF transactions, enabling you to review the actions taken on your system, such as package installations, updates, and removals.

Conclusion

In this tutorial, we have covered the fundamentals of using the DNF command in Linux. You learned how to update the package cache, search for and install packages, remove unwanted software, upgrade packages, manage repositories, resolve dependencies, clean the package cache, verify installed packages, view package information, and configure DNF. By mastering these essential DNF commands, you can efficiently manage software packages on your Linux system.

Now that you understand the DNF command better, you can explore and leverage its capabilities to streamline your package management tasks.

Your Mastodon Instance
Share to...