常見Linux發行版的包管理工具速成指南

常見Linux發行版的包管理工具速成指南

各種Linux發行包都為我們提供了包的管理工具。
我們學會用Docker和虛擬機大法之后,不妨體會一下不同的版本工具帶來的不同的體驗。

從工具上,影響最大的兩個系列,就是debian的dpkg包和Red Hat的rpm包兩大系列。
dpkg系的前端工具是apt,用于Debian系統及其衍生系統如Ubuntu系統,以及Ubuntu的衍生系統如LinuxMint.
rpm系的影響更廣,主要有兩大分支:

  • zypper:用于OpenSUSE系統。OpenSUSE上還有強大的yast2工具
  • yum: 用于CentOS系統。
    • dnf: 由于yum很久不維護,所以Fedora使用了yum的一個新的分支dnf
      曾經,urpmi也是rpm的重要前端,用于Mandriva系統。不過隨著Mandriva停止更新,我們就不再介紹了。

除了上面兩個之外,不得不提到另外兩個高級一些的系統,ArchLinux中的pacman和Gentoo中的emerge. 我們先從基礎開始,后面爭取講一講這兩個高級的。

Paste_Image.png

dpkg系工具

apt是大家最熟悉的工具之一了。基本上大家都會用sudo apt-get install xxx吧。

搜索apt-cache search / apt search

我們要想安裝包,第一件事情當然是搜索想要的軟件在哪個包里。
比如我想找找nodejs相關的包有哪些,我們用apt-cache search來搜:

apt-cache search nodejs

在Ubuntu 12.04 LTS上,搜到下面的內容:

node-lru-cache - Javascript least-recently-used cache object - NodeJS
node-request - simplified HTTP request client for NodeJS
node-semver - Semantic versioner for NodeJS
nodejs - Node.js event-based server-side javascript engine
nodejs-dbg - Node.js event-based server-side javascript engine (debug)
nodejs-dev - Development files for Node.js

在較新的Debian和Ubuntu版本上,不用區分apt-cache, apt-get這些了,直接用apt就是了。比如在Ubuntu 16.04上,直接輸出『apt search nodejs』就好了。

更新軟件列表 apt-get update / apt update

如果沒搜到怎么辦?不用急,有可能軟件列表已經更新了,這個軟件已經被添加進來了。

命令不用參數,很省事:

sudo apt-get update
sudo apt update

然后apt就會到各個軟件源去查詢是否有更新,下面是Ubuntu 12.04(Precise)的執行結果片斷:

Get:26 http://security.ubuntu.com precise-security/main TranslationIndex [208 B]
Get:27 http://security.ubuntu.com precise-security/multiverse TranslationIndex [199 B]
Get:28 http://security.ubuntu.com precise-security/restricted TranslationIndex [202 B]
Get:29 http://security.ubuntu.com precise-security/universe TranslationIndex [205 B]
Get:30 http://security.ubuntu.com precise-security/main Sources [146 kB]                                                                                                                                          
Get:31 http://security.ubuntu.com precise-security/restricted Sources [4,623 B]                                                                                                                                   
Get:32 http://security.ubuntu.com precise-security/universe Sources [54.6 kB]                                                                                                                                     
Get:33 http://security.ubuntu.com precise-security/multiverse Sources [3,036 B]                                                                                                                                   
Get:34 http://security.ubuntu.com precise-security/main amd64 Packages [660 kB]  

Ubuntu 16.04(Xenial)下apt update檢查的就是xenial相關的源:

root@5383f4c36fd1:/# apt update
Get:1 http://archive.ubuntu.com/ubuntu xenial InRelease [247 kB]
Get:2 http://archive.ubuntu.com/ubuntu xenial-updates InRelease [102 kB]                                                                                                                                          
Get:3 http://archive.ubuntu.com/ubuntu xenial-security InRelease [102 kB]                                                                                                                                         
Get:4 http://archive.ubuntu.com/ubuntu xenial/main Sources [1103 kB]  

更新系統 apt-get upgrade / apt upgrade

apt update時發現有了不少包已經更新了,想升級怎么辦?
apt-get upgrade/apt upgrade就是做這個的。

安裝軟件apt-get install / apt install

搜到之后,就是安裝了,這個大家應該都很熟悉了,例:

sudo apt-get install nodejs-dev

卸載包 apt-get remove / apt remove

有安裝就有卸載,通過remove命令來刪除包

查詢安裝了哪些包 dpkg -l

查詢安裝了哪些包就不用麻煩前端工具了,我們直接調用幕后黑手dpkg來查。

下面是dpkg -l的輸出示例,因為太長,我們就選開頭一部分好了:

root@5383f4c36fd1:/# dpkg -l
Desired=Unknown/Install/Remove/Purge/Hold
| Status=Not/Inst/Conf-files/Unpacked/halF-conf/Half-inst/trig-aWait/Trig-pend
|/ Err?=(none)/Reinst-required (Status,Err: uppercase=bad)
||/ Name                                           Version                      Architecture                 Description
+++-==============================================-============================-============================-==================================================================================================
ii  adduser                                        3.113+nmu3ubuntu4            all                          add and remove users and groups
ii  apt                                            1.2.15                       amd64                        commandline package manager
ii  base-files                                     9.4ubuntu4.3                 amd64                        Debian base system miscellaneous files
ii  base-passwd                                    3.5.39                       amd64                        Debian base system master password and group files
ii  bash                                           4.3-14ubuntu1.1              amd64                        GNU Bourne Again SHell
ii  bsdutils                                       1:2.27.1-6ubuntu3.1          amd64                        basic utilities from 4.4BSD-Lite
ii  coreutils                                      8.25-2ubuntu2                amd64                        GNU core utilities
ii  dash                                           0.5.8-2.1ubuntu2             amd64                        POSIX-compliant shell
ii  debconf                                        1.5.58ubuntu1                all                          Debian configuration management system
ii  debianutils                                    4.7                          amd64                        Miscellaneous utilities specific to Debian
ii  diffutils                                      1:3.3-3                      amd64                        File comparison utilities
ii  dpkg                                           1.18.4ubuntu1.1              amd64                        Debian package management system

軟件源列表

這個前面我們講安裝Docker和VirtualBox時都講過,Debian系沒有工具命令維護這個軟件源列表,直接查看或者修改/etc/apt/sources.list就好了
下面是Ubuntu 16.04的例子:

root@5383f4c36fd1:/# cat /etc/apt/sources.list
# See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to
# newer versions of the distribution.

deb http://archive.ubuntu.com/ubuntu/ xenial main restricted
deb-src http://archive.ubuntu.com/ubuntu/ xenial main restricted

## Major bug fix updates produced after the final release of the
## distribution.
deb http://archive.ubuntu.com/ubuntu/ xenial-updates main restricted
deb-src http://archive.ubuntu.com/ubuntu/ xenial-updates main restricted

## Uncomment the following two lines to add software from the 'universe'
## repository.
## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
## team. Also, please note that software in universe WILL NOT receive any
## review or updates from the Ubuntu security team.
deb http://archive.ubuntu.com/ubuntu/ xenial universe
deb-src http://archive.ubuntu.com/ubuntu/ xenial universe
deb http://archive.ubuntu.com/ubuntu/ xenial-updates universe
deb-src http://archive.ubuntu.com/ubuntu/ xenial-updates universe

## N.B. software from this repository may not have been tested as
## extensively as that contained in the main release, although it includes
## newer versions of some applications which may provide useful features.
## Also, please note that software in backports WILL NOT receive any review
## or updates from the Ubuntu security team.
# deb http://archive.ubuntu.com/ubuntu/ xenial-backports main restricted
# deb-src http://archive.ubuntu.com/ubuntu/ xenial-backports main restricted

deb http://archive.ubuntu.com/ubuntu/ xenial-security main restricted
deb-src http://archive.ubuntu.com/ubuntu/ xenial-security main restricted
deb http://archive.ubuntu.com/ubuntu/ xenial-security universe
deb-src http://archive.ubuntu.com/ubuntu/ xenial-security universe
# deb http://archive.ubuntu.com/ubuntu/ xenial-security multiverse
# deb-src http://archive.ubuntu.com/ubuntu/ xenial-security multiverse

rpm系工具

yum / dnf

yum是廣泛使用于Red Hat系統的軟件管理工具。dnf是Fedora上使用的基于yum的新工具,在Fedora上,yum命令都會被轉成dnf的命令來執行。

