常見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停止更新,我們就不再介紹了静汤。
- dnf: 由于yum很久不維護(hù)窜醉,所以Fedora使用了yum的一個(gè)新的分支dnf
除了上面兩個(gè)之外琅催,不得不提到另外兩個(gè)高級一些的系統(tǒng),ArchLinux中的pacman和Gentoo中的emerge. 我們先從基礎(chǔ)開始虫给,后面爭取講一講這兩個(gè)高級的藤抡。
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