本文主要讨论了几种常见的软件包安装方法,分别是编译好的RPM包的安装、RPM源码包的安装、yum工具的使用、二进制软件包的安装,中间会涉及到具体的安装过程以及各种命令的使用。下文中的例子都是在RHEL 6.4中完成的。


常见的软件安装方式

1. 编译好的rpm包的安装

RPM的介绍

RPM是RedHat Package Manager(RedHat软件包管理工具)的缩写,这一文件格式名称虽然打上了RedHat的标志,但是其原始设计理念是开放式的,现在包括OpenLinux、S.u.S.E.以及Turbo Linux等Linux的分发版本都有采用,可以算是公认的行业标准了。


RPM软件包名的结构

name-version-release.arch.rpm


RPM的常用命令

linux的软件包安装_第1张图片


安装过程:

以安装 xv-3.10a.jumbopatch.20070520-18.el6.i686.rpm为例

[root@FLyence rpm]# ll xv-3.10a.xv-3.10a.jumbopatch.20070520-18.el6.i686.rpm
-rw-r--r--. 1 root root 533568 Oct 18  2013 xv-3.10a.xv-3.10a.jumbopatch.20070520-18.el6.i686.rpm
#
[root@FLyence rpm]# rpm -q xv
package xv is not installed      #查询有没有安装过xv
#
[root@FLyence rpm]# rpm -iv xv-3.10a.xv-3.10a.jumbopatch.20070520-18.el6.i686.rpm
warning: xv-3.10a.xv-3.10a.jumbopatch.20070520-18.el6.i686.rpm: Header V3 RSA/SHA256 Signature, key ID 5568bbb2: NOKEY
Preparing packages for installation...
xv-3.10a.jumbopatch.20070520-18.el6


2. rpm源码包的安装

以.src.rpm结尾的,这类软件包是包含了源代码的rpm包,在安装时需要进行编译。需要根据spec制作成相应arch的rpm包,再进行安装。


以安装agg-2.5-9.el6.src.rpm为例

[root@FLyence ~]# rpm -ivh agg-2.5-9.el6.src.rpm
warning: agg-2.5-9.el6.src.rpm: Header V3 RSA/SHA256 Signature, key ID 0608b895: NOKEY
   1:agg              ########################################### [100%]

之后,在家目录会生成一个rpmbuild的文件夹

[root@FLyence ~]# ls rpmbuild/
SOURCES  SPECS

进入SPECS文件夹后,

[root@FLyence SPECS]# rpmbuild -ba agg.spec
#耐心等待吧

这里-ba会同时生成编译好的rpm包和源码rpm包,-bb则只是生成编译好的rpm包。

编译好后,会多出好几个文件夹

[root@FLyence rpmbuild]# ls
BUILD  BUILDROOT  RPMS  SOURCES  SPECS  SRPMS

在RPMS文件夹中就是编译好的rpm文件,SRPMS中则为src文件

[root@FLyence rpmbuild]# ls RPMS/i686/
agg-2.5-9.el6.i686.rpm  agg-debuginfo-2.5-9.el6.i686.rpm  agg-devel-2.5-9.el6.i686.rpm
[root@FLyence rpmbuild]# ls SRPMS/
agg-2.5-9.el6.src.rpm

已编译好的rpm包就可以按上面的方法安装



3. yum工具的使用

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


下面是yum的option,见名知意,还是很简单的。

* install package1 [package2] [...]
* update [package1] [package2] [...]
* update-to [package1] [package2] [...]
* check-update
* upgrade [package1] [package2] [...]
* upgrade-to [package1] [package2] [...]
* distribution-synchronization [package1] [package2] [...]
* remove | erase package1 [package2] [...]
* list [...]
* info [...]
* provides | whatprovides feature1 [feature2] [...]
* clean [ packages | metadata | expire-cache | rpmdb | plugins | all ]
* makecache
* groupinstall group1 [group2] [...]
* groupupdate group1 [group2] [...]
* grouplist [hidden] [groupwildcard] [...]
* groupremove group1 [group2] [...]
* groupinfo group1 [...]
* search string1 [string2] [...]
* shell [filename]
* resolvedep dep1 [dep2] [...]
* localinstall rpmfile1 [rpmfile2] [...]
           (maintained for legacy reasons only - use install)
* localupdate rpmfile1 [rpmfile2] [...]
           (maintained for legacy reasons only - use update)
* reinstall package1 [package2] [...]
* downgrade package1 [package2] [...]
* deplist package1 [package2] [...]
* repolist [all|enabled|disabled]
* version [ all | installed  |  available  |  group-*  |  nogroups*  |  grouplist  |  groupinfo ]
* history [info|list|packages-list|packages-info|summary|addon-info|redo|undo|roll-back|new|sync|stats]
* load-transaction [txfile]
* check
* help [command]


