Linux基础知识之安装RPM包或源码包

2020/03/13
第六课

第九章:安装RPM包或源码包

前面我们多次提到的yum命令,它是Red Hat所特有的安装RPM程序包的工具。使用RPM工具安装某一个程序包时,有可能会因为该程序包依赖另一个程序包而无法安装;而使用yum工具时,就可以连同依赖的程序包一起安装。

9.1 RPM工具

RPM它是以一种数据库记录的方式将我们所需要的套件安装到Linux主机的一套管理程序。也就是说,Linux系统中存在着一个关于RPM的数据库,记录了安装的包以及包与包之间的依赖关系。

  • 缺点:安装环境必须与编译时的环境一致或者相当,包与包之间存在着相互依赖的情况,卸载包时需要先把依赖的包卸载。如果依赖的包是系统所必需的,就不能卸载这个包,否则系统会崩溃。

把光驱挂载到/mnt目录下,先检查一下VMware右下角的小光驱图标是否点亮,如果没有,点击一下,再点击“连接”就可以点亮它,光驱挂载后,会在/mnt/Packages目录下看到很多后缀为.rpm的文件,这些文件就是RPM包。

[root@xxj-could ~]# mount /dev/sr0 /mnt
mount: /dev/sr0 is write-protected, mounting read-only
[root@xxj-could ~]# 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@xxj-could ~]# ls /mnt/Packages/|head
389-ds-base-1.3.4.0-19.el7.x86_64.rpm
389-ds-base-libs-1.3.4.0-19.el7.x86_64.rpm
abattis-cantarell-fonts-0.0.16-3.el7.noarch.rpm
abrt-2.1.11-36.el7.centos.x86_64.rpm
abrt-addon-ccpp-2.1.11-36.el7.centos.x86_64.rpm
abrt-addon-kerneloops-2.1.11-36.el7.centos.x86_64.rpm
abrt-addon-pstoreoops-2.1.11-36.el7.centos.x86_64.rpm
abrt-addon-python-2.1.11-36.el7.centos.x86_64.rpm
abrt-addon-vmcore-2.1.11-36.el7.centos.x86_64.rpm
abrt-addon-xorg-2.1.11-36.el7.centos.x86_64.rpm
[root@xxj-could ~]#
9.1.1 安装RPM包

格式:rpm -ivh filename。

[root@xxj-could Packages]# rpm -ivh libjpeg-turbo-    devel-1.2.90-5.el7.x86_64.rpm --nodeps --force
Preparing...                          ################################# [100%]
Updating / installing...
   1:libjpeg-turbo-devel-1.2.90-5.el7     ################################# [100%]
[root@xxj-could Packages]#
  • -i:表示安装。
  • -v:表示可视化。
  • -h:表示显示安装进度。
  • –force:表示强制安装,即使覆盖属于其他包的文件也要安装。
  • –nodeps:表示当要安装的RPM包依赖于其他包时,即使其他包没有安装,也要安装这个包。
9.1.2 升级RPM包

格式:rpm -Uvh filename,其中-U选项表示升级。

9.1.3 卸载RPM包

格式:rpm -e filename ,这里的filename是通过rpm的查询功能所查询到的包名。

[root@xxj-could Packages]# rpm -qa libjpeg-turbo-devel
libjpeg-turbo-devel-1.2.90-5.el7.x86_64
[root@xxj-could Packages]# rpm -e libjpeg-turbo-devel
[root@xxj-could Packages]# rpm -qa libjpeg-turbo-devel
[root@xxj-could Packages]#  
9.1.4 查询一个包是否已安装

格式:rpm -q rpm包名,(这里的“包名”是不带平台信息和后缀名的)

[root@xxj-could Packages]#  rpm -q libjpeg-turbo-devel
package libjpeg-turbo-devel is not installed
[root@xxj-could Packages]# rpm -ivh libjpeg-turbo-devel-1.2.90-5.el7.x86_64.rpm --nodeps --force
Preparing...                          ################################# [100%]
Updating / installing...
   1:libjpeg-turbo-devel-1.2.90-5.el7 ################################# [100%]
