在 Linux 系统学习和开发过程中,软件安装与管理是绕不开的核心环节。对于初学者而言,直接编译源代码安装软件不仅繁琐,还容易陷入依赖关系的 “迷宫”;而熟练掌握软件包管理器,能让我们高效、便捷地完成软件的安装、更新、卸载等操作。本文将从软件包的本质出发,深入剖析 Linux 软件生态,重点详解 yum 包管理器的核心用法,并结合实战案例帮助大家彻底掌握这一必备技能。下面就让我们正式开始吧!
在 Windows 系统中,我们安装软件通常是下载.exe格式的安装程序,双击后按照向导完成配置。而在 Linux 系统中,软件包(Package)本质上是将编译好的可执行程序、配置文件、依赖库等资源打包形成的归档文件,相当于 Linux 版的 “安装程序”。
简单来说,软件包就是软件的 “压缩包 + 安装脚本” 组合:
与源代码安装相比,使用软件包安装的优势极为明显:
软件包和软件包管理器的关系,就如同手机 App 与应用商店 —— 软件包是 “商品”,包管理器是 “销售和管理平台”。
包管理器(Package Manager)是 Linux 系统中用于管理软件包的工具集,核心功能包括:
不同 Linux 发行版采用了不同的包管理体系,最主流的有yum和apt两类:
包管理器 | 适用发行版 | 软件包格式 | 核心命令 |
|---|---|---|---|
yum | CentOS、RHEL、Fedora | .rpm | yum install/remove/update |
apt | Ubuntu、Debian | .deb | apt install/remove/update |
本文重点讲解 yum 包管理器,其全称为 Yellow dog Updater, Modified,是基于 RPM 包构建的包管理工具,以自动解决依赖关系的强大能力著称,广泛应用于 CentOS 等企业级 Linux 发行版。
Linux 系统安装软件的过程,本质上是 “获取软件包→解决依赖→部署文件→配置系统” 的闭环流程。在没有包管理器的时代,这个流程需要用户手动完成:
./configure配置编译参数;make命令编译源代码;make install安装软件;而包管理器的出现,将上述流程自动化,其工作原理可概括为:
yum install nginx);
软件源仓库(Repository)是存储软件包的远程服务器,相当于包管理器的 “软件超市”。每个 Linux 系统都会配置默认的软件源,用户也可以添加第三方软件源或国内镜像源。
软件依赖是指一个软件运行时需要依赖其他软件库或组件才能正常工作。例如,我们安装 nginx 时,它可能需要依赖:
这些依赖关系可能形成层级结构:A 依赖 B,B 依赖 C,C 依赖 D…… 如果手动安装,需要逐一解决所有依赖,极易出错;而 yum 能自动识别并安装所有依赖包,极大简化了操作。

由于国外官方源的服务器位于海外,国内用户下载软件包时速度较慢,甚至会出现连接超时的情况。因此,将软件源替换为国内镜像源是 Linux 使用的必备优化步骤。

镜像源名称 | 官方链接 | 特点 |
|---|---|---|
阿里云开源镜像站 | https://developer.aliyun.com/mirror/ | 覆盖全、速度快,支持所有主流 Linux 发行版 |
清华大学开源软件镜像站 | https://mirrors.tuna.tsinghua.edu.cn/ | 学术机构维护,稳定可靠,含丰富文档 |
中国科学技术大学开源镜像站 | http://mirrors.ustc.edu.cn/ | 资源全面,支持多种编程语言和开发工具 |
网易开源镜像站 | http://mirrors.163.com/ | 访问速度快,提供便捷的搜索功能 |
北京交通大学自由与开源软件镜像站 | https://mirror.bjtu.edu.cn/ | 文档详细,配置指南清晰 |

以 CentOS 7 为例,将默认 yum 源替换为阿里云镜像源,步骤如下:
(1)备份原有 yum 源配置文件
# 创建备份目录
sudo mkdir /etc/yum.repos.d/backup
# 移动原有配置文件到备份目录
sudo mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/backup/(2)下载阿里云 yum 源配置文件
sudo curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo(3)清理并生成新的 yum 缓存
# 清理旧缓存
sudo yum clean all
# 生成新缓存
sudo yum makecache(4)验证源配置是否成功
# 查看可用的软件源列表
sudo yum repolist输出结果示例(显示阿里云源的仓库信息):
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
repo id repo name status
base/7/x86_64 CentOS-7 - Base - mirrors.aliyun.com 10,072
extras/7/x86_64 CentOS-7 - Extras - mirrors.aliyun.com 515
updates/7/x86_64 CentOS-7 - Updates - mirrors.aliyun.com 1,809
repolist: 12,396如果使用 Ubuntu 系统,可参考以下步骤替换为清华源:
(1)备份原有 apt 源配置文件
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak(2)下载清华源配置文件
sudo wget -O /etc/apt/sources.list https://mirrors.tuna.tsinghua.edu.cn/ubuntu/sources.list(3)更新 apt 缓存
sudo apt updateLinux 软件生态之所以强大,主要得益于以下特点:

