Linux軟件包安裝與卸載

Linux下安裝軟件包的三種方法

  1. rpm工具
  2. yum工具
  3. 源碼包安裝

yum是Redhat所特有的安裝RPM程序包的工具,使用起來至關方便。由於使用RPM安裝某一個程序包有可能會由於該程序包依賴另外一個程序包而沒法安裝,而使用yum工具就能夠連同依賴的程序包一塊兒安裝。CentOS一樣可使用yum工具,並且在CentOS中能夠無償使用yum,但Redhat中只有付費後才能使用yum,默認是沒法使用yum的。css

RPM工具

RPM是「Redhat Package Manager」的縮寫。RPM是以一種數據庫記錄的方式來將所須要的套件安裝到Linux主機的一套管理程序。Linux系統中存在着一個關於RPM的數據庫,它記錄了安裝的包以及包與包之間依賴相關性。RPM包是預先在Linux機器上編譯好並打包好的文件,安裝起來很是快捷。可是也有一些缺點,好比安裝的環境必須與編譯時的環境一致或者至關;包與包之間存在着相互依賴的狀況;卸載包時須要先把依賴的包卸載掉,若是依賴的包是系統所必須的,那就不能卸載這個包,不然會形成系統崩潰。RHEL、CentOS、SUSE系統均可以使用rpm包。node


在打開的Vmware虛擬機中,點擊右下方的光驅形狀小圖標,選擇「鏈接」,將系統安裝盤鏈接到Linux系統中。python

點擊右下方的光驅形狀小圖標

  • 把光驅掛載到/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。linux

安裝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 filenamegit

卸載rpm包

命令rpm -e filename數據庫

這裏的filename是rpm包名。卸載時後邊跟的filename和安裝時的是有區別的,安裝時是把一個存在的文件做爲參數,而卸載時只須要包名便可。apache

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

查詢當前系統全部安裝過的rpm包

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

查詢rpm包是否安裝

命令:rpm -q 包名vim

[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 包名windows

[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 包名centos

[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

源碼包卸載,就是刪除安裝的文件夾便可。源碼包安裝完後,沒有啓動或者有其餘操做,此時刪除指定的文件夾便可。

相關文章
相關標籤/搜索