How to Use dnf Command: Basic Syntax and Examples
The
dnf command is a package manager used in Fedora and related Linux distributions to install, update, and remove software packages. You use it with commands like dnf install, dnf update, and dnf remove followed by the package name.Syntax
The basic syntax of the dnf command is:
dnf [options] command [package_name]
Here:
commandis the action you want to perform, likeinstall,update, orremove.package_nameis the name of the software package you want to manage.optionsare extra flags to modify behavior, like-yto auto-confirm prompts.
bash
dnf [options] command [package_name]
Example
This example shows how to install the vim text editor using dnf. It also shows how to update all packages and remove a package.
bash
sudo dnf install vim -y sudo dnf update -y sudo dnf remove vim -y
Output
Last metadata expiration check: 0:00:10 ago on Thu 01 Jan 2024 12:00:00 PM UTC.
Dependencies resolved.
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
vim x86_64 8.2.3456-1.fc36 fedora 3.5 M
Transaction Summary
================================================================================
Install 1 Package
Total download size: 3.5 M
Installed size: 12 M
Downloading Packages:
vim-8.2.3456-1.fc36.x86_64.rpm 3.5 MB/s | 3.5 MB 00:01
--------------------------------------------------------------------------------
Total 3.5 MB/s | 3.5 MB 00:01
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
Preparing : 1/1
Installing : vim-8.2.3456-1.fc36.x86_64 1/1
Running scriptlet: vim-8.2.3456-1.fc36.x86_64 1/1
Verifying : vim-8.2.3456-1.fc36.x86_64 1/1
Installed:
vim.x86_64 8.2.3456-1.fc36
Complete!
Last metadata expiration check: 0:00:10 ago on Thu 01 Jan 2024 12:00:00 PM UTC.
Dependencies resolved.
Nothing to do.
Complete!
Last metadata expiration check: 0:00:10 ago on Thu 01 Jan 2024 12:00:00 PM UTC.
Dependencies resolved.
================================================================================
Package Arch Version Repository Size
================================================================================
Removing:
vim x86_64 8.2.3456-1.fc36 @fedora 12 M
Transaction Summary
================================================================================
Remove 1 Package
Installed size: 12 M
Removing:
vim.x86_64 8.2.3456-1.fc36
Complete!
Common Pitfalls
Some common mistakes when using dnf include:
- Not using
sudofor commands that require admin rights, causing permission errors. - Forgetting the
-yoption, which makesdnfwait for confirmation and pause scripts. - Trying to install a package that does not exist or is misspelled, resulting in errors.
Always check package names and use dnf search <keyword> to find correct names.
bash
dnf install vim sudo dnf install vim -y
Output
Error: You need to be root to perform this command.
or
Is this ok [y/N]: y
Installing:
vim
Complete!
Quick Reference
| Command | Description |
|---|---|
| dnf install | Install a package |
| dnf update | Update all packages |
| dnf remove | Remove a package |
| dnf search | Search for packages |
| dnf info | Show package details |
| dnf clean all | Clear cache and metadata |
Key Takeaways
Use
dnf with sudo to manage packages on Fedora-based Linux.Common commands include
install, update, and remove.Add
-y to auto-confirm prompts and avoid pauses.Use
dnf search to find correct package names before installing.Check for permission errors by ensuring you run commands as root or with
sudo.