一、Linux下安装软件的三种方式
在Linux下安装软件有三种方式:rpm安装,yum安装,源码包安装。接下来,我们对这三种安装方式一一进行讲解。
二、rpm安装
rpm安装类似于windows自带的“安装/卸载”,通过rpm命令我们可以安装一些现成的二进制包。rpm安装的包的安装路径是固定的。
安装好光盘后,将它挂载到/mnt/,然后进到光盘中的Packages目录中,发现这里有很多包:
[root@ruanwenwu02 ~]# df -h 文件系统 容量 已用 可用 已用% 挂载点 /dev/sda3 18G 1.4G 17G 8% / devtmpfs 479M 0 479M 0% /dev tmpfs 489M 0 489M 0% /dev/shm tmpfs 489M 6.8M 482M 2% /run tmpfs 489M 0 489M 0% /sys/fs/cgroup /dev/sda1 197M 109M 88M 56% /boot tmpfs 98M 0 98M 0% /run/user/0 /dev/sr0 4.1G 4.1G 0 100% /mnt [root@ruanwenwu02 ~]# cd /mnt/ [root@ruanwenwu02 mnt]# ls CentOS_BuildTag EULA images LiveOS repodata RPM-GPG-KEY-CentOS-Testing-7 EFI GPL isolinux Packages RPM-GPG-KEY-CentOS-7 TRANS.TBL [root@ruanwenwu02 mnt]# cd Packages/ [root@ruanwenwu02 Packages]# ls 389-ds-base-1.3.5.10-11.el7.x86_64.rpm 389-ds-base-libs-1.3.5.10-11.el7.x86_64.rpm abattis-cantarell-fonts-0.0.16-3.el7.noarch.rpm abrt-2.1.11-45.el7.centos.x86_64.rpm
我们就拿这些包来做实验。在操作之前我们需要先了解一下包的结构。拿这个包abrt-2.1.11-45.el7.Centos.x86_64.rpm举例:
它被“-”分割开,abrt是包名;2.1.11的意思是主版本为2,次版本为1,修正号为11;centos是平台;x86_64是架构;rpm是文件后缀。
安装:
[root@ruanwenwu02 Packages]# rpm -ivh zlib-devdl 错误:打开 zlib-devdl 失败: 没有那个文件或目录 [root@ruanwenwu02 Packages]# rpm -ivh zlib-devel-1.2.7-17.el7.x86_64.rpm 准备中... ################################# [100%] 正在升级/安装... 1:zlib-devel-1.2.7-17.el7 ################################# [100%]
实验说明使用rpm安装包必须跟包的全名。
升级:
[root@ruanwenwu02 Packages]# rpm -Uvh zlib-devel-1.2.7-17.el7.x86_64.rpm 准备中... ################################# [100%] 软件包 zlib-devel-1.2.7-17.el7.x86_64 已经安装
我这里找不到升级包,就不再演示了。方法见代码。
卸载:
[root@ruanwenwu02 Packages]# rpm -e zlib-devel [root@ruanwenwu02 Packages]# rpm -q zlib-devel 未安装软件包 zlib-devel [root@ruanwenwu02 Packages]# rpm -ivh zlib-devel-1.2.7-17.el7.x86_64.rpm 准备中... ################################# [100%] 正在升级/安装... 1:zlib-devel-1.2.7-17.el7 ################################# [100%] [root@ruanwenwu02 Packages]# rpm -q zlib-devel zlib-devel-1.2.7-17.el7.x86_64
卸载的命令是rpm -e。实验发现下载之后就查询(rpm -q)不到了。再次安装就能查询到了。
查询所有已经安装的包:
[root@ruanwenwu02 Packages]# rpm -qa "vim*" vim-minimal-7.4.160-1.el7.x86_64 vim-filesystem-7.4.160-2.el7.x86_64 vim-enhanced-7.4.160-2.el7.x86_64 vim-common-7.4.160-2.el7.x86_64
发现我们已经安装了这么多包了。
查询是否安装:
[root@ruanwenwu02 Packages]# rpm -qa "ls" [root@ruanwenwu02 Packages]# rpm -qf "ls" 错误:文件 /mnt/Packages/ls:没有那个文件或目录 [root@ruanwenwu02 Packages]# rpm -qf /usr/bin/ls coreutils-8.22-18.el7.x86_64 [root@ruanwenwu02 Packages]# rpm -qa "coreutils" coreutils-8.22-18.el7.x86_64 [root@ruanwenwu02 Packages]# rpm -qa coreut [root@ruanwenwu02 Packages]# rpm -qa coreutils coreutils-8.22-18.el7.x86_64 [root@ruanwenwu02 Packages]# rpm -qa coreutil
实验发现,rpm -qa命令必须接包名或者模糊包名,引号可加可不加,但是如果是模糊查找的话,必须加引号:
[root@ruanwenwu02 Packages]# rpm -qa vim* [root@ruanwenwu02 Packages]# rpm -qa "vim*" vim-minimal-7.4.160-1.el7.x86_64 vim-filesystem-7.4.160-2.el7.x86_64 vim-enhanced-7.4.160-2.el7.x86_64 vim-common-7.4.160-2.el7.x86_64 [root@ruanwenwu02 Packages]# rpm -qa 'vim*' vim-minimal-7.4.160-1.el7.x86_64 vim-filesystem-7.4.160-2.el7.x86_64 vim-enhanced-7.4.160-2.el7.x86_64
查询包信息:
[root@ruanwenwu02 Packages]# rpm -qi vim-enhanced Name : vim-enhanced Epoch : 2 Version : 7.4.160 Release : 2.el7 Architecture: x86_64 Install Date: 2017年10月18日 星期三 19时16分34秒 Group : Applications/Editors Size : 2292098 License : Vim
查询包安装的文件:
[root@ruanwenwu02 Packages]# rpm -ql vim-enhanced /etc/profile.d/vim.csh /etc/profile.d/vim.sh /usr/bin/rvim /usr/bin/vim /usr/bin/vimdiff /usr/bin/vimtutor
查询命令是哪个包的:
[root@ruanwenwu02 Packages]# rpm -qf /usr/bin/vim vim-enhanced-7.4.160-2.el7.x86_64
三、yum常用命令
yum常用命令有安装,升级,卸载,重新安装,groupinstall,groupremove,下载rpm包,清除yum源缓存。
yum安装的优点是它能自动解决包的依赖问题。
安装:
[root@ruanwenwu02 etc]# yum -y install zziplib 已加载插件:fastestmirror, priorities base | 3.6 kB 00:00:00 extras | 3.4 kB 00:00:00 rpmforge | 1.9 kB 00:00:00 updates | 3.4 kB 00:00:00 Loading mirror speeds from cached hostfile * base: mirrors.tuna.tsinghua.edu.cn * extras: mirrors.tuna.tsinghua.edu.cn * rpmforge: mirrors.tuna.tsinghua.edu.cn * updates: mirrors.tuna.tsinghua.edu.cn 197 packages excluded due to repository priority protections 软件包 zziplib-0.13.62-5.el7.x86_64 已安装并且是最新版本 无须任何处理
yum -y install后面写包名就好了。-y参数的作用就是不要再询问是否安装了,默认进行安装。
重新安装:
[root@ruanwenwu02 etc]# yum -y reinstall zziplib 已加载插件:fastestmirror, priorities Loading mirror speeds from cached hostfile * base: mirrors.sohu.com * extras: mirrors.sohu.com * rpmforge: mirrors.tuna.tsinghua.edu.cn * updates: mirrors.sohu.com 197 packages excluded due to repository priority protections 正在解决依赖关系 --> 正在检查事务 ---> 软件包 zziplib.x86_64.0.0.13.62-5.el7 将被 已重新安装 --> 解决依赖关系完成 依赖关系解决 =============================================================================================================== Package 架构 版本 源 大小 =============================================================================================================== 重新安装: zziplib x86_64 0.13.62-5.el7 base 81 k 事务概要 =============================================================================================================== 重新安装 1 软件包 总下载量:81 k 安装大小:211 k Downloading packages: zziplib-0.13.62-5.el7.x86_64.rpm | 81 kB 00:00:02 Running transaction check Running transaction test Transaction test succeeded Running transaction 警告:RPM 数据库已被非 yum 程序修改。 正在安装 : zziplib-0.13.62-5.el7.x86_64 1/1 验证中 : zziplib-0.13.62-5.el7.x86_64 1/1 已安装: zziplib.x86_64 0:0.13.62-5.el7 完毕!
对于已经安装过的包,如果我们还想安装,就可以用这个yum -y reinstall命令。
yum list命令列出所有的包(安装的和未安装的):
zsync.x86_64 0.6.2-1.el6.rf rpmforge zvbi.x86_64 0.2.33-2.el6.rf rpmforge zvbi-devel.x86_64 0.2.33-2.el6.rf rpmforge zziplib.i686 0.13.62-5.el7 base zziplib-devel.i686 0.13.62-5.el7 base zziplib-devel.x86_64 0.13.62-5.el7 base zziplib-utils.x86_64 0.13.62-5.el7 base
最后一列代表是哪个yum源。
搜索:
[root@ruanwenwu02 etc]# yum search vim 已加载插件:fastestmirror, priorities Loading mirror speeds from cached hostfile * base: mirrors.sohu.com * extras: mirrors.sohu.com * rpmforge: mirrors.tuna.tsinghua.edu.cn * updates: mirrors.sohu.com 197 packages excluded due to repository priority protections ============================================== N/S matched: vim =============================================== perl-Vim-Tag.noarch : Generate Perl tags for Vim protobuf-vim.x86_64 : Vim syntax highlighting for Google Protocol Buffers descriptions vim-X11.x86_64 : The VIM version of the vi editor for the X Window System vim-common.x86_64 : The common files needed by any version of the VIM editor vim-enhanced.x86_64 : A version of the VIM editor which includes recent enhancements vim-filesystem.x86_64 : VIM filesystem layout vim-minimal.x86_64 : A minimal version of the VIM editor vim-puppet.noarch : Vim mode for Puppet vim-task.x86_64 : Vim support for TaskWarrior 名称和简介匹配 only,使用“search all”试试。
另一个搜索:
[root@ruanwenwu02 etc]# yum provides '/*/vim' 已加载插件:fastestmirror, priorities Loading mirror speeds from cached hostfile * base: mirrors.sohu.com * extras: mirrors.sohu.com * rpmforge: mirrors.tuna.tsinghua.edu.cn * updates: mirrors.sohu.com 197 packages excluded due to repository priority protections rpmforge/filelists_db | 2.3 MB 00:00:01 cmake-2.8.12.2-2.el7.x86_64 : Cross-platform make system 源 :base 匹配来源: 文件名 :/usr/share/cmake/editors/vim 2:docker-unit-test-1.12.6-48.git0fdc778.el7.centos.x86_64 : Automates deployment of containerized applications : - for running unit tests 源 :extras 匹配来源: 文件名 :/var/lib/docker-unit-test/docker-novolume-plugin-385ec70baac3ef356f868f391c8d7818140fbd44/Godeps/_workspace/src/github.com/docker/docker/contrib/syntax/vim 文件名 :/var/lib/docker-unit-test/contrib/syntax/vim 文件名 :/var/lib/docker-unit-test/v1.10-migrator-c417a6a022c5023c111662e8280f885f6ac259be/Godeps/_workspace/src/github.com/docker/docker/contrib/syntax/vim 文件名 :/var/lib/docker-unit-test/runc-79c3939053c870fbb4de5484d98640d5ba028ef4/Godeps/_workspace/src/github.com/docker/docker/contrib/syntax/vim 文件名 :/var/lib/docker-unit-test/containerd-fa8fb3d455e1baf716f3131581f0ed8b07c573a6/vendor/src/github.com/docker/docker/contrib/syntax/vim
实验发现,通过yum provides命令可以查看到是哪个源的哪个包包含了这个vim命令。
删除包:
[root@ruanwenwu02 etc]# yum remove zziplib 已加载插件:fastestmirror, priorities 正在解决依赖关系 --> 正在检查事务 ---> 软件包 zziplib.x86_64.0.0.13.62-5.el7 将被 删除 --> 解决依赖关系完成 依赖关系解决 =============================================================================================================== Package 架构 版本 源 大小 =============================================================================================================== 正在删除: zziplib x86_64 0.13.62-5.el7 @base 211 k 事务概要 =============================================================================================================== 移除 1 软件包 安装大小:211 k 是否继续?[y/N]:y Downloading packages: Running transaction check Running transaction test Transaction test succeeded Running transaction 正在删除 : zziplib-0.13.62-5.el7.x86_64 1/1 验证中 : zziplib-0.13.62-5.el7.x86_64 1/1 删除: zziplib.x86_64 0:0.13.62-5.el7 完毕!
可以加上-y参数,这样就不会询问了。
升级包:
[root@ruanwenwu02 etc]# yum update zziplib 已加载插件:fastestmirror, priorities Loading mirror speeds from cached hostfile * base: mirrors.tuna.tsinghua.edu.cn * extras: mirrors.tuna.tsinghua.edu.cn * rpmforge: mirrors.tuna.tsinghua.edu.cn * updates: mirrors.tuna.tsinghua.edu.cn 197 packages excluded due to repository priority protections 软件包 zziplib 可用,但尚未安装。 No packages marked for update
注意,如果yum update后面没有跟指定的包名的话,那么会升级系统中所有可升级的包,包括内核,这样是很危险的。
查看组文件包:
[root@ruanwenwu02 etc]# yum grouplist; 已加载插件:fastestmirror, priorities 没有安装组信息文件 Maybe run: yum groups mark convert (see man yum) Loading mirror speeds from cached hostfile * base: mirrors.sohu.com * extras: mirrors.sohu.com * rpmforge: mirrors.tuna.tsinghua.edu.cn * updates: mirrors.sohu.com 197 packages excluded due to repository priority protections 可用的环境分组: 最小安装 基础设施服务器 计算节点 文件及打印服务器 基本网页服务器 虚拟化主机 带 GUI 的服务器 GNOME 桌面 KDE Plasma Workspaces 开发及生成工作站 可用组: 传统 UNIX 兼容性 兼容性程序库 图形管理工具 安全性工具 开发工具 控制台互联网工具 智能卡支持 科学记数法支持 系统管理 系统管理工具 完成
安装组件:
[root@ruanwenwu02 etc]# yum gruopinstall 'Security Tools' Failed to set locale, defaulting to C Loaded plugins: fastestmirror, priorities No such command: gruopinstall. Please use /usr/bin/yum --help [root@ruanwenwu02 etc]# yum groupinstall 'Security Tools' Failed to set locale, defaulting to C Loaded plugins: fastestmirror, priorities
删除组件:
[root@ruanwenwu02 etc]# yum groupremove Failed to set locale, defaulting to C Loaded plugins: fastestmirror, priorities Error: Need a group or list of groups Mini usage: erase PACKAGE... Remove a package or packages from your system aliases: remove, autoremove, erase-n, erase-na, erase-nevra, autoremove-n, autoremove-na, autoremove-nevra, remove-n, remove-na, remove-nevra
清除yum源缓存:
[root@ruanwenwu02 etc]# yum clean all Failed to set locale, defaulting to C Loaded plugins: fastestmirror, priorities Cleaning repos: base extras rpmforge updates Cleaning up everything Cleaning up list of fastest mirrors
清除yum源缓存之后,第一次运行yum相关命令会慢一点,因为它要生成yum缓存。
下载rpm包:
[root@ruanwenwu02 etc]# yum -y install --downloadonly zvbi Failed to set locale, defaulting to C Loaded plugins: fastestmirror, priorities Loading mirror speeds from cached hostfile * base: mirrors.aliyun.com * extras: mirrors.tuna.tsinghua.edu.cn * rpmforge: mirrors.tuna.tsinghua.edu.cn * updates: mirrors.aliyun.com 197 packages excluded due to repository priority protections Resolving Dependencies --> Running transaction check ---> Package zvbi.x86_64 0:0.2.33-2.el6.rf will be installed --> Processing Dependency: libpng12.so.0(PNG12_0)(64bit) for package: zvbi-0.2.33-2.el6.rf.x86_64 --> Processing Dependency: libpng12.so.0()(64bit) for package: zvbi-0.2.33-2.el6.rf.x86_64 --> Running transaction check ---> Package libpng12.x86_64 0:1.2.50-10.el7 will be installed --> Finished Dependency Resolution Dependencies Resolved =============================================================================================================== Package Arch Version Repository Size =============================================================================================================== Installing: zvbi x86_64 0.2.33-2.el6.rf rpmforge 867 k Installing for dependencies: libpng12 x86_64 1.2.50-10.el7 base 171 k Transaction Summary =============================================================================================================== Install 1 Package (+1 Dependent package) Total download size: 1.0 M Installed size: 4.0 M Background downloading packages, then exiting: (1/2): libpng12-1.2.50-10.el7.x86_64.rpm | 171 kB 00:00:00 (2/2): zvbi-0.2.33-2.el6.rf.x86_64.rpm | 867 kB 00:00:01 --------------------------------------------------------------------------------------------------------------- Total 574 kB/s | 1.0 MB 00:00:01 exiting because "Download Only" specified
这样就下载下来了,那么下载下来的包到哪里去了呢?我们来找一找:
[root@ruanwenwu02 etc]# cd /var/cache/yum/x86_64/7/ base/ C7.0.1406-updates/ C7.2.1511-centosplus/ cr/ timedhosts.txt base-debuginfo/ C7.1.1503-base/ C7.2.1511-extras/ extras/ updates/ base-source/ C7.1.1503-centosplus/ C7.2.1511-fasttrack/ extras-source/ updates-source/ C7.0.1406-base/ C7.1.1503-extras/ C7.2.1511-updates/ fasttrack/ C7.0.1406-centosplus/ C7.1.1503-fasttrack/ c7-media/ .gpgkeyschecked.yum C7.0.1406-extras/ C7.1.1503-updates/ centosplus/ rpmforge/ C7.0.1406-fasttrack/ C7.2.1511-base/ centosplus-source/ timedhosts [root@ruanwenwu02 etc]# cd /var/cache/yum/x86_64/7/rpmforge/ [root@ruanwenwu02 rpmforge]# ls cachecookie gen mirrorlist.txt packages primary.sqlite.bz2 repomd.xml [root@ruanwenwu02 rpmforge]# cd packages/ [root@ruanwenwu02 packages]# ls zvbi-0.2.33-2.el6.rf.x86_64.rpm
实验发现,每个源都有自己的缓存目录,对应去找就可以了,但是这样太麻烦了,最好是下载的时候指定目录:
[root@ruanwenwu02 packages]# yum install zoo --downloadonly --downloaddir=/tmp/ Failed to set locale, defaulting to C Loaded plugins: fastestmirror, priorities Loading mirror speeds from cached hostfile * base: mirrors.aliyun.com * extras: mirrors.tuna.tsinghua.edu.cn * rpmforge: mirrors.tuna.tsinghua.edu.cn * updates: mirrors.aliyun.com 197 packages excluded due to repository priority protections Resolving Dependencies --> Running transaction check ---> Package zoo.x86_64 0:2.10-2.2.el6.rf will be installed --> Finished Dependency Resolution Dependencies Resolved =============================================================================================================== Package Arch Version Repository Size =============================================================================================================== Installing: zoo x86_64 2.10-2.2.el6.rf rpmforge 76 k Transaction Summary =============================================================================================================== Install 1 Package Total download size: 76 k Installed size: 132 k Background downloading packages, then exiting: zoo-2.10-2.2.el6.rf.x86_64.rpm | 76 kB 00:00:02 exiting because "Download Only" specified [root@ruanwenwu02 packages]# cd /tmp/ [root@ruanwenwu02 tmp]# ls 1.txt language.txt systemd-private-af36769ad0fd46b2b5b049d6d35927a3-vmtoolsd.service-WbReEr testdisk yum_save_tx.2017-11-08.21-02.aC9nNO.yumtx yum_save_tx.2017-11-08.21-12.Yg9SCW.yumtx yum_save_tx.2017-11-08.21-30.dCeEra.yumtx zoo-2.10-2.2.el6.rf.x86_64.rpm
因为篇幅的原因,这一篇就到这里为止,在下一篇里,我们将会讲到:
1 配置本地yum源。
2 安装扩展源。
3 安装国内源。
4 安装RPMForge源。