1.Linux下软件包的安装方式

Linux下(RH类似系统)软件包的安装方式主要分为三种:

①RPM安装

RPM是由红帽公司开发的软件包管理方式,使用rpm我们可以方便的进行软件的安装、查询、卸载、升级等工作。一般安装路径已经默认,安装速度较快,且出错概率很低。但是rpm软件包之间的依赖性问题往往会很繁琐,尤其是软件由多个rpm包组成时。

②yum安装

Yum(全称为 Yellow dog Updater, Modified)是一个在Fedora和RedHat以及SUSE中的Shell前端软件包管理器。基於RPM包管理,能够从指定的服务器自动下载RPM包并且安装,可以自动处理依赖性关系,并且一次安装所有依赖的软体包,无须繁琐地一次次下载、安装。

③源码安装

Linux下许多文件以源码形式存在,如果需要执行,则必须先对文件进行编译。源码包的安装一般需要手动进行,可以指定安装路径等自定义信息,但是出错率比较高。

2.RPM工具用法

例1:使用-ivh安装rpm包,-Uvh升级rpm包

i:安装

v:可视化

h:用进度条显示安装进度

U:升级rpm包

[root@localhost Packages]# rpm -ivh zip-3.0-11.el7.x86_64.rpm 
准备中...                          ################################# [100%]
正在升级/安装...
   1:zip-3.0-11.el7                   ################################# [100%]
[root@localhost Packages]# rpm -Uvh zip-3.0-11.el7.x86_64.rpm 
准备中...                          ################################# [100%]
	软件包 zip-3.0-11.el7.x86_64 已经安装
[root@localhost Packages]# rpm -iv zlib-1.2.7-17.el7.x86_64.rpm 
软件包准备中...
	软件包 zlib-1.2.7-17.el7.x86_64 已经安装
[root@localhost Packages]#

例2:卸载rpm包

-e:卸载rpm包,后面接的参数只需要rpm包名

[root@localhost Packages]# rpm -e zip

例3:查询rpm包是否已经安装

-q:查询

[root@localhost Packages]# rpm -q zip
rpm -q zip
未安装软件包 zip 
[root@localhost Packages]# rpm -ivh zip-3.0-11.el7.x86_64.rpm 
准备中...                          ################################# [100%]
正在升级/安装...
   1:zip-3.0-11.el7                   ################################# [100%]
[root@localhost Packages]# rpm -q zip
zip-3.0-11.el7.x86_64

例4:-qi查询指定包信息

[root@localhost Packages]# rpm -qi vim-enhanced
Name        : vim-enhanced
Epoch       : 2
Version     : 7.4.160
Release     : 2.el7
Architecture: x86_64
Install Date: 2017年11月10日 星期五 12时34分11秒
Group       : Applications/Editors
Size        : 2292098
License     : Vim
Signature   : RSA/SHA256, 2017年08月11日 星期五 04时15分38秒, Key ID 24c6a8a7f4a80eb5
Source RPM  : vim-7.4.160-2.el7.src.rpm
Build Date  : 2017年08月02日 星期三 08时46分12秒
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 :

例5:-ql列出包安装的文件

[root@localhost Packages]# rpm -qi vim-enhanced
Name        : vim-enhanced
Epoch       : 2
Version     : 7.4.160
Release     : 2.el7
Architecture: x86_64
Install Date: 2017年11月10日 星期五 12时34分11秒
Group       : Applications/Editors
Size        : 2292098
License     : Vim
Signature   : RSA/SHA256, 2017年08月11日 星期五 04时15分38秒, Key ID 24c6a8a7f4a80eb5
Source RPM  : vim-7.4.160-2.el7.src.rpm
Build Date  : 2017年08月02日 星期三 08时46分12秒
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 :

例6:-qf+绝对路径,查询某个文件安装于哪个rpm包

[root@localhost Packages]# rpm -qf /usr/bin/vim
vim-enhanced-7.4.160-2.el7.x86_64

3.YUM工具用法

例1:yum list 列出可以使用的rpm包

[root@localhost Packages]# yum list
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.tuna.tsinghua.edu.cn
 * extras: mirrors.zju.edu.cn
 * updates: mirrors.zju.edu.cn
已安装的软件包

例2:查看yum库的配置文件 /etc/yum.repos.d/CentOS-Base.repo

