How to use yum in CentOS Linux
How to Use yum in CentOS Linux
The Yellowdog Updater Modified (yum) package manager is an essential tool for CentOS Linux users, providing a powerful and user-friendly way to install, update, and manage software packages. Whether you're a system administrator or a Linux enthusiast, mastering yum will significantly streamline your package management tasks and help maintain a secure, up-to-date system.
In this comprehensive guide, we'll explore everything you need to know about using yum in CentOS Linux, from basic commands to advanced package management techniques.
What is yum and Why Use It?
yum is a command-line package management utility for RPM-based Linux distributions, including CentOS, Red Hat Enterprise Linux (RHEL), and Fedora. It serves as a front-end to the RPM package manager, automatically handling dependencies and providing a more intuitive interface for package operations.
Key Benefits of yum:
- Automatic dependency resolution: yum automatically identifies and installs required dependencies
- Repository management: Easy access to multiple software repositories
- Security updates: Streamlined security patch installation
- Transaction history: Track and rollback package changes
- Group installations: Install related packages as a group
Prerequisites and Setup
Before diving into yum commands, ensure you have:
- CentOS Linux system with root or sudo privileges
- Active internet connection for repository access
- Basic command-line knowledge
To verify yum is installed and check its version:
```bash
yum --version
```
Basic yum Commands
Updating Package Information
Before performing any package operations, update the package database:
```bash
sudo yum update
```
This command updates all installed packages to their latest versions. To update only the package database without installing updates:
```bash
sudo yum check-update
```
Installing Packages
To install a single package:
```bash
sudo yum install package_name
```
For example, to install the Apache web server:
```bash
sudo yum install httpd
```
To install multiple packages simultaneously:
```bash
sudo yum install package1 package2 package3
```
Searching for Packages
Search for packages by name or description:
```bash
yum search keyword
```
Example searching for text editors:
```bash
yum search editor
```
For more detailed package information:
```bash
yum info package_name
```
Removing Packages
To remove an installed package:
```bash
sudo yum remove package_name
```
To remove a package and its dependencies that are no longer needed:
```bash
sudo yum autoremove package_name
```
Advanced yum Operations
Working with Package Groups
yum allows you to install groups of related packages. To list available groups:
```bash
yum grouplist
```
To install a package group:
```bash
sudo yum groupinstall "Group Name"
```
Example installing development tools:
```bash
sudo yum groupinstall "Development Tools"
```
Local Package Installation
To install a local RPM file:
```bash
sudo yum localinstall /path/to/package.rpm
```
Downgrading Packages
To downgrade a package to a previous version:
```bash
sudo yum downgrade package_name
```
Viewing Installation History
Check yum transaction history:
```bash
yum history
```
To get detailed information about a specific transaction:
```bash
yum history info transaction_id
```
To undo a transaction:
```bash
sudo yum history undo transaction_id
```
Repository Management
Viewing Configured Repositories
List all enabled repositories:
```bash
yum repolist
```
To see all repositories (enabled and disabled):
```bash
yum repolist all
```
Adding New Repositories
Repository configuration files are stored in `/etc/yum.repos.d/`. To add a new repository, create a `.repo` file:
```bash
sudo nano /etc/yum.repos.d/example.repo
```
Example repository configuration:
```ini
[example-repo]
name=Example Repository
baseurl=https://example.com/centos/$releasever/$basearch/
enabled=1
gpgcheck=1
gpgkey=https://example.com/RPM-GPG-KEY
```
Enabling/Disabling Repositories
To temporarily enable a disabled repository:
```bash
sudo yum --enablerepo=repository_name install package_name
```
To permanently enable or disable a repository:
```bash
sudo yum-config-manager --enable repository_name
sudo yum-config-manager --disable repository_name
```
Useful yum Options and Flags
Common Command Options
- `-y`: Automatically answer "yes" to prompts
- `--nogpgcheck`: Skip GPG signature verification
- `--downloadonly`: Download packages without installing
- `--security`: Only install security updates
Examples:
```bash
Install without prompts
sudo yum install -y httpd
Download package without installing
sudo yum install --downloadonly --downloaddir=/tmp/ package_name
Install only security updates
sudo yum update --security
```
Cleaning yum Cache
yum stores downloaded packages and metadata in cache. To clean the cache:
```bash
Clean package cache
sudo yum clean packages
Clean metadata cache
sudo yum clean metadata
Clean all cache
sudo yum clean all
```
Package Information and Dependencies
Viewing Package Details
Get comprehensive information about a package:
```bash
yum info package_name
```
List files installed by a package:
```bash
rpm -ql package_name
```
Find which package provides a specific file:
```bash
yum whatprovides /path/to/file
```
Dependency Management
View package dependencies:
```bash
yum deplist package_name
```
Check for broken dependencies:
```bash
sudo yum check
```
Security and Updates
Security Updates
List available security updates:
```bash
yum --security check-update
```
Install only security updates:
```bash
sudo yum update --security
```
Automatic Updates
Configure automatic updates by installing and configuring `yum-cron`:
```bash
sudo yum install yum-cron
sudo systemctl enable yum-cron
sudo systemctl start yum-cron
```
Configure automatic updates in `/etc/yum/yum-cron.conf`:
```ini
[commands]
update_cmd = security
update_messages = yes
download_updates = yes
apply_updates = yes
```
Troubleshooting Common Issues
Repository Errors
Problem: "Cannot retrieve repository metadata"
Solution: Clean yum cache and update repository information:
```bash
sudo yum clean all
sudo yum makecache
```
GPG Key Errors
Problem: GPG signature verification fails
Solutions:
1. Import the correct GPG key:
```bash
sudo rpm --import /path/to/GPG-KEY
```
2. Temporarily skip GPG check:
```bash
sudo yum install --nogpgcheck package_name
```
Dependency Conflicts
Problem: Package conflicts or dependency issues
Solutions:
1. Force reinstallation:
```bash
sudo yum reinstall package_name
```
2. Skip broken packages:
```bash
sudo yum update --skip-broken
```
3. Use package-cleanup tool:
```bash
sudo package-cleanup --problems
sudo package-cleanup --dupes
```
Lock File Issues
Problem: "Another app is currently holding the yum lock"
Solution: Remove the lock file and kill yum processes:
```bash
sudo rm -f /var/run/yum.pid
sudo killall yum
```
Network Issues
Problem: Cannot connect to repositories
Solutions:
1. Check network connectivity
2. Verify firewall settings
3. Test repository URLs manually:
```bash
curl -I repository_url
```
Performance Optimization
Parallel Downloads
Enable parallel package downloads by adding to `/etc/yum.conf`:
```ini
[main]
max_parallel_downloads=5
```
Fastest Mirror Plugin
Install and configure the fastest mirror plugin:
```bash
sudo yum install yum-plugin-fastestmirror
```
Delta RPM
Enable delta RPM for smaller update downloads:
```bash
sudo yum install deltarpm
```
Add to `/etc/yum.conf`:
```ini
[main]
deltarpm=1
```
Best Practices
Regular Maintenance
1. Keep system updated: Run regular updates
```bash
sudo yum update
```
2. Clean cache periodically:
```bash
sudo yum clean all
```
3. Check for security updates:
```bash
yum --security check-update
```
Repository Management
- Only enable trusted repositories
- Regularly review enabled repositories
- Use GPG key verification
- Keep repository configurations organized
Backup and Recovery
- Create system snapshots before major updates
- Document installed packages:
```bash
yum list installed > installed_packages.txt
```
- Keep transaction history for rollbacks
Migration Notes: yum to DNF
CentOS 8 and newer versions use DNF (Dandified yum) as the default package manager. While yum commands still work through compatibility layers, consider learning DNF commands for future compatibility:
```bash
yum equivalent commands in DNF
dnf install package_name
dnf update
dnf remove package_name
dnf search keyword
```
Conclusion
Mastering yum is essential for effective CentOS Linux system administration. This powerful package manager simplifies software installation, updates, and maintenance while providing robust dependency resolution and repository management capabilities.
Key takeaways for successful yum usage:
- Start with basic commands and gradually explore advanced features
- Keep your system updated regularly for security and stability
- Understand repository management for accessing additional software
- Use troubleshooting techniques to resolve common issues
- Follow best practices for system maintenance and security
By implementing the techniques and commands covered in this guide, you'll be well-equipped to manage packages efficiently in your CentOS Linux environment. Remember to always test changes in a non-production environment first and maintain regular backups of your system.
Whether you're managing a single server or multiple CentOS systems, yum provides the tools necessary for maintaining secure, up-to-date, and well-organized Linux environments.