常见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. 我们先从基础开始,后面争取讲一讲这两个高级的。

Linux发行包管理系统

dpkg系工具

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

我们要想安装包,第一件事情当然是搜索想要的软件在哪个包里。
比如我想找找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# 

例:

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

Logo

更多推荐