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

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

各種Linux發(fā)行包都為我們提供了包的管理工具溉贿。
我們學(xué)會用Docker和虛擬機(jī)大法之后阁猜,不妨體會一下不同的版本工具帶來的不同的體驗(yàn)拷恨。

從工具上超陆,影響最大的兩個(gè)系列,就是debian的dpkg包和Red Hat的rpm包兩大系列喇澡。
dpkg系的前端工具是apt蘸吓,用于Debian系統(tǒng)及其衍生系統(tǒng)如Ubuntu系統(tǒng),以及Ubuntu的衍生系統(tǒng)如LinuxMint.
rpm系的影響更廣撩幽,主要有兩大分支:

  • zypper:用于OpenSUSE系統(tǒng)。OpenSUSE上還有強(qiáng)大的yast2工具
  • yum: 用于CentOS系統(tǒng)箩艺。
    • dnf: 由于yum很久不維護(hù)窜醉,所以Fedora使用了yum的一個(gè)新的分支dnf
      曾經(jīng),urpmi也是rpm的重要前端艺谆,用于Mandriva系統(tǒng)榨惰。不過隨著Mandriva停止更新,我們就不再介紹了静汤。

除了上面兩個(gè)之外琅催,不得不提到另外兩個(gè)高級一些的系統(tǒng),ArchLinux中的pacman和Gentoo中的emerge. 我們先從基礎(chǔ)開始虫给,后面爭取講一講這兩個(gè)高級的藤抡。

Paste_Image.png

dpkg系工具

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

搜索apt-cache search / apt search

我們要想安裝包,第一件事情當(dāng)然是搜索想要的軟件在哪個(gè)包里药蜻。
比如我想找找nodejs相關(guān)的包有哪些瓷式,我們用apt-cache search來搜:

apt-cache search nodejs

在Ubuntu 12.04 LTS上替饿,搜到下面的內(nèi)容:

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版本上,不用區(qū)分apt-cache, apt-get這些了贸典,直接用apt就是了视卢。比如在Ubuntu 16.04上,直接輸出『apt search nodejs』就好了廊驼。

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

如果沒搜到怎么辦据过?不用急,有可能軟件列表已經(jīng)更新了蔬充,這個(gè)軟件已經(jīng)被添加進(jìn)來了蝶俱。

命令不用參數(shù),很省事:

sudo apt-get update
sudo apt update

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

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相關(guān)的源:

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]  

更新系統(tǒng) apt-get upgrade / apt upgrade

apt update時(shí)發(fā)現(xiàn)有了不少包已經(jīng)更新了榨呆,想升級怎么辦?
apt-get upgrade/apt upgrade就是做這個(gè)的庸队。

安裝軟件apt-get install / apt install

搜到之后积蜻,就是安裝了,這個(gè)大家應(yīng)該都很熟悉了彻消,例:

sudo apt-get install nodejs-dev

卸載包 apt-get remove / apt remove

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

查詢安裝了哪些包 dpkg -l

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

下面是dpkg -l的輸出示例丙笋,因?yàn)樘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

軟件源列表

這個(gè)前面我們講安裝Docker和VirtualBox時(shí)都講過煌贴,Debian系沒有工具命令維護(hù)這個(gè)軟件源列表御板,直接查看或者修改/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系統(tǒng)的軟件管理工具。dnf是Fedora上使用的基于yum的新工具牛郑,在Fedora上怠肋,yum命令都會被轉(zhuǎn)成dnf的命令來執(zhí)行。

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

查詢軟件源這個(gè)淹朋,yum比apt強(qiáng)笙各,總算是提供了一個(gè)命令了。下面是CentOS 7上的輸出結(jié)果:

