Linux下安装软件包的三种方法

  1. rpm工具
  2. yum工具
  3. 源码包安装

yum是Redhat所特有的安装RPM程序包的工具,使用起来相当方便。因为使用RPM安装某一个程序包有可能会因为该程序包依赖另一个程序包而无法安装,而使用yum工具就可以连同依赖的程序包一起安装。CentOS同样可以使用yum工具,而且在CentOS中可以免费使用yum,但Redhat中只有付费后才能使用yum,默认是无法使用yum的。

RPM工具

RPM是“Redhat Package Manager”的缩写。RPM是以一种数据库记录的方式来将所需要的套件安装到Linux主机的一套管理程序。Linux系统中存在着一个关于RPM的数据库,它记录了安装的包以及包与包之间依赖相关性。RPM包是预先在Linux机器上编译好并打包好的文件,安装起来非常快捷。但是也有一些缺点,比如安装的环境必须与编译时的环境一致或者相当;包与包之间存在着相互依赖的情况;卸载包时需要先把依赖的包卸载掉,如果依赖的包是系统所必须的,那就不能卸载这个包,否则会造成系统崩溃。RHEL、CentOS、SUSE系统都可以使用rpm包。


在打开的Vmware虚拟机中,点击右下方的光驱形状小图标,选择“连接”,将系统安装盘连接到Linux系统中。

点击右下方的光驱形状小图标

  • 把光驱挂载到/mnt目录下
[root@centos-01 mytest]# df -h
文件系统        容量  已用  可用 已用% 挂载点
/dev/sda3        28G  1.3G   27G    5% /
devtmpfs        479M     0  479M    0% /dev
tmpfs           489M     0  489M    0% /dev/shm
tmpfs           489M  6.7M  482M    2% /run
tmpfs           489M     0  489M    0% /sys/fs/cgroup
/dev/sda1       197M   97M  100M   50% /boot
tmpfs            98M     0   98M    0% /run/user/0
[root@centos-01 mytest]# ls /mnt/
[root@centos-01 mytest]# mount /dev/cdrom /mnt/
mount: /dev/sr0 写保护,将以只读方式挂载
[root@centos-01 mytest]# cd /mnt/
[root@centos-01 mnt]# ls
CentOS_BuildTag  GPL       LiveOS    RPM-GPG-KEY-CentOS-7
EFI              images    Packages  RPM-GPG-KEY-CentOS-Testing-7
EULA             isolinux  repodata  TRANS.TBL
[root@centos-01 mnt]# cd Packages/

在/mnt/Packages目录下查看到很多.rpm文件,这就是RPM包。rpm包格式:包名-版本号-发布版本号-平台.rpm。

安装rpm包

  • “-i”:安装
  • “-v”:可视化
  • “-h”:显示安装进度
  • --force”:强制安装,即使覆盖属于其他包的文件也要安装
  • --nodeps”:当要安装的rpm包依赖其他包时,即使其他包没有安装,也要安装这个包
[root@centos-01 Packages]# rpm -ivh zsh-5.0.2-28.el7.x86_64.rpm

升级rpm包

  • “-U”:升级

命令rpm -Uvh filename

卸载rpm包

命令rpm -e filename

这里的filename是rpm包名。卸载时后边跟的filename和安装时的是有区别的,安装时是把一个存在的文件作为参数,而卸载时只需要包名即可。

[root@centos-01 Packages]# rpm -e zsh

查询当前系统所有安装过的rpm包

[root@centos-01 Packages]# rpm -qa

查询rpm包是否安装

命令:rpm -q 包名

[root@centos-01 Packages]# rpm -q vim
未安装软件包 vim 
[root@centos-01 Packages]# rpm -q vim-enhanced
vim-enhanced-7.4.160-2.el7.x86_64

查询已安装rpm包的相关信息

命令:rpm -qi 包名