[root@localhost Packages]# ls /etc/yum.repos.d/
CentOS-Base.repo  CentOS-Debuginfo.repo  CentOS-Media.repo    CentOS-Vault.repo
CentOS-CR.repo    CentOS-fasttrack.repo  CentOS-Sources.repo
[root@localhost Packages]# cat /etc/yum.repos.d/CentOS-Base.repo 
# CentOS-Base.repo
#
# The mirror system uses the connecting IP address of the client and the
# update status of each mirror to pick mirrors that are updated to and
# geographically close to the client.  You should use this for CentOS updates
# unless you are manually picking other mirrors.
#
# If the mirrorlist= does not work for you, as a fall back you can try the 
# remarked out baseurl= line instead.
#
#

[base]
name=CentOS-$releasever - Base
mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os&infra=$infra
#baseurl=http://mirror.centos.org/centos/$releasever/os/$basearch/
gpgcheck=1
gpgkey=

例3:使用yum search查询和关键字香菇的所有rpm包

[root@localhost Packages]# yum search vim
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.tuna.tsinghua.edu.cn
 * extras: mirrors.zju.edu.cn
 * updates: mirrors.zju.edu.cn
==================================== N/S matched: 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

  名称和简介匹配 only,使用“search all”试试。

yum list配合grep命令搜索rpm包

