Swiftorial Logo
Home
Swift Lessons
Matchups
CodeSnaps
Tutorials
Career
Resources

Using dnf - A Comprehensive Tutorial

Introduction

DNF, short for Dandified YUM, is a package manager for RPM-based distributions such as Fedora, CentOS, and RHEL. It is the next-generation version of the Yellowdog Updater, Modified (YUM). This tutorial will guide you through the basics of using DNF, from installing packages to managing repositories.

Installing Packages

One of the primary functions of DNF is to install packages. To install a package, use the dnf install command followed by the package name.

Example:

sudo dnf install package_name

For instance, to install the 'nano' text editor:

sudo dnf install nano

Last metadata expiration check: 0:24:44 ago on Sun 17 Oct 2021 12:00:00 PM UTC.
Dependencies resolved.
=======================================================================
 Package         Arch           Version              Repository   Size
=======================================================================
Installing:
 nano            x86_64         2.9.8-1.fc28         updates     542 k

Transaction Summary
=======================================================================
Install  1 Package

Total download size: 542 k
Installed size: 1.8 M
Is this ok [y/N]:
                

Updating Packages

To update a specific package, you can use the dnf update command followed by the package name. If you want to update all installed packages to their latest versions, simply use dnf update without any arguments.

Example:

sudo dnf update package_name

To update all packages:

sudo dnf update

Removing Packages

If you need to remove an installed package, use the dnf remove command followed by the package name.

Example:

sudo dnf remove package_name

For example, to remove the 'nano' text editor:

sudo dnf remove nano

Searching for Packages

To search for a specific package or a keyword within the package descriptions, use the dnf search command followed by the keyword.

Example:

dnf search keyword

For instance, to search for packages related to 'editor':

dnf search editor

Listing Installed Packages

DNF allows you to list all installed packages on your system using the dnf list installed command.

Example:

dnf list installed

Managing Repositories

Repositories are collections of packages. DNF uses repository files located in the /etc/yum.repos.d/ directory. You can add, enable, or disable repositories as needed.

Adding a Repository

To add a repository, you can create a new file with a .repo extension in the /etc/yum.repos.d/ directory.

Example:

sudo nano /etc/yum.repos.d/newrepo.repo

Add the following content:

[newrepo]
name=New Repository
baseurl=http://example.com/repo
enabled=1
gpgcheck=1
gpgkey=http://example.com/repo/RPM-GPG-KEY
                

Enabling/Disabling a Repository

To enable a repository, use:

sudo dnf config-manager --set-enabled repo_name

To disable a repository, use:

sudo dnf config-manager --set-disabled repo_name

Cleaning Up

Over time, the cache can grow and take up a lot of space. You can clean up the cache using the dnf clean command.

Cleaning Metadata

To clean the metadata cache:

sudo dnf clean metadata

Cleaning All Cached Files

To clean all cached files:

sudo dnf clean all

Conclusion

This tutorial covered the basic usage of DNF, including installing, updating, and removing packages, searching for packages, listing installed packages, managing repositories, and cleaning up the cache. With these commands, you should be able to effectively manage software on your RPM-based Linux distribution.