yum 作为 CentOS 系统默认的包管理器,功能强大且操作简洁。下面将为大家详细讲解 yum 的常用命令,并结合实际案例展示其用法。
使用 yum 前需要满足以下条件:
ping www.baidu.com验证);yum repolist验证)。验证网络连通性示例:
ping www.baidu.com -c 3输出结果(显示网络正常):
PING www.a.shifen.com (183.232.231.172) 56(84) bytes of data.
64 bytes from 183.232.231.172 (183.232.231.172): icmp_seq=1 ttl=54 time=12.3 ms
64 bytes from 183.232.231.172 (183.232.231.172): icmp_seq=2 ttl=54 time=11.8 ms
64 bytes from 183.232.231.172 (183.232.231.172): icmp_seq=3 ttl=54 time=12.1 ms
--- www.a.shifen.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2003ms
rtt min/avg/max/mdev = 11.825/12.107/12.345/0.226 msyum 提供了丰富的查询命令,可帮助我们查找软件包、查看软件信息、查询安装状态等。
yum list 说明:该命令会列出当前软件源中所有可用的软件包,数量通常较多,大家可以结合grep筛选目标软件。
语法:yum list | grep 软件包名称
示例:搜索 lrzsz 软件包(一款文件传输工具)
yum list | grep lrzsz输出结果:
lrzsz.x86_64 0.12.20-36.el7 base结果说明:
lrzsz.x86_64:软件包名称,x86_64表示 64 位系统版本(32 位系统后缀为i686);0.12.20-36.el7:软件版本号,el7表示适用于 CentOS 7/RHEL 7;base:软件源名称,即该软件包来自 base 仓库。 语法:yum info 软件包名称
示例:查看 lrzsz 软件包的详细信息
yum info lrzsz输出结果:
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
Available Packages
Name : lrzsz
Arch : x86_64
Version : 0.12.20
Release : 36.el7
Size : 78 k
Repo : base/7/x86_64
Summary : The lrz and lsz modem communications programs
URL : http://www.ohse.de/uwe/software/lrzsz.html
License : GPLv2+
Description : Lrzsz (consisting of lrz and lsz) is a cosmetically modified
: zmodem/ymodem/xmodem package built from the public-domain version
: of the rzsz package. Lrzsz was created to provide a working
: GNU copylefted Zmodem solution for Linux systems.结果说明:
Name:软件包名称;Arch:支持的架构(x86_64 表示 64 位);Version:软件版本;Size:软件包大小;Repo:所属软件源;Summary:软件简介;Description:软件详细描述。 语法:yum list installed | grep 软件包名称
示例:查询是否已安装 gcc 编译器
yum list installed | grep gcc输出结果(已安装):
gcc.x86_64 4.8.5-44.el7 @base
gcc-c++.x86_64 4.8.5-44.el7 @base
gcc-gfortran.x86_64 4.8.5-44.el7 @base若未安装,则无输出结果。
语法:yum deplist 软件包名称
示例:查询 nginx 软件包的依赖关系
yum deplist nginx输出结果(部分):
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
Finding dependencies:
package: nginx.x86_64 1:1.20.1-9.el7
dependency: libpcre.so.1()(64bit)
provider: pcre.x86_64 8.32-17.el7
dependency: libcrypto.so.10()(64bit)
provider: openssl-libs.x86_64 1:1.0.2k-25.el7_9
dependency: libz.so.1()(64bit)
provider: zlib.x86_64 1.2.7-21.el7_9
dependency: systemd-units
provider: systemd.x86_64 219-78.el7_9.5 yum 安装软件的核心命令是yum install,支持单软件安装、多软件安装、静默安装等多种场景。
语法:sudo yum install 软件包名称
示例:安装 lrzsz 软件包
sudo yum install lrzsz执行过程输出(部分):
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package lrzsz.x86_64 0:0.12.20-36.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
================================================================================
Package Arch Version Repository Size
================================================================================
Installing:
lrzsz x86_64 0.12.20-36.el7 base 78 k
Transaction Summary
================================================================================
Install 1 Package
Total download size: 78 k
Installed size: 187 k
Is this ok [y/d/N]: y # 输入y确认安装
Downloading packages:
lrzsz-0.12.20-36.el7.x86_64.rpm | 78 kB 00:00:00
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : lrzsz-0.12.20-36.el7.x86_64 1/1
Verifying : lrzsz-0.12.20-36.el7.x86_64 1/1
Installed:
lrzsz.x86_64 0:0.12.20-36.el7
Complete! # 安装完成 语法:sudo yum install -y 软件包名称
示例:静默安装 gcc 编译器
sudo yum install -y gcc 说明:-y选项表示自动回答 “yes”,无需手动确认安装过程,比较适用于脚本自动化场景。
语法:sudo yum install -y 软件包1 软件包2 软件包3
示例:同时安装 gcc、g++、make 工具
sudo yum install -y gcc g++ make输出结果(部分):
Installed:
gcc.x86_64 0:4.8.5-44.el7 gcc-c++.x86_64 0:4.8.5-44.el7 make.x86_64 1:3.82-24.el7
Dependency Installed:
cpp.x86_64 0:4.8.5-44.el7 glibc-devel.x86_64 0:2.17-326.el7_9
glibc-headers.x86_64 0:2.17-326.el7_9 kernel-headers.x86_64 0:3.10.0-1160.83.1.el7
libmpc.x86_64 0:1.0.1-3.el7 mpfr.x86_64 0:3.1.1-4.el7
Complete!部分软件不在 CentOS 默认源中,需要先安装 EPEL(Extra Packages for Enterprise Linux)扩展源,再进行安装。
示例:安装 EPEL 源并安装 nginx
# 安装EPEL扩展源
sudo yum install -y epel-release
# 安装nginx
sudo yum install -y nginx说明:EPEL 源提供了大量官方源中没有的软件包,是 CentOS 系统必备的扩展源。
yum 支持单个软件升级和系统所有软件的批量升级,确保软件保持最新版本,修复已知漏洞。
语法:sudo yum update -y 软件包名称
示例:升级 nginx 软件
sudo yum update -y nginx输出结果(部分):
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* epel: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package nginx.x86_64 1:1.20.1-9.el7 will be updated
---> Package nginx.x86_64 1:1.20.1-10.el7 will be an update
--> Finished Dependency Resolution
Dependencies Resolved
================================================================================
Package Arch Version Repository Size
================================================================================
Updating:
nginx x86_64 1:1.20.1-10.el7 epel 570 k
Transaction Summary
================================================================================
Upgrade 1 Package
Total download size: 570 k
Downloading packages:
nginx-1.20.1-10.el7.x86_64.rpm | 570 kB 00:00:01
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Updating : 1:nginx-1.20.1-10.el7.x86_64 1/2
Cleanup : 1:nginx-1.20.1-9.el7.x86_64 2/2
Verifying : 1:nginx-1.20.1-10.el7.x86_64 1/2
Verifying : 1:nginx-1.20.1-9.el7.x86_64 2/2
Updated:
nginx.x86_64 1:1.20.1-10.el7
Complete! 语法:sudo yum update -y
说明:该命令会升级系统中所有已安装的软件包,包括内核。升级完成后建议重启系统以应用更新。
语法:yum check-update
示例:查看系统中可升级的软件包
yum check-update输出结果(部分):
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* epel: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
nginx.x86_64 1:1.20.1-10.el7 epel
openssl.x86_64 1:1.0.2k-25.el7_9 updates
openssl-libs.x86_64 1:1.0.2k-25.el7_9 updates
systemd.x86_64 219-78.el7_9.5 updates当软件不再需要时,就可以使用 yum 彻底卸载软件及其相关依赖。
语法:sudo yum remove -y 软件包名称
示例:卸载 lrzsz 软件
sudo yum remove -y lrzsz输出结果(部分):
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
Resolving Dependencies
--> Running transaction check
---> Package lrzsz.x86_64 0:0.12.20-36.el7 will be erased
--> Finished Dependency Resolution
Dependencies Resolved
================================================================================
Package Arch Version Repository Size
================================================================================
Removing:
lrzsz x86_64 0.12.20-36.el7 @base 187 k
Transaction Summary
================================================================================
Remove 1 Package
Installed size: 187 k
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Erasing : lrzsz-0.12.20-36.el7.x86_64 1/1
Verifying : lrzsz-0.12.20-36.el7.x86_64 1/1
Removed:
lrzsz.x86_64 0:0.12.20-36.el7
Complete! yum 默认只卸载指定软件,不自动删除其依赖的软件包。如果需要清理无用依赖,可结合yum autoremove命令:
# 卸载nginx
sudo yum remove -y nginx
# 清理无用依赖
sudo yum autoremove -y 说明:yum autoremove会删除系统中没有被任何软件依赖的包,可释放磁盘空间,但使用时需谨慎,避免误删必要组件。
yum 会缓存下载的软件包和元数据,以加快后续操作速度。但缓存文件会占用磁盘空间,需定期清理。
# 清理所有缓存
sudo yum clean all
# 清理软件包缓存
sudo yum clean packages
# 清理元数据缓存
sudo yum clean metadatasudo yum makecache说明:生成缓存时,yum 会从软件源下载最新的元数据并缓存到本地,后续查询和安装操作会优先使用缓存,提升速度。
除了替换镜像源,yum 还支持启用 / 禁用软件源、添加第三方源等操作。
yum repolist all输出结果(部分):
repo id repo name status
base/7/x86_64 CentOS-7 - Base - mirrors.aliyun.com enabled: 10,072
epel/x86_64 Extra Packages for Enterprise Linux 7 - enabled: 13,752
extras/7/x86_64 CentOS-7 - Extras - mirrors.aliyun.com enabled: 515
updates/7/x86_64 CentOS-7 - Updates - mirrors.aliyun.com enabled: 1,809语法:
sudo yum-config-manager --disable 源名称sudo yum-config-manager --enable 源名称示例:禁用 epel 源
sudo yum-config-manager --disable epel验证禁用结果:
yum repolist all | grep epel输出结果:
epel/x86_64 Extra Packages for Enterprise Linux 7 - disabled(1)创建 nginx 源配置文件
sudo vi /etc/yum.repos.d/nginx.repo(2)写入以下内容(Nginx 官方源)
[nginx-stable]
name=nginx stable repo
baseurl=http://nginx.org/packages/centos/$releasever/$basearch/
gpgcheck=1
enabled=1
gpgkey=https://nginx.org/keys/nginx_signing.key
module_hotfixes=true(3)验证源是否生效
yum repolist | grep nginx输出结果:
nginx-stable/x86_64 nginx stable repo 14原因:有其他 yum 进程正在运行(如系统自动更新),占用了 yum 锁。
解决方案:
(1)查看占用 yum 锁的进程
ps aux | grep yum(2)终止该进程(替换为实际 PID)
sudo kill -9 PID(3)删除 yum 锁文件
sudo rm -f /var/run/yum.pid原因:
解决方案:
sudo yum install -y epel-release);sudo yum clean all && sudo yum makecache)。原因:已安装的软件包与待安装软件的依赖版本不兼容。
解决方案:
yum install --skip-broken 软件包名称跳过冲突依赖;yum 会记录所有安装、升级、卸载操作,可通过历史记录进行回滚、查询等操作。
yum history输出结果(部分):
Loaded plugins: fastestmirror
ID | Command line | Date and time | Action(s) | Altered
-------------------------------------------------------------------------------
10 | install -y nginx | 2024-10-26 14:30 | Install | 15
9 | remove -y lrzsz | 2024-10-26 14:25 | Erase | 1
8 | install -y lrzsz | 2024-10-26 14:20 | Install | 1
7 | install -y epel-release | 2024-10-26 14:15 | Install | 1 语法:sudo yum history undo 历史ID
示例:回滚 ID 为 9 的卸载操作(即重新安装 lrzsz)
sudo yum history undo 9 -y说明:回滚操作会撤销指定历史记录中的所有变更,适用于误操作后的恢复。
在没有网络的环境中,可先下载软件包及其依赖,再进行离线安装。
# 创建存放软件包的目录
mkdir -p /tmp/yum-packages
# 下载lrzsz及其依赖到指定目录
yum install -y --downloadonly --downloaddir=/tmp/yum-packages lrzsz# 进入软件包目录
cd /tmp/yum-packages
# 安装所有rpm包
sudo rpm -ivh *.rpm 说明:--downloadonly选项表示只下载不安装,--downloaddir指定下载目录。
掌握 yum 的使用,不仅能让我们高效管理 Linux 系统中的软件,还能为后续的开发和运维工作打下坚实基础。无论是安装开发工具、部署服务程序,还是维护系统安全,yum 都能成为我们的得力助手。希望本文能帮助大家彻底理解 yum 的工作原理和使用方法,在 Linux 的学习和实践中少走弯路。 后续建议大家多动手实践,尝试使用 yum 安装不同的软件包(如 gcc、nginx、mysql 等),熟悉各种命令的用法,逐步形成自己的操作习惯。如果遇到问题,可结合 yum 的错误提示和本文的解决方案进行排查,也可以查阅官方文档或社区资源获取更多帮助。感谢大家的支持!