例如:安装lftp这个工具

[root@FLyence rpmbuild]# yum -y install lftp
Loaded plugins: fastestmirror, refresh-packagekit, security
Loading mirror speeds from cached hostfile
Setting up Install Process
Resolving Dependencies
--> Running transaction check
---> Package lftp.i686 0:4.0.9-1.el6 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
=======================================================================================================
 Package             Arch                Version                       Repository                 Size
=======================================================================================================
Installing:
 lftp                i686                4.0.9-1.el6                   163_mirror                735 k
Transaction Summary
=======================================================================================================
Install       1 Package(s)
Total download size: 735 k
Installed size: 2.3 M
Downloading Packages:
lftp-4.0.9-1.el6.i686.rpm                                                       | 735 kB     00:03   
Running rpm_check_debug
Running Transaction Test
Transaction Test Succeeded
Running Transaction
  Installing : lftp-4.0.9-1.el6.i686                                                               1/1
  Verifying  : lftp-4.0.9-1.el6.i686                                                               1/1
Installed:
  lftp.i686 0:4.0.9-1.el6                                                                            
Complete!

yum会自动解决依赖关系,管理软件很方便


4. 二进制软件包的安装

其实,在linux下面安装一个源码包是最常见的。安装一个源码包,需要我们自己将源代码编译成二进制的可执行文件。如果你读得懂这些源代码,那么你就可以去修改这些源代码自定义功能,然后再去编译成你想要的。使用源码包的好处除了可以自定义修改源代码外还可以定制相关的功能,因为源码包在编译的时候是可以附加额外的选项的。

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

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

1. ./configure 在这一步可以定制功能,加上相应的选项即可,具有有什么选项可以通过"./configure --help"命令来查看。在这一步会自动检测你的linux系统与相关的套件是否有编译该源码包时需要的库,因为一旦缺少某个库就不能完成编译。只有检测通过后才会生成一个Makefile文件。

2. make使用这个命令会根据Makefile文件中预设的参数进行编译,这一步其实就是gcc在工作了。

3. make install 安装步骤,生成相关的软件存放目录和配置文件的过程。

上面介绍的3步并不是所有的源码包软件都一样的,笔者以前也曾经遇到过,安装步骤并不是这样,也就是说源码包的安装并非具有一定的标准安装步骤。这就需要你拿到源码包解压后,然后进入到目录找相关的帮助文档,通常会以INSTALL或者README为文件名。这个文件记得一定要看。


下面以安装htop-1.0.2.tar.gz为例

准备htop-1.0.2.tar.gz

[root@FLyence ~]# ls htop-1.0.2.tar.gz
htop-1.0.2.tar.gz

解压

[root@FLyence ~]# tar -xf htop-1.0.2.tar.gz

进入htop-1.0.2

[root@FLyence ~]# cd htop-1.0.2
[root@FLyence htop-1.0.2]# ls
aclocal.m4               ColumnsPanel.c         Header.h            Makefile.in        ScreenManager.c
Affinity.c               ColumnsPanel.h         HostnameMeter.c     MemoryMeter.c      ScreenManager.h
Affinity.h               compile                HostnameMeter.h     MemoryMeter.h      scripts
AffinityPanel.c          config.guess           htop.1              Meter.c            Settings.c
AffinityPanel.h          config.h               htop.1.in           Meter.h            Settings.h
AUTHORS                  config.h.in            htop.c              MetersPanel.c      SignalsPanel.c
autogen.sh               config.sub             htop.desktop        MetersPanel.h      SignalsPanel.h
AvailableColumnsPanel.c  configure              htop.h              missing            String.c
AvailableColumnsPanel.h  configure.ac           htop.png            NEWS               String.h
AvailableMetersPanel.c   COPYING                INSTALL             Object.c           SwapMeter.c
AvailableMetersPanel.h   CPUMeter.c             install-sh          Object.h           SwapMeter.h
BatteryMeter.c           CPUMeter.h             IOPriority.c        OpenFilesScreen.c  TasksMeter.c
BatteryMeter.h           CRT.c                  IOPriority.h        OpenFilesScreen.h  TasksMeter.h
CategoriesPanel.c        CRT.h                  IOPriorityPanel.c   Panel.c            TraceScreen.c
CategoriesPanel.h        depcomp                IOPriorityPanel.h   Panel.h            TraceScreen.h
ChangeLog                DisplayOptionsPanel.c  ListItem.c          Process.c          UptimeMeter.c
CheckItem.c              DisplayOptionsPanel.h  ListItem.h          Process.h          UptimeMeter.h
CheckItem.h              FunctionBar.c          LoadAverageMeter.c  ProcessList.c      UsersTable.c
ClockMeter.c             FunctionBar.h          LoadAverageMeter.h  ProcessList.h      UsersTable.h
ClockMeter.h             Hashtable.c            ltmain.sh           README             Vector.c
ColorsPanel.c            Hashtable.h            m4                  RichString.c       Vector.h
ColorsPanel.h            Header.c               Makefile.am         RichString.h