[root@xxj-could Packages]# rpm -q libjpeg-turbo-devel
libjpeg-turbo-devel-1.2.90-5.el7.x86_64
[root@xxj-could Packages]# 

可以使用命令rpm -qa查询当前系统所有已安装的RPM包:

[root@xxj-could Packages]# rpm -qa | head
grub2-2.02-0.29.el7.centos.x86_64
centos-release-7-2.1511.el7.centos.2.10.x86_64
audit-2.4.1-5.el7.x86_64
filesystem-3.2-20.el7.x86_64
rdma-7.2_4.1_rc6-1.el7.noarch
kbd-misc-1.15.5-11.el7.noarch
microcode_ctl-2.1-12.el7.x86_64
linux-firmware-20150904-                    43.git6ebf5d5.el7.noarch
openssh-server-6.6.1p1-22.el7.x86_64
nss-softokn-freebl-3.16.2.3-13.el7_1.x86_64
[root@xxj-could Packages]# 
9.1.5 得到一个已安装的RPM包的相关信息

格式:rpm -qi rpm包名,(这里的“包名”是不带平台信息和后缀名的)

[root@xxj-could Packages]# rpm -qi libjpeg-turbo-devel
Name        : libjpeg-turbo-devel
Version     : 1.2.90
Release     : 5.el7
Architecture: x86_64
Install Date: Sat 14 Mar 2020 10:17:26 AM EDT
Group       : Development/Libraries
Size        : 321621
License     : IJG
Signature   : RSA/SHA256, Thu 03 Jul 2014 10:55:32 PM EDT, Key ID 24c6a8a7f4a80eb5
Source RPM  : libjpeg-turbo-1.2.90-5.el7.src.rpm
Build Date  : Mon 09 Jun 2014 11:48:52 PM EDT
Build Host  : worker1.bsys.centos.org
Relocations : (not relocatable)
Packager    : CentOS BuildSystem <http://bugs.centos.org>
Vendor      : CentOS
URL         : http://sourceforge.net/projects/libjpeg-turbo
Summary     : Headers for the libjpeg-turbo library
Description :
This package contains header files necessary for developing programs which
will manipulate JPEG files using the libjpeg-turbo library.
[root@xxj-could Packages]#
9.1.6 列出一个RPM包的安装文件

格式:rpm -ql rpm包名

[root@xxj-could Packages]# rpm -ql libjpeg-turbo-devel
/usr/include/jconfig.h
/usr/include/jerror.h
/usr/include/jmorecfg.h
/usr/include/jpeglib.h
/usr/lib64/libjpeg.so
/usr/share/doc/libjpeg-turbo-devel-1.2.90
/usr/share/doc/libjpeg-turbo-devel-1.2.90/coderules.txt
/usr/share/doc/libjpeg-turbo-devel-1.2.90/example.c
/usr/share/doc/libjpeg-turbo-devel-1.2.90/jconfig.txt
/usr/share/doc/libjpeg-turbo-devel-1.2.90/libjpeg.txt
/usr/share/doc/libjpeg-turbo-devel-1.2.90/structure.txt
[root@xxj-could Packages]# 
9.1.7 列出某个文件属于哪个RPM包

格式:rpm -qf [文件的绝对路径]

[root@xxj-could Packages]# rpm -qf /usr/lib64/libjpeg.so
libjpeg-turbo-devel-1.2.90-5.el7.x86_64
[root@xxj-could Packages]# 

9.2 yum工具

Linux中,yum工具比RPM工具更加方便。yum工具最大的优势在于可以联网去下载所需要的RPM包,然后自动安装。如果要安装的RPM包有依赖关系,yum工具会帮我们依次安装所有相关的RPM包。

