如何在安裝之前檢查L(zhǎng)inux軟件包的版本?
大多數(shù)人都知道如何在 Linux 中查找已安裝軟件包的版本,但是,你會(huì)如何查找那些還沒(méi)有安裝的軟件包的版本呢?很簡(jiǎn)單!本文將介紹在 Debian 及其衍生品(如 Ubuntu)中,如何在軟件包安裝之前檢查它的版本。對(duì)于那些想在安裝之前知道軟件包版本的人來(lái)說(shuō),這個(gè)小技巧可能會(huì)有所幫助。
在安裝之前檢查 Linux 軟件包版本
在基于 DEB 的系統(tǒng)中,即使軟件包還沒(méi)有安裝,也有很多方法可以查看他的版本。接下來(lái),我將一一介紹。
方法 1 – 使用 Apt
檢查軟件包的版本的懶人方法:
$ apt show <package-name>
示例:
$ apt show vim
示例輸出:
Package: vim
Version: 2:8.0.1453-1ubuntu1.1
Priority: optional
Section: editors
Origin: Ubuntu
Maintainer: Ubuntu Developers <[email protected]>
Original-Maintainer: Debian Vim Maintainers <[email protected]>
Bugs: https://bugs.launchpad.net/ubuntu/+filebug
Installed-Size: 2,852 kB
Provides: editor
Depends: vim-common (= 2:8.0.1453-1ubuntu1.1), vim-runtime (= 2:8.0.1453-1ubuntu1.1), libacl1 (>= 2.2.51-8), libc6 (>= 2.15), libgpm2 (>= 1.20.7), libpython3.6 (>= 3.6.5), libselinux1 (>= 1.32), libtinfo5 (>= 6)
Suggests: ctags, vim-doc, vim-scripts
Homepage: https://vim.sourceforge.io/
Task: cloud-image, server
Supported: 5y
Download-Size: 1,152 kB
APT-Sources: http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 Packages
Description: Vi IMproved - enhanced vi editor
Vim is an almost compatible version of the UNIX editor Vi.
.
Many new features have been added: multi level undo, syntax
highlighting, command line history, on-line help, filename
completion, block operations, folding, Unicode support, etc.
.
This package contains a version of vim compiled with a rather
standard set of features. This package does not provide a GUI
version of Vim. See the other vim-* packages if you need more
(or less).
N: There is 1 additional record. Please use the '-a' switch to see it
正如你在上面的輸出中看到的,apt show
命令顯示了軟件包許多重要的細(xì)節(jié),例如:
- 包名稱,
- 版本,
- 來(lái)源(vim 來(lái)自哪里),
- 維護(hù)者,
- 包的主頁(yè),
- 依賴,
- 下載大小,
- 簡(jiǎn)介,
- 其他。
因此,Ubuntu 倉(cāng)庫(kù)中可用的 Vim 版本是 8.0.1453。如果我把它安裝到我的 Ubuntu 系統(tǒng)上,就會(huì)得到這個(gè)版本。
或者,如果你不想看那么多的內(nèi)容,那么可以使用 apt policy
這個(gè)命令:
$ apt policy vim
vim:
Installed: (none)
Candidate: 2:8.0.1453-1ubuntu1.1
Version table:
2:8.0.1453-1ubuntu1.1 500
500 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 Packages
500 http://security.ubuntu.com/ubuntu bionic-security/main amd64 Packages
2:8.0.1453-1ubuntu1 500
500 http://archive.ubuntu.com/ubuntu bionic/main amd64 Packages
甚至更短:
$ apt list vim
Listing... Done
vim/bionic-updates,bionic-security 2:8.0.1453-1ubuntu1.1 amd64
N: There is 1 additional version. Please use the '-a' switch to see it
apt
是 Ubuntu 最新版本的默認(rèn)包管理器。因此,這個(gè)命令足以找到一個(gè)軟件包的詳細(xì)信息,給定的軟件包是否安裝并不重要。這個(gè)命令將簡(jiǎn)單地列出給定包的版本以及其他詳細(xì)信息。
方法 2 – 使用 Apt-get
要查看軟件包的版本而不安裝它,我們可以使用 apt-get
命令和 -s
選項(xiàng)。
$ apt-get -s install vim
示例輸出:
NOTE: This is only a simulation!
apt-get needs root privileges for real execution.
Keep also in mind that locking is deactivated,
so don't depend on the relevance to the real current situation!
Reading package lists... Done
Building dependency tree
Reading state information... Done
Suggested packages:
ctags vim-doc vim-scripts
The following NEW packages will be installed:
vim
0 upgraded, 1 newly installed, 0 to remove and 45 not upgraded.
Inst vim (2:8.0.1453-1ubuntu1.1 Ubuntu:18.04/bionic-updates, Ubuntu:18.04/bionic-security [amd64])
Conf vim (2:8.0.1453-1ubuntu1.1 Ubuntu:18.04/bionic-updates, Ubuntu:18.04/bionic-security [amd64])
這里,-s
選項(xiàng)代表 模擬。正如你在輸出中看到的,它不執(zhí)行任何操作。相反,它只是模擬執(zhí)行,好讓你知道在安裝 Vim 時(shí)會(huì)發(fā)生什么。
你可以將 install
選項(xiàng)替換為 upgrade
,以查看升級(jí)包時(shí)會(huì)發(fā)生什么。
$ apt-get -s upgrade vim
方法 3 – 使用 Aptitude
在 Debian 及其衍生品中,aptitude
是一個(gè)基于 ncurses(LCTT 譯注:ncurses 是終端基于文本的字符處理的庫(kù))和命令行的前端 APT 包管理器。
使用 aptitude 來(lái)查看軟件包的版本,只需運(yùn)行:
$ aptitude versions vim
p 2:8.0.1453-1ubuntu1 bionic 500
p 2:8.0.1453-1ubuntu1.1 bionic-security,bionic-updates 500
你還可以使用模擬選項(xiàng)(-s
)來(lái)查看安裝或升級(jí)包時(shí)會(huì)發(fā)生什么。
$ aptitude -V -s install vim
The following NEW packages will be installed:
vim [2:8.0.1453-1ubuntu1.1]
0 packages upgraded, 1 newly installed, 0 to remove and 45 not upgraded.
Need to get 1,152 kB of archives. After unpacking 2,852 kB will be used.
Would download/install/remove packages.
這里,-V
標(biāo)志用于顯示軟件包的詳細(xì)信息。
$ aptitude -V -s upgrade vim
類(lèi)似的,只需將 install
替換為 upgrade
選項(xiàng),即可查看升級(jí)包會(huì)發(fā)生什么。
$ aptitude search vim -F "%c %p %d %V"
這里,
-F
用于指定應(yīng)使用哪種格式來(lái)顯示輸出,%c
– 包的狀態(tài)(已安裝或未安裝),%p
– 包的名稱,%d
– 包的簡(jiǎn)介,%V
– 包的版本。
當(dāng)你不知道完整的軟件包名稱時(shí),這非常有用。這個(gè)命令將列出包含給定字符串(即 vim)的所有軟件包。
以下是上述命令的示例輸出:
[...]
p vim Vi IMproved - enhanced vi editor 2:8.0.1453-1ub
p vim-tlib Some vim utility functions 1.23-1
p vim-ultisnips snippet solution for Vim 3.1-3
p vim-vimerl Erlang plugin for Vim 1.4.1+git20120
p vim-vimerl-syntax Erlang syntax for Vim 1.4.1+git20120
p vim-vimoutliner script for building an outline editor on top of Vim 0.3.4+pristine
p vim-voom Vim two-pane outliner 5.2-1
p vim-youcompleteme fast, as-you-type, fuzzy-search code completion engine for Vim 0+20161219+git
方法 4 – 使用 Apt-cache
apt-cache
命令用于查詢基于 Debian 的系統(tǒng)中的 APT 緩存。對(duì)于要在 APT 的包緩存上執(zhí)行很多操作時(shí),它很有用。一個(gè)很好的例子是我們可以從某個(gè)倉(cāng)庫(kù)或 ppa 中列出已安裝的應(yīng)用程序。
不僅是已安裝的應(yīng)用程序,我們還可以找到軟件包的版本,即使它沒(méi)有被安裝。例如,以下命令將找到 Vim 的版本:
$ apt-cache policy vim
示例輸出:
vim:
Installed: (none)
Candidate: 2:8.0.1453-1ubuntu1.1
Version table:
2:8.0.1453-1ubuntu1.1 500
500 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 Packages
500 http://security.ubuntu.com/ubuntu bionic-security/main amd64 Packages
2:8.0.1453-1ubuntu1 500
500 http://archive.ubuntu.com/ubuntu bionic/main amd64 Packages
正如你在上面的輸出中所看到的,Vim 并沒(méi)有安裝。如果你想安裝它,你會(huì)知道它的版本是 8.0.1453。它還顯示 vim 包來(lái)自哪個(gè)倉(cāng)庫(kù)。
方法 5 – 使用 Apt-show-versions
在 Debian 和基于 Debian 的系統(tǒng)中,apt-show-versions
命令用于列出已安裝和可用軟件包的版本。它還顯示所有可升級(jí)軟件包的列表。如果你有一個(gè)混合的穩(wěn)定或測(cè)試環(huán)境,這是非常方便的。例如,如果你同時(shí)啟用了穩(wěn)定和測(cè)試倉(cāng)庫(kù),那么你可以輕松地從測(cè)試庫(kù)找到應(yīng)用程序列表,還可以升級(jí)測(cè)試庫(kù)中的所有軟件包。
默認(rèn)情況下系統(tǒng)沒(méi)有安裝 apt-show-versions
,你需要使用以下命令來(lái)安裝它:
$ sudo apt-get install apt-show-versions
安裝后,運(yùn)行以下命令查找軟件包的版本,例如 Vim:
$ apt-show-versions -a vim
vim:amd64 2:8.0.1453-1ubuntu1 bionic archive.ubuntu.com
vim:amd64 2:8.0.1453-1ubuntu1.1 bionic-security security.ubuntu.com
vim:amd64 2:8.0.1453-1ubuntu1.1 bionic-updates archive.ubuntu.com
vim:amd64 not installed
這里,-a
選項(xiàng)打印給定軟件包的所有可用版本。
如果已經(jīng)安裝了給定的軟件包,那么就不需要使用 -a
選項(xiàng)。在這種情況下,只需運(yùn)行:
$ apt-show-versions vim
差不多完了。如果你還了解其他方法,在下面的評(píng)論中分享,我將檢查并更新本指南。