[root@localhost 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
protobuf-vim.x86_64                         2.5.0-8.el7                base     
vim-X11.x86_64                              2:7.4.160-2.el7            base

例4:yum install 安装rpm包/yum remove 卸载rpm包(可以连带卸载依赖的包)

[root@localhost Packages]# yum install -y zip
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.tuna.tsinghua.edu.cn
 * extras: mirrors.zju.edu.cn
 * updates: mirrors.zju.edu.cn
正在解决依赖关系
--> 正在检查事务
---> 软件包 zip.x86_64.0.3.0-11.el7 将被 安装
--> 解决依赖关系完成

依赖关系解决

===========================================================================================
 Package           架构                 版本                      源                  大小
===========================================================================================
正在安装:
 zip               x86_64               3.0-11.el7                base               260 k

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

总下载量:260 k
安装大小:796 k
Downloading packages:
zip-3.0-11.el7.x86_64.rpm                                           | 260 kB  00:00:06     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在安装    : zip-3.0-11.el7.x86_64                                                  1/1 
  验证中      : zip-3.0-11.el7.x86_64                                                  1/1 

已安装:
  zip.x86_64 0:3.0-11.el7                                                                  

完毕!

例5:yum install用于安装单个文件以及解决其依赖关系。

使用yum grouplist则可以查询可以使用的软件组,groupinstall则可以安扎un个软件组。

[root@localhost Packages]# yum grouplist
Failed to set locale, defaulting to C
Loaded plugins: fastestmirror
There is no installed groups file.
Maybe run: yum groups mark convert (see man yum)
Loading mirror speeds from cached hostfile
 * base: mirrors.tuna.tsinghua.edu.cn
 * extras: mirrors.zju.edu.cn
 * updates: mirrors.zju.edu.cn
Available Environment Groups:
   Minimal Install
   Compute Node
   Infrastructure Server
   File and Print Server
   Basic Web Server
   Virtualization Host
   Server with GUI
   GNOME Desktop
   KDE Plasma Workspaces
   Development and Creative Workstation
Available Groups:
   Compatibility Libraries
   Console Internet Tools
   Development Tools
   Graphical Administration Tools
   Legacy UNIX Compatibility
   Scientific Support
   Security Tools
   Smart Card Support
   System Administration Tools
   System Management
Done
[root@localhost Packages]# yum groupinstall -y GNOME Desktop

4.搭建本地yum库

①备份配置文件

[root@localhost Packages]# cp -r /etc/yum.repos.d /etc/yum.repos.d.bak

②清空所有repo配置文件

[root@localhost yum.repos.d.bak]# cd /etc/yum.repos.d
[root@localhost yum.repos.d]# rm -rf ./*.repo

③创建dvd.repo文件,并将以下内容写进文件

[root@localhost yum.repos.d]# vi dvd.repo

[dvd]                      #库名
name=install dvd           #描述信息
baseurl=        #库的链接
enable=1                   #库是否可用,1可用,0不可用
gpcheck=0                  #是否检查,1检查,0不检查

④清除yum缓存

[root@localhost yum.repos.d]# yum clean all
Failed to set locale, defaulting to C
Loaded plugins: fastestmirror
Cleaning repos: 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

使用yum list 查询可用的rpm包,可见,可用的rpm包均来自于dvd库

Available Packages
NetworkManager-adsl.x86_64                     1:1.8.0-9.el7                      dvd      
NetworkManager-bluetooth.x86_64                1:1.8.0-9.el7                      dvd      
NetworkManager-wwan.x86_64                     1:1.8.0-9.el7                      dvd      
aide.x86_64                                    0.15.1-13.el7                      dvd      
avahi-autoipd.x86_64                           0.6.31-17.el7                      dvd      
bluez-libs.x86_64                              5.44-2.el7                         dvd      
coolkey.x86_64                                 1.1.0-37.el7                       dvd      
dracut-fips.x86_64                             033-502.el7                        dvd

5.更换国内yum仓库源

可以使用wget或者curl命令下载国内源,然后替换/etc/yum.repos.d/CentOS-Base.repo即可

6.安装扩展源

当base源内包不足时,需要使用yum install 安装epel扩展源epel-release

[root@localhost etc]# yum install -y epel-release
Failed to set locale, defaulting to C
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.shuosc.org
 * updates: mirrors.shuosc.org
Resolving Dependencies
--> Running transaction check
---> Package epel-release.noarch 0:7-9 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

===========================================================================================
 Package                   Arch                Version           Repository           Size
===========================================================================================
Installing:
 epel-release              noarch              7-9               extras               14 k

Transaction Summary
===========================================================================================
Install  1 Package

Total download size: 14 k
Installed size: 24 k
Downloading packages:
epel-release-7-9.noarch.rpm                                         |  14 kB  00:00:05     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : epel-release-7-9.noarch                                                 1/1 
  Verifying  : epel-release-7-9.noarch                                                 1/1 

Installed:
  epel-release.noarch 0:7-9                                                                

Complete!

安装完成后etc/yum.repos.d/下的文件回到多处出两个,其中epel.repo为核心的配置文件

[root@localhost yum.repos.d]# ls
CentOS-Base.repo       CentOS-Media.repo    CentOS-fasttrack.repo
CentOS-CR.repo         CentOS-Sources.repo  epel-testing.repo
CentOS-Debuginfo.repo  CentOS-Vault.repo    epel.repo

7.下载RPM包

使用yum的--download插件可以下载rpm文件,但不直接安装

--downloaddir=绝对路径可以制定下载目录,否则默认路径为/var/cache/yum/x86_64/7/

[root@localhost yum.repos.d]# rpm -q zsh
package zsh is not installed
[root@localhost yum.repos.d]# yum install zsh --downloadonly  --downloaddir=/tmp/
Failed to set locale, defaulting to C
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * epel: mirror.ehost.vn
 * extras: mirrors.shuosc.org
 * updates: mirrors.shuosc.org
Resolving Dependencies
--> Running transaction check
---> Package zsh.x86_64 0:5.0.2-28.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

===========================================================================================
 Package          Arch                Version                      Repository         Size
===========================================================================================
Installing:
 zsh              x86_64              5.0.2-28.el7                 base              2.4 M

Transaction Summary
===========================================================================================
Install  1 Package

Total download size: 2.4 M
Installed size: 5.6 M
Background downloading packages, then exiting:
zsh-5.0.2-28.el7.x86_64.rpm                                         | 2.4 MB  00:00:05     
exiting because "Download Only" specified
[root@localhost yum.repos.d]# ls /tmp
1.txt
ks-script-i3ssZb
systemd-private-9bddb1e7e2284ed49533fb912be03178-chronyd.service-ERU7So
systemd-private-ffa4448671884a4698166a44413216de-chronyd.service-2gtbva
yum.log
yum_save_tx.2017-11-19.22-40.UQoOGE.yumtx
zsh-5.0.2-28.el7.x86_64.rpm

8.源码包安装

①将下载好的源码包解压,并进入该目录。

②执行./configure --prefix=绝对路径 →安装源码包,并指定路径

③编译文件make

④make install 将变异好的文件放入指定的prefix目录下

⑤卸载文件只需要将目录下所有文件删除即可