9.2.1 列出所有可用的RPM包

格式:yum list

[root@xxj-could Packages]# yum list | head -n 20
Loaded plugins: fastestmirror
Determining fastest mirrors
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.nju.edu.cn
Installed Packages
NetworkManager.x86_64                       1:1.0.6-27.el7             @anaconda
NetworkManager-libnm.x86_64                 1:1.0.6-27.el7             @anaconda
NetworkManager-team.x86_64                  1:1.0.6-27.el7             @anaconda
NetworkManager-tui.x86_64                   1:1.0.6-27.el7             @anaconda
acl.x86_64                                  2.2.51-12.el7              @anaconda
aic94xx-firmware.noarch                     30-6.el7                   @anaconda
alsa-firmware.noarch                        1.0.28-2.el7               @anaconda
alsa-lib.x86_64                             1.0.28-2.el7               @anaconda
alsa-tools-firmware.x86_64                  1.0.28-2.el7               @anaconda
audit.x86_64                                2.4.1-5.el7                @anaconda
audit-libs.x86_64                           2.4.1-5.el7                @anaconda
authconfig.x86_64                           6.2.8-10.el7               @anaconda
avahi-autoipd.x86_64                        0.6.31-15.el7              @anaconda
avahi-libs.x86_64                           0.6.31-15.el7              @anaconda


Exiting on Broken Pipe
[root@xxj-could Packages]# 

有mirrors.nju.edu.cn信息出现,说明RPM包资源是从这里下载的;
从上面例子可以看到:最左侧是RPM包名,中间是版本信息,最右侧是安装信息;
其中安装信息@base、@updates或@anaconda是已安装;base或anaconda是未安装;updates是RPM包已安装但需要升级。

9.2.2 搜索RPM包

格式:yum search [相关关键词]

[root@xxj-could ~]# yum search vim 
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.nju.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

  Name and summary matches only, use "search all" for everything.
[root@xxj-could ~]# 

还可以用grep来过滤,找到相应的RPM包;

[root@xxj-could ~]# yum list | grep 'vim'
vim-common.x86_64                           2:7.4.629-6.el7            @base    
vim-enhanced.x86_64                         2:7.4.629-6.el7            @base    
vim-filesystem.x86_64                       2:7.4.629-6.el7            @base    
vim-minimal.x86_64                          2:7.4.160-1.el7            @anaconda
protobuf-vim.x86_64                         2.5.0-8.el7                base     
vim-X11.x86_64                              2:7.4.629-6.el7            base     
vim-minimal.x86_64                          2:7.4.629-6.el7            base     
[root@xxj-could ~]# 
9.2.3 安装RPM包

格式:yum install [-y] [RPM包名]。如果不加-y选项,则会以与用户交互的方式安装。
在这里插入图片描述

9.2.4 卸载RPM包

格式:yum remove [-y] [RPM包名]
使用yum remove命令卸载时最好不要加-y选项,不要连同其他重要的RPM包也一起卸载了。
在这里插入图片描述

9.2.5 升级RPM包

格式:yum update [-y] [RPM包名]
在这里插入图片描述

9.2.6 使用本地光盘制作yum源

有时候,Linux系统不能联网,此时当然就不能很便捷地使用联网的yum源了,这时就需要我们自己在Linux系统下使用光盘制作yum源。
(1)挂载光盘

[root@xxj-could dev]# mount /dev/sr0 /mnt
mount: /dev/sr0 is write-protected, mounting read-only
[root@xxj-could dev]# 

(2)删除/etc/yum.repos.d目录下所有repo文件(删除之前最好先做一个备份)

[root@xxj-could yum.repos.d]# cp -r /etc/yum.repos.d/ /etc/yum.repos.d_bak
[root@xxj-could yum.repos.d]# rm -rf /etc/yum.repos.d/*
[root@xxj-could yum.repos.d]#

(3)创建新文件dvd.repo