[root@centos-01 Packages]# rpm -qi vim-enhanced
Name        : vim-enhanced
Epoch       : 2
Version     : 7.4.160
Release     : 2.el7
Architecture: x86_64
Install Date: 20180422日 星期日 153956Group       : Applications/Editors
Size        : 2292098
License     : Vim
Signature   : RSA/SHA256, 20170811日 星期五 041538秒, Key ID 24c6a8a7f4a80eb5
Source RPM  : vim-7.4.160-2.el7.src.rpm
Build Date  : 20170802日 星期三 084612秒
Build Host  : c1bm.rdu2.centos.org
Relocations : (not relocatable)
Packager    : CentOS BuildSystem <http://bugs.centos.org>
Vendor      : CentOS
URL         : http://www.vim.org/
Summary     : A version of the VIM editor which includes recent enhancements
Description :
VIM (VIsual editor iMproved) is an updated and improved version of the
vi editor.  Vi was the first real screen-based editor for UNIX, and is
still very popular.  VIM improves on vi by adding new features:
multiple windows, multi-level undo, block highlighting and more.  The
vim-enhanced package contains a version of VIM with extra, recently
introduced features like Python and Perl interpreters.

Install the vim-enhanced package if you'd like to use a version of the
VIM editor which includes recently added enhancements like
interpreters for the Python and Perl scripting languages.  You'll also
need to install the vim-common package.

列出rpm包安装的文件

命令:rpm -ql 包名

