Sign In
Sign In

How to Use DNF to Manage Packages on CentOS

How to Use DNF to Manage Packages on CentOS
Adnene Mabrouk
Technical writer
Linux
18.10.2024
Reading time: 4 min

DNF (Dandified Yum) is the next-generation version of Yum, the default package manager for CentOS and Fedora distributions. It is designed to resolve dependencies more efficiently, handle larger package sets, and improve performance over its predecessor. DNF simplifies the management of software packages by allowing users to install, update, and remove packages from the command line with a user-friendly interface.

Installing and Removing Packages with DNF

One of the primary functions of DNF is installing and removing software packages. To install a package using DNF, you need root or sudo privileges. The syntax is straightforward:

sudo dnf install package_name

For example, to install the Apache HTTP server:

sudo dnf install httpd

To remove a package, the command is similar:

sudo dnf remove package_name

For example, to remove Apache HTTP server:

sudo dnf remove httpd

Updating and Upgrading Packages

Keeping your system up to date is essential for security and performance. DNF makes this process simple. To update all the packages on your system, use:

sudo dnf update

This command will update installed packages to the latest versions available in the configured repositories. If you want to upgrade your entire system to the latest release (such as when moving between CentOS versions), you can use:

sudo dnf upgrade

The difference between update and upgrade is that the latter will also remove obsolete packages, whereas update does not.

Searching for Packages in DNF

DNF allows users to search for packages before installing them. This is helpful if you're unsure of the exact package name or want to explore available options. To search for a package:

sudo dnf search <keyword>

For example, to search for packages related to Apache:

sudo dnf search apache

DNF will list all packages that match the search term, along with a brief description.

Managing Repositories with DNF

Repositories are essential for managing where DNF pulls its packages from. DNF automatically handles repository configuration files, usually located in /etc/yum.repos.d/. You can add, enable, or disable repositories with DNF.

To add a new repository, you need to create a .repo file in /etc/yum.repos.d/. For example, let's say you want to add the EPEL (Extra Packages for Enterprise Linux) repository, which provides additional packages not available in the default CentOS repositories.

  • Install the EPEL repository using DNF

EPEL is available as a package that can be installed directly:

sudo dnf install epel-release

This command automatically creates the necessary .repo file and enables the EPEL repository.

  • Manually adding a repository

If you want to manually add a repository, you would create a .repo file, for instance, myrepo.repo, in /etc/yum.repos.d/, and add the following content:

[myrepo]
name=My Custom Repo
baseurl=http://example.com/repo/centos/$releasever/$basearch/
enabled=1
gpgcheck=1
gpgkey=http://example.com/repo/RPM-GPG-KEY-myrepo

Here:

  • name specifies the name of the repository.

  • baseurl defines the URL from where the packages will be downloaded.

  • enabled=1 ensures the repository is active.

  • gpgcheck=1 enables GPG key checking for security.

  • gpgkey provides the URL to the GPG key used to verify the packages.

  • To disable the epel repository:
sudo dnf config-manager --set-enabled epel
  • To enable it again:
sudo dnf config-manager --set-enabled epel

Cleaning Up Unused Packages

Over time, your system may accumulate unnecessary packages and cache files, which take up valuable space. DNF includes a built-in command to clean up unused packages and free up disk space:

sudo dnf autoremove

This will remove any orphaned packages that are no longer required by the system. Additionally, you can clean up cached data using:

sudo dnf clean all

This command clears all cached package files stored in /var/cache/dnf/.

Troubleshooting DNF Issues

Occasionally, you may encounter issues when managing packages with DNF. Common problems include broken dependencies or repository errors. Here are some troubleshooting tips:

  1. Broken dependencies: If you're facing dependency issues, try running:

sudo dnf install --best --allowerasing

This command attempts to resolve conflicts by allowing DNF to erase conflicting packages.

  1. Corrupted cache: If the cache becomes corrupted, clean it up using:

sudo dnf clean metadata
  1. Failed transactions: If a DNF transaction fails, try rebuilding the database:

sudo rpm --rebuilddb

By using these tips, you can quickly resolve most issues you might face with DNF.

Conclusion

DNF is a powerful and efficient package manager that makes software management on CentOS easy. From installing and updating packages to managing repositories and cleaning up unused files, DNF provides a wide range of features to ensure your system runs smoothly. With this guide, you should be well-equipped to handle package management tasks on your CentOS system.

Linux
18.10.2024
Reading time: 4 min

Do you have questions,
comments, or concerns?

Our professionals are available to assist you at any moment,
whether you need help or are just unsure of where to start
Email us