[root@xxj-could yum.repos.d]# vim /etc/yum.repos.d/dvd.repo
[root@xxj-could yum.repos.d]# cat /etc/yum.repos.d/dvd.repo
[dvd]
name=install dvd
baseurl=file:///mnt
enabled=1
gpgcheck=0
[root@xxj-could yum.repos.d]# 

(4)刷新repos生成缓存

[root@xxj-could yum.repos.d]# yum makecache
Loaded plugins: fastestmirror
dvd                                                                  | 3.6 kB  00:00:00     
(1/4): dvd/filelists_db                                              | 2.9 MB  00:00:00     
(2/4): dvd/group_gz                                                  | 155 kB  00:00:00     
(3/4): dvd/other_db                                                  | 1.2 MB  00:00:00     
(4/4): dvd/primary_db                                                | 2.8 MB  00:00:00     
Loading mirror speeds from cached hostfile
Metadata Cache Created
[root@xxj-could yum.repos.d]#

然后就可以使用yum命令安装你所需要的软件包了:
在这里插入图片描述

9.2.7 利用yum工具下载RPM包

先恢复之前的yum源,否则后续使用yum还会出问题:

[root@xxj-could yum.repos.d]# rm -rf /etc/yum.repos.d
[root@xxj-could yum.repos.d]# mv /etc/yum.repos.d_bak/ /etc/yum.repos.d
[root@xxj-could yum.repos.d]# yum list

有时我们需要下载RPM包但不安装,而仅仅是复制给其他机器使用,使用yum工具就可以做到只下载而不安装:

  • 格式:yum install [包名] -y --downloadonly

这样下载了RPM包,但并没有保存到我们预想的目录下,默认会在/var/cache/yum/x86_64/7/xxx/packages(xxx指的是base、update或者extras等等)下载到指定目录的命令:

  • 格式:yum install [包名] -y --downloadonly --downloaddir=/usr/local/src
    在这里插入图片描述
    如果包已经安装过了,那么使用yum install --downloadonly 就不起作用了,它会提示已经安装过,根本就下载不下来,那么就可以使用:
  • yum reinstall -y bash --downloadonly
    在这里插入图片描述

9.3 安装源码包

在Linux下安装源码包是最常用的。安装源码包,需要我们把源代码编译成可执行的二进制文件。如果你读得懂这些源代码,就那可以修改这些源代码的自定义功能,然后再按你的需求编译。使用源码包除了可以自定义修改源代码外,还可以定制相关的功能,因为源码包在编译时可以附加额外的选项。

源码包的编译用到了linux系统里的编译器,常见的源码包一般都是用C语言开发的,因为C语言是Linux上最标准的程序语言。Linux上的C语言编译器叫做gcc,利用它可以把C语言编译成可执行的二进制文件。所以,如果你的机器上没有安装gcc,就无法编译源码。你可以使用 yum install -y gcc 来完成安装。

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

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

对于以上这3步骤,并不是所有的源码包软件都一样的,也就是说,源码包的安装并没有标准的安装步骤。这就需要你拿到源码包解压后,进入目录,找到相关的帮助文档(通常,会以INSTALL或者README为文件名)。