使用./configure -help,查看定制选项,--prefix=PREFIX,选择安装的目录

[root@FLyence htop-1.0.2]# ./configure --prefix=/usr/local/htop
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... no
checking for suffix of object files... o
checking whether we are using the GNU C compiler... yes
##等待一会儿

查看生成的Makefile文件

[root@FLyence htop-1.0.2]# ls Makefile
Makefile

使用make进行编译

[root@FLyence htop-1.0.2]# make
make  all-am
make[1]: Entering directory `/root/htop-1.0.2'
gcc -DHAVE_CONFIG_H -I.  -DNDEBUG  -pedantic -Wall -Wextra -std=c99 -rdynamic -D_XOPEN_SOURCE_EXTENDED -DSYSCONFDIR=\"/usr/local/htop/etc\" -g -O2 -MT htop-AvailableMetersPanel.o -MD -MP -MF .deps/htop-AvailableMetersPanel.Tpo -c -o htop-AvailableMetersPanel.o `test -f 'AvailableMetersPanel.c' || echo './'`AvailableMetersPanel.c
#等待

最后只要make install进行安装即可。

[root@FLyence htop-1.0.2]# make install
make  install-am
make[1]: Entering directory `/root/htop-1.0.2'
make[2]: Entering directory `/root/htop-1.0.2'
test -z "/usr/local/htop/bin" || /bin/mkdir -p "/usr/local/htop/bin"
  /bin/sh ./libtool   --mode=install /usr/bin/install -c htop '/usr/local/htop/bin'
libtool: install: /usr/bin/install -c htop /usr/local/htop/bin/htop
test -z "/usr/local/htop/share/applications" || /bin/mkdir -p "/usr/local/htop/share/applications"
 /usr/bin/install -c -m 644 htop.desktop '/usr/local/htop/share/applications'
test -z "/usr/local/htop/share/man/man1" || /bin/mkdir -p "/usr/local/htop/share/man/man1"
 /usr/bin/install -c -m 644 htop.1 '/usr/local/htop/share/man/man1'
test -z "/usr/local/htop/share/pixmaps" || /bin/mkdir -p "/usr/local/htop/share/pixmaps"
 /usr/bin/install -c -m 644 htop.png '/usr/local/htop/share/pixmaps'
make[2]: Leaving directory `/root/htop-1.0.2'
make[1]: Leaving directory `/root/htop-1.0.2'

查看安装目录,确实安装成功了

[root@FLyence htop-1.0.2]# ll /usr/local/htop/
total 8
drwxr-xr-x. 2 root root 4096 Oct  8 19:32 bin
drwxr-xr-x. 5 root root 4096 Oct  8 19:32 share

这是直接使用htop命令,却不会成功。

[root@FLyence htop-1.0.2]# htop
-bash: htop: command not found


这是因为PATH路径找不到该命令

在bash的配置文件,当然有很多,例如/etc/profile, /etc/profile.d/下的文件,~/.bash_profile, ~/.bashrc等。只要在其中找一个文件,配置依稀PATH变量即可。

vim ~/.bash_profile
  1 # .bash_profile
  2
  3 # Get the aliases and functions
  4 if [ -f ~/.bashrc ]; then
  5         . ~/.bashrc
  6 fi
  7
  8 # User specific environment and startup programs
  9
 10 PATH=$PATH:$HOME/bin:/usr/local/htop/bin
 11
 12 export PATH

然后运行htop

linux的软件包安装_第2张图片


当然,想要用man查询htop时,也是不行的,因为查找路径没有配置。

[root@FLyence htop-1.0.2]# man htop
No manual entry for htop

有两种方法:

1. 使用-M选项,指定查找目录

[root@FLyence htop-1.0.2]# man htop -M /usr/local/htop/share/man

linux的软件包安装_第3张图片


2.在配置文件/etc/man.conf中增加MANPATH的范围。

linux的软件包安装_第4张图片


由于htop是比较小的软件,安装好的目录下并没有include目录。放安装较大的软件时,如apache,安装好会有自身的库文件。这时需要将安装目录下的include文件夹链接到/usr/include/中

ln -sv /usr/local/apache/include /usr/include/httpd