How to install on RHEL/CentOS/Alma/Rocky → dnf install (or yum)
How to Install Packages on RHEL/CentOS/Alma/Rocky Linux Using dnf and yum
Table of Contents
1. [Introduction](#introduction)
2. [Prerequisites](#prerequisites)
3. [Understanding Package Managers](#understanding-package-managers)
4. [Basic Installation Commands](#basic-installation-commands)
5. [Advanced Installation Options](#advanced-installation-options)
6. [Practical Examples](#practical-examples)
7. [Repository Management](#repository-management)
8. [Troubleshooting Common Issues](#troubleshooting-common-issues)
9. [Best Practices](#best-practices)
10. [Security Considerations](#security-considerations)
11. [Conclusion](#conclusion)
Introduction
Installing software packages on Red Hat Enterprise Linux (RHEL) and its derivatives—CentOS, AlmaLinux, and Rocky Linux—is a fundamental skill for system administrators and developers. This comprehensive guide will walk you through the process of using both `dnf` (Dandified YUM) and `yum` (Yellowdog Updater Modified) package managers to install, manage, and troubleshoot software packages effectively.
Whether you're a beginner setting up your first RHEL-based system or an experienced administrator looking to refine your package management skills, this guide covers everything from basic installation commands to advanced repository management and troubleshooting techniques.
Prerequisites
Before diving into package installation, ensure you have:
System Requirements
- A running RHEL, CentOS, AlmaLinux, or Rocky Linux system
- Root access or sudo privileges
- Active internet connection for downloading packages
- Basic familiarity with command-line interface
Version Compatibility
- dnf: Default on RHEL 8+, CentOS 8+, AlmaLinux 8+, Rocky Linux 8+
- yum: Default on RHEL 7 and earlier, CentOS 7 and earlier
- Both tools are often available simultaneously for backward compatibility
Checking Your System
First, identify your distribution and version:
```bash
Check distribution and version
cat /etc/os-release
Check which package manager is available
which dnf
which yum
Check package manager version
dnf --version
yum --version
```
Understanding Package Managers
DNF vs YUM: Key Differences
| Feature | DNF | YUM |
|---------|-----|-----|
| Performance | Faster dependency resolution | Slower with complex dependencies |
| Memory Usage | Lower memory footprint | Higher memory consumption |
| API | Improved Python API | Legacy Python API |
| Default Version | RHEL 8+ | RHEL 7 and earlier |
| Dependency Solving | Advanced SAT solver | Traditional resolver |
Package Management Concepts
Packages: Pre-compiled software bundles containing executable files, configuration files, and metadata.
Repositories: Collections of packages stored on servers, configured locally through repository files.
Dependencies: Other packages required for a package to function correctly.
Metadata: Information about packages including versions, dependencies, and descriptions.
Basic Installation Commands
Installing Single Packages
The most fundamental operation is installing a single package:
```bash
Using dnf (RHEL 8+)
sudo dnf install package-name
Using yum (RHEL 7 and earlier)
sudo yum install package-name
Example: Installing vim text editor
sudo dnf install vim
```
Installing Multiple Packages
Install several packages simultaneously:
```bash
Install multiple packages
sudo dnf install vim git curl wget
Alternative syntax
sudo dnf install vim git curl wget -y
```
Installing Specific Package Versions
Sometimes you need a specific version:
```bash
List available versions
dnf list --showduplicates package-name
Install specific version
sudo dnf install package-name-version
Example: Installing specific nginx version
sudo dnf install nginx-1.20.1-1.el8
```
Installing from Different Sources
Installing RPM Files Directly
```bash
Install local RPM file
sudo dnf install /path/to/package.rpm
Install RPM from URL
sudo dnf install https://example.com/package.rpm
Using rpm command (lower level)
sudo rpm -ivh package.rpm
```
Installing Package Groups
Package groups bundle related software:
```bash
List available groups
dnf group list
Install development tools
sudo dnf group install "Development Tools"
Install with shorter syntax
sudo dnf install @development-tools
```
Advanced Installation Options
Installation Flags and Options
Common DNF/YUM Flags
```bash
Assume yes to all prompts
sudo dnf install -y package-name
Download only, don't install
sudo dnf download package-name
Install with all dependencies
sudo dnf install --best package-name
Skip broken packages
sudo dnf install --skip-broken package-name
Exclude specific packages
sudo dnf install package-name --exclude=unwanted-package
```
Verbose and Debug Options
```bash
Verbose output
sudo dnf install -v package-name
Debug information
sudo dnf install -d 10 package-name
Show what would be done without executing
sudo dnf install --assumeno package-name
```
Working with Disabled Repositories
```bash
Enable specific repository for installation
sudo dnf install --enablerepo=repository-name package-name
Disable repository during installation
sudo dnf install --disablerepo=repository-name package-name
Install from specific repository only
sudo dnf install --disablerepo="*" --enablerepo=specific-repo package-name
```
Practical Examples
Example 1: Setting Up a Web Server
Complete LAMP stack installation:
```bash
Update system first
sudo dnf update -y
Install Apache web server
sudo dnf install httpd -y
Install PHP and common modules
sudo dnf install php php-mysqlnd php-fpm php-opcache php-gd php-xml php-mbstring -y
Install MariaDB
sudo dnf install mariadb-server mariadb -y
Enable and start services
sudo systemctl enable httpd mariadb php-fpm
sudo systemctl start httpd mariadb php-fpm
```
Example 2: Development Environment Setup
Setting up a complete development environment:
```bash
Install development tools group
sudo dnf group install "Development Tools" -y
Install version control
sudo dnf install git subversion -y
Install programming languages
sudo dnf install python3 python3-pip nodejs npm -y
Install text editors and IDEs
sudo dnf install vim emacs code -y
Install containerization tools
sudo dnf install docker podman -y
```
Example 3: System Monitoring Tools
Installing comprehensive monitoring suite:
```bash
System monitoring tools
sudo dnf install htop iotop nethogs -y
Network tools
sudo dnf install nmap tcpdump wireshark-cli -y
Performance analysis
sudo dnf install sysstat perf strace -y
Log analysis
sudo dnf install logwatch rsyslog-elasticsearch -y
```
Repository Management
Understanding Repository Configuration
Repository configurations are stored in `/etc/yum.repos.d/`:
```bash
List repository files
ls /etc/yum.repos.d/
View repository configuration
cat /etc/yum.repos.d/CentOS-Base.repo
```
Adding Third-Party Repositories
EPEL Repository (Extra Packages for Enterprise Linux)
```bash
RHEL 8/9, AlmaLinux, Rocky Linux
sudo dnf install epel-release -y
CentOS 7
sudo yum install epel-release -y
Verify EPEL installation
dnf repolist | grep epel
```
RPM Fusion Repository
```bash
Install RPM Fusion Free
sudo dnf install --nogpgcheck https://mirrors.rpmfusion.org/free/el/rpmfusion-free-release-$(rpm -E %rhel).noarch.rpm
Install RPM Fusion Non-free
sudo dnf install --nogpgcheck https://mirrors.rpmfusion.org/nonfree/el/rpmfusion-nonfree-release-$(rpm -E %rhel).noarch.rpm
```
Creating Custom Repository Files
```bash
Create custom repository file
sudo vi /etc/yum.repos.d/custom.repo
Example repository configuration
[custom-repo]
name=Custom Repository
baseurl=https://repo.example.com/rhel/$releasever/$basearch/
enabled=1
gpgcheck=1
gpgkey=https://repo.example.com/RPM-GPG-KEY
```
Repository Management Commands
```bash
List enabled repositories
dnf repolist
List all repositories (enabled and disabled)
dnf repolist --all
Enable repository
sudo dnf config-manager --enable repository-name
Disable repository
sudo dnf config-manager --disable repository-name
Add new repository
sudo dnf config-manager --add-repo https://example.com/repo.repo
Clean repository cache
sudo dnf clean all
Rebuild repository cache
sudo dnf makecache
```
Troubleshooting Common Issues
Package Installation Failures
Dependency Resolution Problems
```bash
Check for dependency issues
dnf deplist package-name
Install with best effort (skip problematic dependencies)
sudo dnf install --best --allowerasing package-name
Force installation (use with caution)
sudo rpm -ivh --force --nodeps package.rpm
```
Repository Connection Issues
```bash
Test repository connectivity
dnf repolist -v
Check network connectivity
ping 8.8.8.8
curl -I http://mirror.centos.org
Temporarily disable problematic repositories
sudo dnf install --disablerepo=problematic-repo package-name
```
GPG Key Problems
```bash
Import GPG keys manually
sudo rpm --import https://repo.example.com/RPM-GPG-KEY
Install without GPG check (not recommended)
sudo dnf install --nogpgcheck package-name
Check installed GPG keys
rpm -qa gpg-pubkey*
```
Disk Space Issues
```bash
Check available disk space
df -h
Clean package cache
sudo dnf clean all
Remove unnecessary packages
sudo dnf autoremove
Check largest installed packages
rpm -qa --queryformat '%{SIZE} %{NAME}\n' | sort -rn | head -20
```
Lock File Issues
```bash
Remove stuck lock files (when no package manager is running)
sudo rm -f /var/lib/rpm/.rpm.lock
sudo rm -f /var/cache/dnf/*/packages.lock
Rebuild RPM database if corrupted
sudo rpm --rebuilddb
```
Common Error Messages and Solutions
"No package available"
```bash
Update repository metadata
sudo dnf makecache
Search for similar package names
dnf search partial-package-name
Check if package is in different repository
dnf provides */filename
```
"Transaction check error"
```bash
Clean cache and retry
sudo dnf clean all
sudo dnf makecache
Check for conflicting packages
rpm -qa | grep conflicting-package
Use distro-sync to resolve conflicts
sudo dnf distro-sync
```
Best Practices
Security Best Practices
Repository Security
```bash
Always verify GPG signatures
Never disable GPG checking unless absolutely necessary
Use official repositories when possible
Regularly update repository configurations
```
Package Verification
```bash
Verify package integrity after installation
rpm -V package-name
Check package signatures
rpm -K package.rpm
Verify all installed packages
rpm -Va
```
Performance Optimization
Cache Management
```bash
Enable metadata caching
echo "metadata_expire=7d" >> /etc/dnf/dnf.conf
Use fastest mirror
sudo dnf install dnf-plugin-fastestmirror
Parallel downloads (DNF only)
echo "max_parallel_downloads=10" >> /etc/dnf/dnf.conf
```
System Maintenance
```bash
Regular system updates
sudo dnf update -y
Clean old packages
sudo dnf autoremove
Check for security updates
sudo dnf updateinfo list security
```
Documentation and Logging
Keep Installation Records
```bash
Log installations
dnf history
Detailed history
dnf history info transaction-id
Undo installations
sudo dnf history undo transaction-id
```
Package Documentation
```bash
Read package information
dnf info package-name
List package files
rpm -ql package-name
Find package documentation
rpm -qd package-name
```
Security Considerations
Package Source Verification
Always verify package sources and signatures:
```bash
Check package signature
rpm -K package.rpm
Verify repository GPG keys
rpm -qa gpg-pubkey* --qf '%{NAME}-%{VERSION}-%{RELEASE}\t%{SUMMARY}\n'
Import trusted keys only
sudo rpm --import /path/to/trusted/GPG-KEY
```
Minimal Installation Principle
Install only necessary packages:
```bash
List installed packages
dnf list installed
Remove unnecessary packages
sudo dnf remove unnecessary-package
Find packages that can be removed
sudo dnf autoremove --assumeno
```
Regular Security Updates
```bash
Check for security updates
dnf updateinfo list security
Install security updates only
sudo dnf update --security
Set up automatic security updates
sudo dnf install dnf-automatic
sudo systemctl enable --now dnf-automatic.timer
```
Conclusion
Mastering package management with `dnf` and `yum` on RHEL-based distributions is essential for effective system administration. This comprehensive guide has covered everything from basic installation commands to advanced troubleshooting techniques and security best practices.
Key Takeaways
1. Choose the Right Tool: Use `dnf` for modern RHEL-based systems (8+) and `yum` for older versions
2. Repository Management: Properly configure and maintain repositories for reliable package sources
3. Security First: Always verify package signatures and use trusted repositories
4. Regular Maintenance: Keep systems updated and remove unnecessary packages
5. Troubleshooting Skills: Develop systematic approaches to resolve common package management issues
Next Steps
To further enhance your package management skills:
1. Explore Advanced Features: Learn about package groups, modules, and streams
2. Automation: Implement automated package management with Ansible or other configuration management tools
3. Custom Repositories: Set up internal package repositories for enterprise environments
4. Monitoring: Implement package update monitoring and alerting systems
Additional Resources
- Official Red Hat Documentation
- CentOS, AlmaLinux, and Rocky Linux community resources
- Package management automation tools
- Security advisory mailing lists
By following the practices and techniques outlined in this guide, you'll be well-equipped to manage packages effectively across all major RHEL-based distributions, ensuring your systems remain secure, up-to-date, and properly maintained.
Remember that package management is an ongoing responsibility that requires attention to security updates, system maintenance, and staying informed about best practices in your specific environment. Regular practice with these commands and concepts will build the expertise needed for professional system administration.