9.3.1 下载源码包
[root@xxj-could ~]# cd /usr/local/src/
[root@xxj-could src]# wget https://mirrors.tuna.tsinghua.edu.cn/apache/httpd/httpd-2.4.39.tar.gz
[root@xxj-could src]# ll
total 8984
-rw-r--r--. 1 root root 9199150 Mar 30  2019 httpd-2.4.39.tar.gz
[root@xxj-could src]# 
9.3.2 解压源码包
[root@xxj-could src]# tar -zxvf httpd-2.4.39.tar.gz 
9.3.3 配置相关的选项并生成Makefile
[root@xxj-could src]# cd httpd-2.4.39
[root@xxj-could httpd-2.4.39]# ./configure --help | less
`configure' configures this package to adapt to many kinds of systems.

Usage: ./configure [OPTION]... [VAR=VALUE]...

To assign environment variables (e.g., CC, CFLAGS...), specify them as
VAR=VALUE.  See below for descriptions of some of the useful variables.

Defaults for the options are specified in brackets.

Configuration:
  -h, --help              display this help and exit
  --help=short        display options specific to this package
  --help=recursive    display the short help of all the included packages
  -V, --version           display version information and exit
  -q, --quiet, --silent   do not print `checking ...' messages
  --cache-file=FILE   cache test results in FILE [disabled]
  -C, --config-cache      alias for `--cache-file=config.cache'
  -n, --no-create         do not create output files
  --srcdir=DIR        find the sources in DIR [configure dir or `..']

Installation directories:
  --prefix=PREFIX         install architecture-independent files in PREFIX
                      [/usr/local/apache2]
  --exec-prefix=EPREFIX   install architecture-dependent files in EPREFIX
                      [PREFIX]

:...skipping...
`configure' configures this package to adapt to many kinds of systems.

Usage: ./configure [OPTION]... [VAR=VALUE]...

To assign environment variables (e.g., CC, CFLAGS...), specify them as
VAR=VALUE.  See below for descriptions of some of the useful variables.

Defaults for the options are specified in brackets.

Configuration:
  -h, --help              display this help and exit
  --help=short        display options specific to this package
  --help=recursive    display the short help of all the included packages
  -V, --version           display version information and exit
  -q, --quiet, --silent   do not print `checking ...' messages
  --cache-file=FILE   cache test results in FILE [disabled]
  -C, --config-cache      alias for `--cache-file=config.cache'
  -n, --no-create         do not create output files
  --srcdir=DIR        find the sources in DIR [configure dir or `..']

Installation directories:
  --prefix=PREFIX         install architecture-independent files in PREFIX
                      [/usr/local/apache2]
  --exec-prefix=EPREFIX   install architecture-dependent files in EPREFIX
                      [PREFIX]

常用的配置选项是–prefix=PREFIX ,意思是定义软件包的安装路径;了解其他配置选项,可以使用命令 ./configure --help查看详情。

[root@xxj-could httpd-2.4.39]# yum install -y gcc    //安装gcc编译器
[root@xxj-could httpd-2.4.39]# ./configure --prefix=/usr/local/apache2
[root@xxj-could httpd-2.4.39]# echo $?      //验证是否成功
0											//0为成功
[root@xxj-could httpd-2.4.39]# ls -l Makefile
-rw-r--r--. 1 root root 8954 3月   8 06:44 Makefile
[root@xxj-could httpd-2.4.39]#    
9.3.4 进行编译

编译时间较长,会出现杂乱的信息,在编译后使用命令echo $?验证是否编译成功即可。

[root@xxj-could httpd-2.4.39]# make   //编译
Making all in srclib
make[1]: 进入目录“/usr/local/src/httpd-2.4.39/srclib”
Making all in apr
make[2]: 进入目录“/usr/local/src/httpd-2.4.39/srclib/apr”
make[3]: 进入目录“/usr/local/src/httpd-2.4.39/srclib/apr”
/usr/local/src/httpd-2.4.39/srclib/apr/build/mkdir.sh tools
...
[root@xxj-could httpd-2.4.39]# echo $?      //验证是否成功
0	
9.3.5 安装

格式:make install

[root@xxj-could httpd-2.4.39]# make install
[root@xxj-could httpd-2.4.39]# echo $?
0

执行完后则会在/usr/local/apache2目录下增加很多目录,使用ls命令查看该目录。

[root@xxj-could httpd-2.4.39]# ls /usr/local/apache2/   
bin    cgi-bin  error   icons    lib   man     modules
build  conf     htdocs  include  logs  manual
[root@xxj-could httpd-2.4.39]# 
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值