Linux dnf Command with Practical Examples

LinuxLinuxBeginner
Practice Now

Introduction

In this lab, you will learn how to use the dnf (Dandified YUM) command, the default package manager for modern Red Hat-based Linux distributions like Fedora, CentOS, and RHEL. You will understand the basic usage of the dnf command, install and update packages, and manage package groups and dependencies. The lab covers the essential package management tasks, providing practical examples to help you become proficient in managing software packages on your Linux system.

Linux Commands Cheat Sheet


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("`Linux`")) -.-> linux/BasicSystemCommandsGroup(["`Basic System Commands`"]) linux/BasicSystemCommandsGroup -.-> linux/help("`Command Assistance`") linux/BasicSystemCommandsGroup -.-> linux/man("`Manual Access`") subgraph Lab Skills linux/help -.-> lab-422646{{"`Linux dnf Command with Practical Examples`"}} linux/man -.-> lab-422646{{"`Linux dnf Command with Practical Examples`"}} end

Understand the dnf Command

In this step, you will learn about the dnf (Dandified YUM) command, which is the default package manager for modern Red Hat-based Linux distributions like Fedora, CentOS, and RHEL.

First, let's check the current version of dnf installed on the system:

sudo dnf --version

Example output:

dnf version 4.9.0

The dnf command is used for various package management tasks, such as:

  • Installing new packages
  • Updating existing packages
  • Removing packages
  • Searching for packages
  • Listing installed packages
  • Managing package groups and dependencies

To get a quick overview of the available dnf commands and options, you can use the built-in help:

sudo dnf help

This will display a list of the most common dnf commands and a brief description of each.

You can also get more detailed information about a specific dnf command by using the man command:

man dnf

This will open the manual page for the dnf command, providing comprehensive documentation on its usage and available options.

Install and Update Packages using dnf

In this step, you will learn how to use the dnf command to install new packages and update existing packages on your system.

First, let's search for a package using the dnf search command. For example, to search for the "tree" package:

sudo dnf search tree

Example output:

Last metadata expiration check: 0:00:36 ago on Fri 14 Apr 2023 05:33:00 PM UTC.
tree.x86_64 : Display a tree-like view of the directory structure
tree-python3.x86_64 : Python3 bindings for tree
tree-qt.x86_64 : Qt-based tree viewer

To install the "tree" package, use the dnf install command:

sudo dnf install -y tree

Example output:

Dependencies resolved.
...
Installed:
  tree-1.8.0-10.el8.x86_64

Now, let's update all the installed packages on the system using the dnf update command:

sudo dnf update -y

Example output:

Dependencies resolved.
...
Updated:
  ...
Complete!

The -y flag in the above commands automatically answers "yes" to any prompts, making the installation and update process unattended.

Manage Package Groups and Dependencies with dnf

In this step, you will learn how to manage package groups and dependencies using the dnf command.

Package groups in dnf are collections of related packages that can be installed or removed together. To list all available package groups, use the dnf group list command:

sudo dnf group list

Example output:

Available Environment Groups:
   ...
   Server with GUI
   Minimal Install
   ...
Available Groups:
   Authoring and Publishing
   C Development Tools and Libraries
   ...

To install a package group, use the dnf group install command. For example, to install the "Development Tools" group:

sudo dnf group install -y "Development Tools"

Example output:

Dependencies resolved.
...
Installed:
  ...

When installing a package, dnf will also handle the package dependencies automatically. You can see the list of dependencies that will be installed by using the dnf deplist command:

sudo dnf deplist tree

Example output:

package: tree-1.8.0-10.el8.x86_64
dependency: libc.so.6()(64bit)
dependency: libm.so.6()(64bit)
dependency: libncurses.so.6()(64bit)
dependency: libpthread.so.0()(64bit)
dependency: rtld(GNU_HASH)

This shows the dependencies required by the "tree" package.

To remove a package group, use the dnf group remove command:

sudo dnf group remove -y "Development Tools"

Example output:

Dependencies resolved.
...
Removed:
  ...

Summary

In this lab, you learned about the dnf (Dandified YUM) command, which is the default package manager for modern Red Hat-based Linux distributions. You explored how to use dnf to install new packages, update existing packages, search for packages, and manage package groups and dependencies. Specifically, you learned how to check the current version of dnf, search for packages, install the "tree" package, and update all installed packages on the system.

Linux Commands Cheat Sheet

Other Linux Tutorials you may like