更新軟件源:yum check-update / dnf check-update
更新系統:yum update / dnf update
搜索:yum search 包名
安裝:yum install 包名
刪除:yum erase 包名
查詢有哪些軟件源:yum repolist

查詢軟件源這個,yum比apt強,總算是提供了一個命令了。下面是CentOS 7上的輸出結果:

[root@9fb0d15b3900 yum.repos.d]# yum repolist
Loaded plugins: fastestmirror, ovl
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
repo id                                                                                              repo name                                                                                               status
base/7/x86_64                                                                                        CentOS-7 - Base                                                                                         9363
extras/7/x86_64                                                                                      CentOS-7 - Extras                                                                                        393
updates/7/x86_64                                                                                     CentOS-7 - Updates  

但是,好日子也就到此為止了。如果我們要想添加刪除軟件源,還是要自己改文件。這些文件在/etc/yum.repos.d/下面,直接修改下面的repo文件。

[root@9fb0d15b3900 /]# cd /etc/yum.repos.d/
[root@9fb0d15b3900 yum.repos.d]# ls
CentOS-Base.repo  CentOS-CR.repo  CentOS-Debuginfo.repo  CentOS-Media.repo  CentOS-Sources.repo  CentOS-Vault.repo  CentOS-fasttrack.repo

我們來看其中一個文件的內容:

[root@9fb0d15b3900 yum.repos.d]# cat 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=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

#released updates 
[updates]
name=CentOS-$releasever - Updates
mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates&infra=$infra
#baseurl=http://mirror.centos.org/centos/$releasever/updates/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras
mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras&infra=$infra
#baseurl=http://mirror.centos.org/centos/$releasever/extras/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus
mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus&infra=$infra
#baseurl=http://mirror.centos.org/centos/$releasever/centosplus/$basearch/
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

在Fedora上,如果運行yum,會被提示改用dnf:

[root@a908449fa797 /]# yum check-update
Redirecting to '/usr/bin/dnf check-update' (see 'man yum2dnf')

列出已經安裝的包

不麻煩前端了,直接調用rpm命令:

[root@188d23031964 /]# rpm -qa
fedora-repos-25-1.noarch
setup-2.10.4-1.fc25.noarch
basesystem-11-2.fc24.noarch
libreport-filesystem-2.8.0-1.fc25.x86_64
tzdata-2016h-1.fc25.noarch
ncurses-base-6.0-6.20160709.fc25.noarch
glibc-common-2.24-3.fc25.x86_64
ncurses-libs-6.0-6.20160709.fc25.x86_64
libsepol-2.5-10.fc25.x86_64
libselinux-2.5-12.fc25.x86_64
...

zypper

在講zypper之前,我們不得不提一下圖形化界面的yast2,這是SUSE系列的特色管理系統。在命令行界面下,也會顯示一個圖形界面,可以選擇安裝刪除更新等。

更新源信息:zypper refresh

bash-4.3# zypper refresh
Retrieving repository 'NON-OSS' metadata ....................................................................................................................................................................[done]
Building repository 'NON-OSS' cache .........................................................................................................................................................................[done]
Retrieving repository 'OSS' metadata ........................................................................................................................................................................[done]
Building repository 'OSS' cache .............................................................................................................................................................................[done]
Retrieving repository 'OSS Update' metadata .................................................................................................................................................................[done]
Building repository 'OSS Update' cache ......................................................................................................................................................................[done]
Retrieving repository 'Update Non-Oss' metadata .............................................................................................................................................................[done]
Building repository 'Update Non-Oss' cache ..................................................................................................................................................................[done]
All repositories have been refreshed.

更新包:zypper update

從更新上看,鮮明地看出來,更新的包還都是rpm包。

bash-4.3# zypper update
Loading repository data...
Reading installed packages...

The following 5 NEW packages are going to be installed:
  dbus-1 kbd kmod pkg-config udev

The following 10 packages are going to be upgraded:
  libblkid1 libfdisk1 libmount1 libpcre1 libsmartcols1 libsystemd0 libudev1 libuuid1 systemd util-linux

