Managing software packages is a fundamental skill for anyone working with Linux systems, especially on distributions like CentOS and almalinux. These systems rely on the DNF (Dandified YUM) package manager, a powerful and user-friendly tool that streamlines the process of installing, updating, and removing software. By the end of this guide, you’ll be adept at using various dnf commands to maintain a healthy and up-to-date system, ensuring your servers and workstations run smoothly and securely.
Prerequisites
To follow along with this guide, you’ll need:
- A running CentOS or AlmaLinux system.
- Access to a terminal or command line interface.
- Root privileges or a user with
sudoaccess. Mostdnfcommands require elevated permissions to modify the system.
Step 1: Understanding DNF Basics
DNF is the next-generation version of YUM (Yellowdog Updater, Modified) and is the default package manager for RHEL-based distributions, including CentOS Stream 8+, AlmaLinux, Rocky Linux, and Fedora. It offers improved performance, better dependency resolution, and a more robust API compared to its predecessor. All dnf commands typically follow the structure: sudo dnf [options] command [package_name].
Pro-Tip: Using sudo
Always prefix your dnf commands with sudo unless you are logged in directly as the root user. This ensures you have the necessary permissions to make system-wide changes. If you forget sudo, DNF will likely prompt you with a permission denied error.
Step 2: Updating Your System
Regularly updating your system is crucial for security and performance, patching vulnerabilities, and providing the latest features. It’s often the first command you run on a new system.
Update All Packages
sudo dnf update
This command downloads and installs all available updates for your currently installed packages. It also updates the DNF metadata cache.
Upgrade All Packages (including obsoleted ones)
sudo dnf upgrade
While often used interchangeably with update, upgrade is a more comprehensive command that handles obsoleted packages (packages that have been replaced by newer versions with different names). For most daily use, update is sufficient, but upgrade ensures a cleaner system over time.
Warning: Kernel Updates and Reboots
If a kernel package is updated, your system will continue to run on the old kernel until a reboot. It’s a good practice to reboot your system after a kernel update to ensure you’re running on the latest, most secure kernel version.
Step 3: Installing New Packages
The most common task after updating is installing new software. DNF makes this straightforward.
Install a Single Package
sudo dnf install <package_name>
Example: Let’s install htop, an interactive process viewer.
sudo dnf install htop
DNF will show you a summary of the package(s) to be installed, including any dependencies, and ask for confirmation. Type y and press Enter to proceed.
Pro-Tip: Installing Multiple Packages
You can install several packages at once by listing them space-separated:
sudo dnf install nginx php-fpm mariadb-server
Step 4: Removing Packages
When software is no longer needed, you should remove it to free up disk space and reduce potential security risks.
Remove a Specific Package
sudo dnf remove <package_name>
Example: To remove htop:
sudo dnf remove htop
DNF will list the package and any dependent packages that will also be removed. Confirm with y.
Remove Orphaned Dependencies
sudo dnf autoremove
This command removes packages that were installed as dependencies of other packages but are no longer required by any currently installed software. It’s a great way to keep your system lean.
Warning: Dependency Impact
Be cautious when removing packages. DNF will inform you if other packages depend on the one you’re trying to remove. Removing a critical dependency can break other applications or even your system.
Step 5: Searching for Packages
Sometimes you don’t know the exact name of a package, or you want to find software related to a specific function.
Search by Keyword
dnf search <keyword>
Example: To find web server related packages:
dnf search web server
This will return a list of packages whose names or descriptions contain the specified keyword.
List Installed or Available Packages
dnf list installed
dnf list available
dnf list <package_name_pattern>
You can also use wildcards. For instance, to list all installed PHP packages:
dnf list installed php*
Step 6: Viewing Package Information
Before installing or removing a package, you might want to learn more about it.
Display Detailed Package Information
dnf info <package_name>
Example: Get information about the Nginx web server:
dnf info nginx
This provides details like version, size, description, available repositories, and dependencies.
Find Which Package Owns a File
dnf provides <file_path>
Use Case: If you find a file like /usr/bin/git and want to know which package installed it:
dnf provides /usr/bin/git
Step 7: Managing DNF Repositories
Repositories are locations where DNF fetches packages. CentOS and AlmaLinux come with official repositories enabled, but you might need to add others for specific software.
List Enabled Repositories
dnf repolist
This command shows all currently enabled repositories, their IDs, names, and status.
Pro-Tip: Adding EPEL Repository
The Extra Packages for Enterprise Linux (EPEL) repository is a popular third-party repository that provides high-quality additional packages. To install it:
sudo dnf install epel-release
After installation, run sudo dnf repolist again to see the new EPEL repository.
Step 8: Cleaning DNF Cache
DNF downloads package metadata and actual package files into a local cache. Over time, this cache can grow quite large. Cleaning it can free up disk space.
Clean All DNF Cache
sudo dnf clean all
This command removes all cached repository metadata and package files. It’s useful for troubleshooting repository issues or simply reclaiming disk space.
You’ve now covered the most essential dnf commands for managing packages on your CentOS or AlmaLinux system. Continue exploring DNF by looking into its group management features (dnf group list, dnf group install) or reviewing transaction history (dnf history) to undo or redo operations.
