Fading Coder

One Final Commit for the Last Sprint

Home > Tech > Content

A Complete Guide to YUM Package Management on CentOS

Tech 2

What is YUM

YUM (Yellowdog Updater, Modified) is a dependancy-aware package management tool for RPM-based Linux distributions that simplifies the entire lifecycle of software management, from installation to removal, compared to manual RPM management.

YUM Repository Basics

A YUM repository is a collection of RPM packages with structured metadata that YUM uses to automatically resolve dependencies. There are two primary categories of YUM repositories:

  1. Local repositories: Stored on local disk (e.g. mounted installation ISO, custom directory of pre-downloaded packages) for offline installation scenarios
  2. Network repositories: Hosted on public mirror servers, offering the latest versions and widest selection of software

Popular public network mirrors in China include Alibaba Cloud Open Mirror, Tsinghua University Open Source Mirror, and official upstream repos. Most production setups use a combination of the core CentOS base repo (for operating system native packages) and the EPEL repo (for extra third-party software) to get full package coverage. In simple terms, a YUM repo is just a URL or file path pointing to a collection of RPMs, defined in a .repo configuration file that YUM reads at runtime.

YUM Configuration Directory Structure

All YUM repository configurations are stored in a fixed system path. Only files ending with .repo in the first level of this directory are recognized as valid repository configs. YUM ignores files in subdirectories and files that don't match the .repo naming rule.

cd /etc/yum.repos.d/
pwd
# Output: /etc/yum.repos.d

# List existing repository files
ls

Set Up A Local CD-ROM YUM Repository

This method uses the official CentOS installation ISO as a local source for core packages, ideal for offline environments with no network access.

  1. Attach the CentOS 7 ISO image to your system (for VMware virtual machines, simply connect the ISO file to the VM's virtual CD drive).
  2. Mount the CD device to a local mount point:
# Create a dedicated mount point
mkdir /mnt/cdrom_repo

# Mount the CD drive (most systems expose the CD device as /dev/sr0)
mount /dev/sr0 /mnt/cdrom_repo
# The "write protection, mounting read-only" message is normal and expected

# Verify the mount was successful
ls /mnt/cdrom_repo
df -h | grep /dev/sr0
  1. Create the YUM repository config file:
vim /etc/yum.repos.d/local_cdrom.repo

Add the following content to the file:

[local-cd]
name=CentOS 7 Local CDROM Repository
baseurl=file:///mnt/cdrom_repo/
enabled=1
gpgcheck=0
  1. Clear old YUM cache and generate new metadata for the new repository:
# Clear existing cached metadata
yum clean all
rm -rf /var/cache/yum

# Generate new cache for the new repository
yum makecache
  1. Test the repository:
# Count total available packages
yum list | wc -l

# Test install Firefox as an example
yum remove firefox -y
yum install firefox -y

# Search for database packages
yum list | grep mariadb

The default CentOS CD repository only includes MariaDB, a community fork of MySQL created after Oracle changed MySQL's licensing terms. It has identical functionality to original MySQL, only differing in package naming. If you need to install the original Oracle-maintained MySQL, you will need to add an external repository.

Set Up Alibaba Cloud Network YUM Repository

Network repositories contain far more packages than the installation CD, including the latest versions of most popular open source software. For CentOS 7, you can set up the Alibaba Cloud mirror with these steps:

# Download the base CentOS repository configuration
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo

# Download the EPEL repository configuration (adds thousands of third-party packages)
wget -O /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo

# Clear old cache and generate new metadata
yum clean all
rm -rf /var/cache/yum
yum makecache

# Verify the repository works correctly
yum list | wc -l
yum list | grep nginx
yum list | grep mysql

Build A Custom Local YUM Repo For Offline Installation

If you need to install software on an offline server, you can pre-download all required RPMs and their dependencies on a connected machine, then build a custom local repo to use on the offline host.

YUM provides two flags to download packages without installing them: --downloadonly skips installation after download, and --downloaddir specifies where to save the RPM files.

# Create a directory to store your offline packages
mkdir /opt/offline_packages

# Download vim and all its required dependencies to the directory
yum install --downloadonly --downloaddir=/opt/offline_packages vim

# Install the createrepo tool to generate YUM metadata
yum install createrepo -y

# Generate the required repository metadata for your package directory
createrepo /opt/offline_packages

After running createrepo, a new repodata directory will be created in your package folder, which is required for YUM to recognize the directory as a valid repository. Next, create the repository configuration file:

cat >> /etc/yum.repos.d/custom_offline.repo <<EOF
[custom-offline]
name=Custom Offline Package Repository
baseurl=file:///opt/offline_packages
enabled=1
gpgcheck=0
EOF

You can now install packages directly from your local repository without any network access:

yum install vim -y

Alternative: Cache RPM Files After Installation

If you have already installed packages via YUM and want to save the downloaded RPMs for later reuse, you can enable package caching:

  1. Edit your repository configuration file, add the line keepcache=1 to the repository section
  2. Install the package normally: yum install nginx
  3. Locate all cached RPM files with this command:
find /var/cache/yum/ -name *.rpm

Adjust YUM Repository Priority

If you have multiple repositories that offer different versions of the same package, you can set priority to control which repository YUM prefers. Add the line priority=N to your repository configuration, where lower numbers mean higher priority (1 is the highest priority). For most use cases, the simplest solution is to disable or remove any unnecessary repositories to avoid unexpected version conflicts.

Example: Install MySQL 5.6.43 via Official YUM Repository

First, clean any existing MySQL or MariaDB packages to avoid dependency conflicts (warning: this deletes all existing MySQL data, only run on fresh installations):

yum remove mysql* mariadb* -y

Create the official MySQL 5.6 repository configuration:

cat > /etc/yum.repos.d/mysql56-official.repo <<EOF
[mysql56-community]
name=MySQL 5.6 Community Repository
baseurl=https://repo.mysql.com/yum/mysql-5.6-community/el/7/x86_64/
enabled=1
gpgcheck=0
EOF

Install the exact version of the MySQL server package:

yum install mysql-community-server-5.6.43

Resolve any dependency errors if they occur during installation. After installation completes, start the MySQL service:

systemctl start mysqld

# Verify the service is running correctly
netstat -tunlp | grep mysql
ps -ef | grep mysql

# Test login (default root account has no password on fresh installation)
mysql -uroot -p

Once logged in, confirm the installation works:

show databases;
exit;

Related Articles

Understanding Strong and Weak References in Java

Strong References Strong reference are the most prevalent type of object referencing in Java. When an object has a strong reference pointing to it, the garbage collector will not reclaim its memory. F...

Comprehensive Guide to SSTI Explained with Payload Bypass Techniques

Introduction Server-Side Template Injection (SSTI) is a vulnerability in web applications where user input is improper handled within the template engine and executed on the server. This exploit can r...

Implement Image Upload Functionality for Django Integrated TinyMCE Editor

Django’s Admin panel is highly user-friendly, and pairing it with TinyMCE, an effective rich text editor, simplifies content management significantly. Combining the two is particular useful for bloggi...

Leave a Comment

Anonymous

◎Feel free to join the discussion and share your thoughts.