10 packages to upgrade, 5 new.
Overall download size: 9.7 MiB. Already cached: 0 B. After the operation, additional 12.2 MiB will be used.
Continue? [y/n/? shows all options] (y): y
Retrieving package kbd-2.0.3-1.4.x86_64                                                                                                                                      (1/15),   1.7 MiB (  4.0 MiB unpacked)
Retrieving: kbd-2.0.3-1.4.x86_64.rpm ........................................................................................................................................................................[done]
Retrieving package kmod-17-6.2.x86_64                                                                                                                                        (2/15),  67.5 KiB (142.2 KiB unpacked)
Retrieving: kmod-17-6.2.x86_64.rpm ..........................................................................................................................................................................[done]

更新發行版 zypper dist-upgrade

bash-4.3# zypper dist-upgrade
Warning: You are about to do a distribution upgrade with all enabled repositories. Make sure these repositories are compatible before you continue. See 'man zypper' for more information about this command.
Loading repository data...
Reading installed packages...
Computing distribution upgrade...

The following NEW package is going to be installed:
  openSUSE-release-ftp

1 new package to install.
Overall download size: 3.0 KiB. Already cached: 0 B. After the operation, additional 66.0 B will be used.
Continue? [y/n/? shows all options] (y): y
Retrieving package openSUSE-release-ftp-42.2-1.150.x86_64                                                                                                                     (1/1),   3.0 KiB (   66   B unpacked)
Retrieving: openSUSE-release-ftp-42.2-1.150.x86_64.rpm ......................................................................................................................................................[done]
Checking for file conflicts: ................................................................................................................................................................................[done]
(1/1) Installing: openSUSE-release-ftp-42.2-1.150.x86_64 ....................................................................................................................................................[done]
bash-4.3# 

搜索包:zypper search

例:

bash-4.3# zypper search nodejs
Loading repository data...
Reading installed packages...

S | Name             | Summary                                          | Type      
--+------------------+--------------------------------------------------+-----------
  | nodejs-packaging | RPM Macros and Utilities for Node.js Packaging   | package   
  | nodejs4          | Evented I/O for V8 JavaScript                    | package   
  | nodejs4          | Evented I/O for V8 JavaScript                    | srcpackage
  | nodejs4-devel    | Files needed for development of NodeJS platforms | package   
  | nodejs4-docs     | Node.js API documentation                        | package   

安裝:zypper install

如果不能完全匹配,就選最接近的,例:

bash-4.3# zypper install nodejs
Loading repository data...
Reading installed packages...
'nodejs' not found in package names. Trying capabilities.
Resolving package dependencies...

The following NEW package is going to be installed:
  nodejs4

1 new package to install.
Overall download size: 3.3 MiB. Already cached: 0 B. After the operation, additional 12.7 MiB will be used.
Continue? [y/n/? shows all options] (y): y
Retrieving package nodejs4-4.6.1-3.1.x86_64                                                                                                                                   (1/1),   3.3 MiB ( 12.7 MiB unpacked)
Retrieving: nodejs4-4.6.1-3.1.x86_64.rpm ........................................................................................................................................................[done (4.9 MiB/s)]
Checking for file conflicts: ................................................................................................................................................................................[done]
(1/1) Installing: nodejs4-4.6.1-3.1.x86_64 ..................................................................................................................................................................[done]

移除包:zypper remove

列出源信息:zypper repos

例:

bash-4.3# zypper repos
# | Alias          | Name           | Enabled | GPG Check | Refresh
--+----------------+----------------+---------+-----------+--------
1 | non-oss        | NON-OSS        | Yes     | (r ) Yes  | Yes    
2 | oss            | OSS            | Yes     | (r ) Yes  | Yes    
3 | oss-update     | OSS Update     | Yes     | (r ) Yes  | Yes    
4 | update-non-oss | Update Non-Oss | Yes     | (r ) Yes  | Yes    

對于repo管理,zypper提供了zypper addrepo和zypper removerepo兩個命令,總算比手工改文件進步一點,但是也有限。

列出已經安裝的包

還是直接請rpm命令出場:rpm -qa

最后編輯于
?著作權歸作者所有,轉載或內容合作請聯系作者
平臺聲明:文章內容(如有圖片或視頻亦包括在內)由作者上傳并發布,文章內容僅代表作者本人觀點,簡書系信息發布平臺,僅提供信息存儲服務。

推薦閱讀更多精彩內容