[root@centos-01 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

查看某一个文件是由哪个包安装

命令:rpm -qf 文件的绝对路径

[root@centos-01 Packages]# which vim
/usr/bin/vim
[root@centos-01 Packages]# rpm -qf /usr/bin/vim
vim-enhanced-7.4.160-2.el7.x86_64
[root@centos-01 Packages]# rpm -qf `which vim`
vim-enhanced-7.4.160-2.el7.x86_64

yum工具

yum工具比rpm工具好用多了,当然前提是当前使用的Linux系统是支持yum的。yum最大的优势在于可以联网去下载所需要的rpm包,然后自动安装,在这个过程中如果要安装的rpm包有依赖关系,yum会解决掉这些依赖关系依次安装所有rpm包。

列出所有可用的rpm包

[root@centos-01 Packages]# yum list

搜索rpm包

[root@centos-01 Packages]# yum search vim
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * epel: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
===================================== N/S matched: vim =====================================
beakerlib-vim-syntax.noarch : Files for syntax highlighting BeakerLib tests in VIM editor
fluxbox-vim-syntax.noarch : Fluxbox syntax scripts for vim
neovim.x86_64 : Vim-fork focused on extensibility and agility
protobuf-vim.x86_64 : Vim syntax highlighting for Google Protocol Buffers descriptions
python2-neovim.noarch : Python client to Neovim
python34-neovim.noarch : Python client to Neovim
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-go.x86_64 : Go development plugin for Vim
vim-gtk-syntax.noarch : Vim syntax highlighting for GLib, Gtk+, Gstreamer, and more
vim-jellybeans.noarch : A colorful, dark color scheme for Vim
vim-minimal.x86_64 : A minimal version of the VIM editor
vim-toml.noarch : Vim syntax for TOML
vim-vimoutliner.noarch : Script for building an outline editor on top of Vim
xtuple-csvimp-devel.x86_64 : CSVImp development files
vim-fugitive.noarch : A Git wrapper so awesome, it should be illegal
vim-halibut.noarch : Syntax file for the halibut manual tool
xtuple-csvimp.x86_64 : xTuple data import utility

  名称和简介匹配 only,使用“search all”试试。
[root@centos-01 Packages]# yum list |grep 'vim'
vim-common.x86_64                         2:7.4.160-2.el7              @base    
vim-enhanced.x86_64                       2:7.4.160-2.el7              @base    
vim-filesystem.x86_64                     2:7.4.160-2.el7              @base    
vim-minimal.x86_64                        2:7.4.160-2.el7              @anaconda
beakerlib-vim-syntax.noarch               1.17-13.el7                  epel     
fluxbox-vim-syntax.noarch                 1.3.7-1.el7                  epel     
neovim.x86_64                             0.2.0-2.el7                  epel     
protobuf-vim.x86_64                       2.5.0-8.el7                  base     
python2-neovim.noarch                     0.1.13-3.el7                 epel     
python34-neovim.noarch                    0.1.13-3.el7                 epel     
vim-X11.x86_64                            2:7.4.160-2.el7              base     
vim-fugitive.noarch                       2.2-8.el7                    epel     
vim-go.x86_64                             1.8-3.el7                    epel     
vim-gtk-syntax.noarch                     20130716-1.el7               epel     
vim-halibut.noarch                        1.2-1.el7                    epel     
vim-jellybeans.noarch                     1.6-1.el7                    epel     
vim-toml.noarch                           0-0.1.20180306git624f024.el7 epel     
vim-vimoutliner.noarch                    0.4.0-8.el7                  epel     
xtuple-csvimp.x86_64                      0.5.4-8.el7                  epel     
xtuple-csvimp-devel.x86_64                0.5.4-8.el7                  epel     

安装rpm包

命令:yum install [-y] [rpm包名]

如果不加“-y”选项,则会以与用户交互的方式安装,首先是列出需要安装的rpm包信息,然后会问用户是否需要安装,输入y则安装,输入n则不安装。加上“-y”选项,这样就省略掉了询问用户是否安装的那一步。

列出系统中以组安装的包(套件)

[root@centos-01 Packages]# yum grouplist
[root@centos-01 Packages]# LANG=en
[root@centos-01 Packages]# yum grouplist

在列出的组中,选择安装Virtualization Host

[root@centos-01 Packages]# yum groupinstall "Virtualization Host"

卸载rpm包

命令:yum remove [-y] [rpm包名]

卸载某个rpm包一定要看清楚了,不要连其他重要的rpm包一起卸载了,以免影响正常的业务。

升级rpm包

命令:yum update [-y] [rpm包]

列出软件包提供哪些文件

[root@centos-01 Packages]# yum provides "/*/vim"

Failed to set locale, defaulting to C

前面设置成en英文后,出现如下错误

Failed to set locale, defaulting to C

再设置回中文即可

[root@centos-01 Packages]# LANG=zh_CN.UTF-8

yum搭建本地仓库

前面在介绍RPM工具时,已经挂载光驱到/mnt目录下。如果当前虚拟机没有挂载,请参照RPM工具开头部分操作。

[root@centos-01 Packages]# ls /mnt/
CentOS_BuildTag  GPL       LiveOS    RPM-GPG-KEY-CentOS-7
EFI              images    Packages  RPM-GPG-KEY-CentOS-Testing-7
EULA             isolinux  repodata  TRANS.TBL
[root@centos-01 Packages]# cd
[root@centos-01 ~]# cp -r /etc/yum.repos.d /etc/yum.repos.d.bak
[root@centos-01 ~]# cd /etc/yum.repos.d
[root@centos-01 yum.repos.d]# ls
CentOS.repo  epel.repo
[root@centos-01 yum.repos.d]# rm -rf ./*
[root@centos-01 yum.repos.d]# vi dvd.repo

添加

[dvd]
name=install dvd
baseurl=file:///mnt
enable=1
gpgcheck=0

保存退出

[root@centos-01 yum.repos.d]# yum clean all
已加载插件:fastestmirror
正在清理软件源: dvd
Cleaning up everything
Maybe you want: rm -rf /var/cache/yum, to also free up space taken by orphaned data from disabled or removed repos
Cleaning up list of fastest mirrors
[root@centos-01 yum.repos.d]# yum list

更换yum仓库源

[root@centos-01 yum.repos.d]# ls
dvd.repo
[root@centos-01 yum.repos.d]# rm -f dvd.repo

使用wget或者curl命令下载CentOS7-Base-163.repo

[root@centos-01 yum.repos.d]# wget http://mirrors.163.com/.help/CentOS7-Base-163.repo
[root@centos-01 yum.repos.d]# curl -O http://mirrors.163.com/.help/CentOS7-Base-163.repo
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  1572  100  1572    0     0   8395      0 --:--:-- --:--:-- --:--:--  8451
[root@centos-01 yum.repos.d]# ls
CentOS7-Base-163.repo
[root@centos-01 yum.repos.d]# yum list
[root@centos-01 yum.repos.d]# yum clean all
[root@centos-01 yum.repos.d]# yum repolist all

安装扩展源epel

[root@centos-01 yum.repos.d]# yum install -y epel-release
[root@centos-01 yum.repos.d]# ls
CentOS7-Base-163.repo  epel.repo  epel-testing.repo
[root@centos-01 yum.repos.d]# yum list
[root@centos-01 yum.repos.d]# yum list |grep epel
[root@centos-01 yum.repos.d]# yum repolist all

yum下载rpm包

yum install 包名 --downloadonly
[root@centos-01 yum.repos.d]# ls /var/cache/yum/x86_64/7/
base        epel            epel-testing            extras          updates
centosplus  epel-debuginfo  epel-testing-debuginfo  timedhosts
dvd         epel-source     epel-testing-source     timedhosts.txt

/var/cache/yum/x86_64/7/目录下,有不同源对应的文件夹。yum下载的rpm包,会根据下载源存放在对应源的/packages目录下,比如:/var/cache/yum/x86_64/7/updates/packages//var/cache/yum/x86_64/7/base/packages/

指定下载rpm路径

yum install 包名 --downloadonly --downloaddir=路径

下载已经安装过的rpm包

yum reinstall 包名 --downloadonly --downloaddir=路径
[root@centos-01 yum.repos.d]# yum install vim-enhanced --downloadonly --downloaddir=/tmp/
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
 * epel: mirror01.idc.hinet.net
软件包 2:vim-enhanced-7.4.160-2.el7.x86_64 已安装并且是最新版本
无须任何处理
[root@centos-01 yum.repos.d]# ls /tmp/
1.txt      systemd-private-2186b3ce9fed4170938e51d9b57ddad3-chronyd.service-i763QZ
2.txt.gz   systemd-private-2186b3ce9fed4170938e51d9b57ddad3-vgauthd.service-Q0qSiA
3.txt.bz2  systemd-private-2186b3ce9fed4170938e51d9b57ddad3-vmtoolsd.service-saunXb
4.txt.xz   yum_save_tx.2018-05-05.19-44.not9LG.yumtx
mytest
[root@centos-01 yum.repos.d]# yum reinstall vim-enhanced --downloadonly --downloaddir=/tmp/
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
 * epel: mirrors.aliyun.com
正在解决依赖关系
--> 正在检查事务
---> 软件包 vim-enhanced.x86_64.2.7.4.160-2.el7 将被 已重新安装
--> 解决依赖关系完成

依赖关系解决

======================================================================================================
 Package                   架构                版本                           源                 大小
======================================================================================================
重新安装:
 vim-enhanced              x86_64              2:7.4.160-2.el7                base              1.0 M

事务概要
======================================================================================================
重新安装  1 软件包

总下载量:1.0 M
安装大小:2.2 M
Background downloading packages, then exiting:
vim-enhanced-7.4.160-2.el7.x86_64.rpm                                          | 1.0 MB  00:00:00     
exiting because "Download Only" specified
[root@centos-01 yum.repos.d]# ls /tmp/
1.txt
2.txt.gz
3.txt.bz2
4.txt.xz
mytest
systemd-private-2186b3ce9fed4170938e51d9b57ddad3-chronyd.service-i763QZ
systemd-private-2186b3ce9fed4170938e51d9b57ddad3-vgauthd.service-Q0qSiA
systemd-private-2186b3ce9fed4170938e51d9b57ddad3-vmtoolsd.service-saunXb
vim-enhanced-7.4.160-2.el7.x86_64.rpm
yum_save_tx.2018-05-05.19-44.not9LG.yumtx
yum_save_tx.2018-05-05.21-20.Iue83X.yumtx

源码包安装

[root@centos-01 yum.repos.d]# cd
[root@centos-01 ~]# cd /usr/local/src/
[root@centos-01 src]# ls
[root@centos-01 src]# wget http://archive.apache.org/dist/httpd/httpd-2.2.9.tar.gz
[root@centos-01 src]# ls
httpd-2.2.9.tar.gz
[root@centos-01 src]# tar -zxvf httpd-2.2.9.tar.gz
[root@centos-01 src]# ls
httpd-2.2.9  httpd-2.2.9.tar.gz
[root@centos-01 src]# cd httpd-2.2.9
[root@centos-01 httpd-2.2.9]# ls
ABOUT_APACHE      BuildBin.dsp   docs         InstallBin.dsp  modules           ROADMAP
acinclude.m4      buildconf      emacs-style  LAYOUT          NOTICE            server
Apache.dsw        CHANGES        httpd.dsp    libhttpd.dsp    NWGNUmakefile     srclib
apachenw.mcp.zip  config.layout  httpd.spec   LICENSE         os                support
build             configure      include      Makefile.in     README            test
BuildAll.dsp      configure.in   INSTALL      Makefile.win    README.platforms  VERSIONING
[root@centos-01 httpd-2.2.9]# ./configure --prefix=/usr/local/apache2
checking for chosen layout... Apache
checking for working mkdir -p... yes
checking build system type... x86_64-unknown-linux-gnu
checking host system type... x86_64-unknown-linux-gnu
checking target system type... x86_64-unknown-linux-gnu

Configuring Apache Portable Runtime library ...

checking for APR... reconfig
configuring package in srclib/apr now
checking build system type... x86_64-unknown-linux-gnu
checking host system type... x86_64-unknown-linux-gnu
checking target system type... x86_64-unknown-linux-gnu
Configuring APR library
Platform: x86_64-unknown-linux-gnu
checking for working mkdir -p... yes
APR Version: 1.3.0
checking for chosen layout... apr
checking for gcc... no
checking for cc... no
checking for cl.exe... no
configure: error: no acceptable C compiler found in $PATH
See `config.log' for more details.
configure failed for srclib/apr [root@centos-01 httpd-2.2.9]# echo $?
1

echo $?显示非零值,说明上一条命令是错的,需要安装gcc。

源码包的编译用到了Linux系统里的编译器,常见的源码包一般都是用C语言开发的,这也是因为C语言为Linux上最标准的程序语言。Linux上的C语言编译器叫做gcc。

[root@centos-01 httpd-2.2.9]# yum install gcc

安装一个源码包,通常需要三个步骤:

  1. ./configure
    在这一步可以定制功能,加上相应的选项即可,具体有说明选项可以通过./configure --help命令查看。在这一步会自动检测Linux系统与相关的套件是否有编译该源码包时需要的库,因为一旦缺少某个库就不能完成编译。只有检测通过后才会生成一个Makefile文件。
  2. make
    使用这个命令会根据Makefile文件中预设的参数进行编译,这一步其实就是gcc在工作了。
  3. make install
    安装步骤,生成相关的软件存放目录和配置文件的过程。

上面的三步,并不是所有的源码包软件都一样的。拿到源码包解压后,进入到目录找相关的帮助文档。通常会以INSTALL或者README为文件名。

[root@centos-01 httpd-2.2.9]# ./configure --prefix=/usr/local/apache2
[root@centos-01 httpd-2.2.9]# echo $?
0
[root@centos-01 httpd-2.2.9]# make
[root@centos-01 httpd-2.2.9]# echo $?
0
[root@centos-01 httpd-2.2.9]# make install
[root@centos-01 httpd-2.2.9]# echo $?
0
[root@centos-01 httpd-2.2.9]# ls /usr/local/apache2/
bin  build  cgi-bin  conf  error  htdocs  icons  include  lib  logs  man  manual  modules

源码包卸载,就是删除安装的文件夹即可。源码包安装完后,没有启动或者有其他操作,此时删除指定的文件夹即可。