[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

我們來看其中一個(gè)文件的內(nèi)容:

[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上,如果運(yùn)行yum鲫懒,會被提示改用dnf:

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

列出已經(jīng)安裝的包

不麻煩前端了嫩实,直接調(diào)用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系列的特色管理系統(tǒng)甲献。在命令行界面下,也會顯示一個(gè)圖形界面颂翼,可以選擇安裝刪除更新等晃洒。

更新源信息: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]

更新發(fā)行版 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兩個(gè)命令,總算比手工改文件進(jìn)步一點(diǎn)刽锤,但是也有限镊尺。

列出已經(jīng)安裝的包

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

最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末,一起剝皮案震驚了整個(gè)濱河市并思,隨后出現(xiàn)的幾起案子庐氮,更是在濱河造成了極大的恐慌,老刑警劉巖宋彼,帶你破解...
    沈念sama閱讀 206,723評論 6 481
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件弄砍,死亡現(xiàn)場離奇詭異,居然都是意外死亡输涕,警方通過查閱死者的電腦和手機(jī)输枯,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 88,485評論 2 382
  • 文/潘曉璐 我一進(jìn)店門,熙熙樓的掌柜王于貴愁眉苦臉地迎上來占贫,“玉大人,你說我怎么就攤上這事先口⌒桶拢” “怎么了?”我有些...
    開封第一講書人閱讀 152,998評論 0 344
  • 文/不壞的土叔 我叫張陵碉京,是天一觀的道長厢汹。 經(jīng)常有香客問我,道長谐宙,這世上最難降的妖魔是什么烫葬? 我笑而不...
    開封第一講書人閱讀 55,323評論 1 279
  • 正文 為了忘掉前任,我火速辦了婚禮,結(jié)果婚禮上搭综,老公的妹妹穿的比我還像新娘垢箕。我一直安慰自己,他們只是感情好兑巾,可當(dāng)我...
    茶點(diǎn)故事閱讀 64,355評論 5 374
  • 文/花漫 我一把揭開白布条获。 她就那樣靜靜地躺著,像睡著了一般蒋歌。 火紅的嫁衣襯著肌膚如雪帅掘。 梳的紋絲不亂的頭發(fā)上,一...
    開封第一講書人閱讀 49,079評論 1 285
  • 那天堂油,我揣著相機(jī)與錄音修档,去河邊找鬼。 笑死府框,一個(gè)胖子當(dāng)著我的面吹牛吱窝,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播寓免,決...
    沈念sama閱讀 38,389評論 3 400
  • 文/蒼蘭香墨 我猛地睜開眼癣诱,長吁一口氣:“原來是場噩夢啊……” “哼!你這毒婦竟也來了袜香?” 一聲冷哼從身側(cè)響起撕予,我...
    開封第一講書人閱讀 37,019評論 0 259
  • 序言:老撾萬榮一對情侶失蹤,失蹤者是張志新(化名)和其女友劉穎蜈首,沒想到半個(gè)月后实抡,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體,經(jīng)...
    沈念sama閱讀 43,519評論 1 300
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡欢策,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 35,971評論 2 325
  • 正文 我和宋清朗相戀三年吆寨,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片踩寇。...
    茶點(diǎn)故事閱讀 38,100評論 1 333
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡啄清,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出俺孙,到底是詐尸還是另有隱情辣卒,我是刑警寧澤,帶...
    沈念sama閱讀 33,738評論 4 324
  • 正文 年R本政府宣布睛榄,位于F島的核電站荣茫,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏场靴。R本人自食惡果不足惜啡莉,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 39,293評論 3 307
  • 文/蒙蒙 一港准、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧咧欣,春花似錦浅缸、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 30,289評論 0 19
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽。三九已至蚕礼,卻和暖如春烟具,著一層夾襖步出監(jiān)牢的瞬間,已是汗流浹背奠蹬。 一陣腳步聲響...
    開封第一講書人閱讀 31,517評論 1 262
  • 我被黑心中介騙來泰國打工朝聋, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留,地道東北人囤躁。 一個(gè)月前我還...
    沈念sama閱讀 45,547評論 2 354
  • 正文 我出身青樓冀痕,卻偏偏與公主長得像,于是被迫代替她去往敵國和親狸演。 傳聞我的和親對象是個(gè)殘疾皇子言蛇,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 42,834評論 2 345

推薦閱讀更多精彩內(nèi)容