linux学习之ubuntu的包管理


原文地址:http://www.ibm.com/developerworks/cn/linux/l-lpic1-v3-102-4/index.html?ca=drs-

Ian Shields , 资深程序员, IBM

简介:  学习如何在 Linux® 系统上安装、升级和管理包。本文主要关注 Advanced Packaging Tool (APT),APT 是 Debian 以及从 Debian 衍生出的发行版(比如 Ubuntu)使用的包管理系统。可以使用本文中的资料来准备 Linux 系统管理员认证的 LPI 101 考试,或者只是为了了解添加新软件并及时更新系统的最佳方法。

查 看本系列更多内容

 

概述

在本文中,学习如何在 Linux 系统上使用 Debian 包管理工具管理包。学习:

  • 安装、卸载、升级和删除 Debian 二进制包
  • 寻找包含特定文件或库的包(即使还没有安装包)
  • 获取版本、内容、依赖关系、包完整性和安装状态等包信息(即使还没有安装包)

本文帮助您准备 Linux Professional Institute's Junior Level Administration (LPIC-1) 考试 101 中主题 102 下的目标 102.4。该目标的权值为 3。

先决条件

为了最有效地利用本系列中的文章,您应该具有基本的 Linux 知识,并需要准备一个 Linux 系统,用于练习本文介绍的命令。有时候不同版本的程序输出格式不同,因此您所得到的结果未必总是与此处的相同。特别是,许多输出高度依赖于系统上已经安装 的包。您自己的输出可能很不一样,但是应该能够看出重要的共同点。

 

包管理简介

过去,许多 Linux 程序以源代码的形式发行,用户把源代码构建为所需的程序或程序集;源代码还附带必需的手册页、配置文件等等。现在,大多数 Linux 发行商使用称为 的预构建的程序或程序集,包便于在发行版上安装。在本文中,学习帮助安装、更新和删除包的包 管理 工具。本文主要关注 Advanced Packaging Tool (APT ),APT 是 Debian 以及从 Debian 衍生出的发行版(比如 Ubuntu)使用的包管理系统。本系列中的另一篇文章 “学习 Linux 101:RPM 和 YUM 包管理 ” 讨论 Red Hat 包管理工具。

从用户的角度来说,基本的包管理功能由命令提供。Linux 开发人员一直致力于让 Linux 更容易使用,他们开发了其他工具(包括 GUI 工具)来补充基本工具,这对最终用户隐藏了基本工具的一部分复杂性。在本文和 RPM 和 YUM 包管理 中,我们主要讨论基本工具,但是也会提到一些其他工具,让您能够继续研究它们。

APT、RPM 和 YUM(后两个是用于 Red Hat 系统的包管理工具)有许多相似之处。它们都可以安装和删除包。关于安装的包的信息保存在数据库中。它们都有基本的命令行功能,同时通过其他工具提供对用户 更友好的界面。它们都可以从 Internet 获取包。

在安装 Linux 系统时,通常会安装许多包。这个集合可能是根据系统的用途定制的,比如服务器、桌面或开发工作站。有时候,可能需要安装新的包以添加新功能,更新现有的 包,甚至删除不再需要或已经被新的包取代的包。我们来看看如何完成这些任务以及如何解决一些相关的难题,比如寻找包含某一命令的包。


安装 Debian 包

假设您想学 Lisp,一位同事让您使用 gcl 命令。您试着输入 gcl --helpwhich gcltype gcl 。但是,如果系统无法找到 gcl ,您可能会看到与清单 1 相似的输出。


清单 1. 没有找到 gcl 命令

				
ian@pinguino:~$ gcl --help
-bash: gcl: command not found

ian@pinguino:~$ gcl --help
The program 'gcl' is currently not installed. You can install it by typing:
sudo apt-get install gcl
gcl: command not found

清单 2. 使用 apt-get 安装 gcl
				
ian@pinguino:~$ sudo apt-get install gcl
[sudo] password for ian:
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
linux-headers-2.6.31-14 linux-headers-2.6.31-14-generic
Use 'apt-get autoremove' to remove them.
The following extra packages will be installed:
libreadline5
Suggested packages:
gcl-doc
The following NEW packages will be installed:
gcl libreadline5
0 upgraded, 2 newly installed, 0 to remove and 30 not upgraded.
Need to get 47.1MB of archives.
After this operation, 157MB of additional disk space will be used.
Do you want to continue [Y/n]? y
Get:1 http://us.archive.ubuntu.com karmic/main libreadline5 5.2-6 [140kB]
Get:2 http://us.archive.ubuntu.com karmic/universe gcl 2.6.7-45ubuntu1 [47.0MB]
Fetched 47.1MB in 1min 33s (502kB/s)
Preconfiguring packages ...
Selecting previously deselected package libreadline5.
(Reading database ... 142156 files and directories currently installed.)
Unpacking libreadline5 (from .../libreadline5_5.2-6_i386.deb) ...
Selecting previously deselected package gcl.
Unpacking gcl (from .../gcl_2.6.7-45ubuntu1_i386.deb) ...
Processing triggers for man-db ...
Setting up libreadline5 (5.2-6) ...

Setting up gcl (2.6.7-45ubuntu1) ...
install/gcl: Handling install for emacsen flavor emacs22
Loading 00debian-vars...
No /etc/mailname. Reverting to default...
Loading /etc/emacs/site-start.d/50dictionaries-common.el (source)...
Loading debian-ispell...
Loading /var/cache/dictionaries-common/emacsen-ispell-default.el (source)...
Loading /var/cache/dictionaries-common/emacsen-ispell-dicts.el (source)...
Loading /etc/emacs/site-start.d/50gcl.el (source)...
Wrote /usr/share/emacs22/site-lisp/gcl/add-default.elc
Wrote /usr/share/emacs22/site-lisp/gcl/ansi-doc.elc
Wrote /usr/share/emacs22/site-lisp/gcl/dbl.elc
Wrote /usr/share/emacs22/site-lisp/gcl/doc-to-texi.elc
Wrote /usr/share/emacs22/site-lisp/gcl/gcl.elc
Wrote /usr/share/emacs22/site-lisp/gcl/man1-to-texi.elc
Wrote /usr/share/emacs22/site-lisp/gcl/smart-complete.elc
Wrote /usr/share/emacs22/site-lisp/gcl/sshell.elc
install/gcl: Handling install for emacsen flavor emacs23
Loading 00debian-vars...
No /etc/mailname. Reverting to default...
Loading /etc/emacs/site-start.d/50dictionaries-common.el (source)...
Loading debian-ispell...
Loading /var/cache/dictionaries-common/emacsen-ispell-default.el (source)...
Loading /var/cache/dictionaries-common/emacsen-ispell-dicts.el (source)...
Loading /etc/emacs/site-start.d/50gcl.el (source)...
Wrote /usr/share/emacs23/site-lisp/gcl/add-default.elc
Wrote /usr/share/emacs23/site-lisp/gcl/ansi-doc.elc
Wrote /usr/share/emacs23/site-lisp/gcl/dbl.elc
Wrote /usr/share/emacs23/site-lisp/gcl/doc-to-texi.elc
Wrote /usr/share/emacs23/site-lisp/gcl/gcl.elc
Wrote /usr/share/emacs23/site-lisp/gcl/man1-to-texi.elc
Wrote /usr/share/emacs23/site-lisp/gcl/smart-complete.elc
Wrote /usr/share/emacs23/site-lisp/gcl/sshell.elc

Processing triggers for libc-bin ...
ldconfig deferred processing now taking place

在清单 2 中的输出中,可以看到 apt-get 从某个地方读取了包列表(稍后进一步讨论),构建一个依赖关系树并判断 libreadline5 是必需的,但是当前还没有安装。您还会注意到,它建议安装另一个用于文档的包 gcl-doc。显示一些汇总信息(包括空间使用量)之后,提示您继续,安装 gcl 和必需的依赖包。Debian 包的扩展名通常是 .deb,您会看到下载并打开包,如下所示:

Unpacking gcl (from .../gcl_2.6.7-45ubuntu1_i386.deb) ...

假设您不想安装包,只是想查明这个包是否依赖于其他包。可以在 apt-get 命令中使用 -s 选项(代表 simulate )。还有几个功能相似的选项,比如 --just-print--dry-run 。 完整的信息参见手册页。清单 3 说明模拟安装 gcl-doc 包的情况。


清单 3. gcl-doc 的模拟或演习安装
				
ian@pinguino:~$ sudo apt-get install -s gcl-doc
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
linux-headers-2.6.31-14 linux-headers-2.6.31-14-generic
Use 'apt-get autoremove' to remove them.
The following NEW packages will be installed:
gcl-doc
0 upgraded, 1 newly installed, 0 to remove and 30 not upgraded.
Inst gcl-doc (2.6.7-45ubuntu1 Ubuntu:9.10/karmic)

包的位置

在前一节中,学习了如何安装 Debian 包。但是,包来自什么地方呢?apt-get 如何知道从哪里下载包?我们提到过 apt-get 从某个地方读取包列表。这个位置的起点是 /etc/apt/sources.list。这个列表告诉 apt-get 到哪里寻找包,包括 CD-ROM、本地文件系统以及使用 http 或 ftp 在网上寻找。可以在 /etc/apt/sources.list.d 目录中添加更多来源。

清单 4 显示我系统上 /etc/apt/sources.list 的前几行。注意,第一行中的发行版 CD 被注释掉了(第一个 #)。如果需要安装许多可能不经常更新的新包,可能有必要删除这行的注释标志并从发行版 CD 或 DVD 安装。如果使用宽带网络连接或需要大量更新,那么更高效的办法是直接从 /etc/apt/sources.list 中后面的网络来源下载最新级别的包。


清单 4. /etc/apt/sources.list
				
ian@pinguino:~$ cat /etc/apt/sources.list
#deb cdrom:[Ubuntu 9.10 _Karmic Koala_ - Release i386 (20091028.5)]/ karmic main restrict
ed
# See http://help.ubuntu.com/community/UpgradeNotes for how to upgrade to
# newer versions of the distribution.

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

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

## 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://us.archive.ubuntu.com/ubuntu/ karmic universe
deb-src http://us.archive.ubuntu.com/ubuntu/ karmic universe
deb http://us.archive.ubuntu.com/ubuntu/ karmic-updates universe

删除 Debian 包

如果想删除包,可以使用 apt-getremove 选项。模拟运行见清单 5。


清单 5. gcl 的模拟删除
				
ian@pinguino:~$ sudo apt-get remove -s gcl
[[sudo] password for ian:
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages were automatically installed and are no longer required:
linux-headers-2.6.31-14 linux-headers-2.6.31-14-generic libreadline5
Use 'apt-get autoremove' to remove them.
The following packages will be REMOVED:
gcl
0 upgraded, 0 newly installed, 1 to remove and 30 not upgraded.

清单 6. 删除 gcl 和依赖包
				
ian@pinguino:~$ sudo apt-get autoremove gcl
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages will be REMOVED:
gcl libreadline5 linux-headers-2.6.31-14 linux-headers-2.6.31-14-generic
0 upgraded, 0 newly installed, 4 to remove and 30 not upgraded.
After this operation, 239MB disk space will be freed.
Do you want to continue [Y/n]? y
(Reading database ... 142327 files and directories currently installed.)
Removing gcl ...
remove/gcl: purging byte-compiled files for emacs22
remove/gcl: purging byte-compiled files for emacs23
Removing libreadline5 ...
Removing linux-headers-2.6.31-14-generic ...
Removing linux-headers-2.6.31-14 ...
Processing triggers for man-db ...
Processing triggers for libc-bin ...

更新 Debian 包

如果需要更新某个包,同样应该使用带 install 选项的 apt-get 命令。清单 7 说明如何更新我系统上已经安装的 tzdata 包。在更新包之前,一定要运行 apt-get update , 从而确保本地数据库反映最新的可用更新。


清单 7. 更新单一包
				
ian@pinguino:~$ sudo apt-get install tzdata
[sudo] password for ian:
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following packages will be upgraded:
tzdata
1 upgraded, 0 newly installed, 0 to remove and 29 not upgraded.
Need to get 679kB of archives.
After this operation, 0B of additional disk space will be used.
Get:1 http://us.archive.ubuntu.com karmic-updates/main tzdata 2010i-0ubuntu0.9.10 [679kB]
Fetched 679kB in 1s (569kB/s)
Preconfiguring packages ...
(Reading database ... 124394 files and directories currently installed.)
Preparing to replace tzdata 2010h-0ubuntu0.9.10 (using .../tzdata_2010i-0ubuntu0.9.10_all
.deb) ...
Unpacking replacement tzdata ...
Setting up tzdata (2010i-0ubuntu0.9.10) ...

Current default time zone: 'America/New_York'
Local time is now: Mon May 3 16:11:57 EDT 2010.
Universal Time is now: Mon May 3 20:11:57 UTC 2010.


Run 'dpkg-reconfigure tzdata' if you wish to change it.


更新所有包或升级到新的发行版

除了更新指定的包之外,还可以使用 apt-get upgrade 命令更新系统上的所有包。与之相似,apt-get dist-upgrade 帮助迁移到发行版的新级别。

关于 apt-get 的其他功能和选项的更多信息参见手册页。


APT 配置 — apt.conf 文件

如果查看 apt-get 的手册页,会发现它有许多选项。如果您经常使用 apt-get 命令,觉得不喜欢默认选项,可以在 /etc/apt/apt.conf 中设置新的默认选项。脚本可以使用 apt-config 程序访问 apt.conf 文件。关于 apt.conf 和 apt-config 的更多信息参见手册页。


 

重新配置 Debian 包

APT 包含一个称为 debconf 的功能,它用于在安装包之后配置它们。使用这个功能的包可以在安装之后重新配置。最容易的重新配置方法是使用 dpkg-reconfigure 命令。例如,adduser 命令可能会创建所有系统用户可读的主目录。考虑到保护隐私,您不希望这样。同样,清单 7 中更新的 tzdata 包建议运行 dpkg-reconfigure tzdata 以改变时区。必须用根权力运行 dpkg-reconfigure

图 1 显示在运行 dpkg-reconfigure tzdata 时提示的第一个问题。预设置的默认时区可能不是 America;它反映您自己的系统。使用 Tab 键和游标移动键在这个文本模式的屏幕上移动。


图 1. 使用 dpkg-reconfigure 重新配置时区

 

Debian 包信息

现在,我们来讨论一些获取包信息的工具。其中一些工具还有其他功能,但是这里只关注如何获取信息。

用 dpkg 获取包状态

APT 系统中有一个 dpkg 工具。这是一个介质级包管理工具,它可以安装和删除包以及显示状态信息。可以通过 /etc/dpkg/dpkg.cfg 控制 dpkg 的配置,还可以通过主目录中的 .dpkg.cfg 文件提供进一步配置。

dpkg 工具使用文件系统中 /var/lib/dpkg 树中的许多文件。尤其是 /var/lib/dpkg/status 文件包含系统上包的状态信息。清单 8 使用 dpkg -s 在更新 tzdata 包之后显示它的状态,在删除 gcl 包之后显示它的状态。注意,gcl 包仍然留下了一些配置文件。还可以使用 purge 选项从缓存中清除下载的包文件并删除配置信息。


清单 8. tzdata 包状态

				
ian@pinguino:~$ dpkg -s gcl tzdata
Package: gcl
Status: deinstall ok config-files
Priority: optional
Section: interpreters
Installed-Size: 152848
Maintainer: Ubuntu MOTU Developers <ubuntu-motu@lists.ubuntu.com>
Architecture: i386
Version: 2.6.7-45ubuntu1
Config-Version: 2.6.7-45ubuntu1
Depends: libc6 (>= 2.7), libgmp3c2, libice6 (>= 1:1.0.0), libncurses5 (>= 5.6+20071006-3
), libreadline5 (>= 5.2), libsm6, libx11-6, libxaw7, libxext6, libxmu6, libxt6, tcl8.4 (
>= 8.4.16), tk8.4 (>= 8.4.16), debconf (>= 1.2.0), gcc, emacs22 | emacsen
Suggests: gcl-doc
Conffiles:
/etc/default/gcl 9301be50652f86b8d3f8b835f6dce03e
/etc/emacs/site-start.d/50gcl.el 12116c8c8988326764799973a0a7d5ab
Description: GNU Common Lisp compiler
GNU Common Lisp (GCL) is a Common Lisp compiler and interpreter
implemented in C, and complying mostly with the standard set
forth in the book "Common Lisp, the Language I". It attempts
to strike a useful middle ground in performance and portability
from its design around C.
.
This package contains the Lisp system itself. Documentation
is provided in the gcl-doc package.
Original-Maintainer: Camm Maguire <camm@enhanced.com>

Package: tzdata
Status: install ok installed
Priority: required
Section: libs
Installed-Size: 6276
Maintainer: Ubuntu Developers <ubuntu-devel-discuss@lists.ubuntu.com>
Architecture: all
Version: 2010i-0ubuntu0.9.10
Replaces: libc0.1, libc0.3, libc6, libc6.1
Provides: tzdata-squeeze
Depends: debconf (>= 0.5) | debconf-2.0
Description: time zone and daylight-saving time data
This package contains data required for the implementation of
standard local time for many representative locations around the
globe. It is updated periodically to reflect changes made by
political bodies to time zone boundaries, UTC offsets, and
daylight-saving rules.
Original-Maintainer: GNU Libc Maintainers <debian-glibc@lists.debian.org>


包和其中的文件

用户常常希望知道一个包中有哪些文件,或者某个文件来自哪个包。dpkg 可以完成这两个任务。清单 9 使用 dpkg -L 列出由 libparted 包安装的文件(包括目录)。对于大多数包,只需指定包名,不需要指定版本。但是,一些包可能有多个版本可用,所以在使用 dpkg 查询包信息时可能需要指定更详细的包名。


清单 9. libparted 包中有什么?

				
ian@pinguino:~$ dpkg -L libparted
Package `libparted' is not installed.
Use dpkg --info (= dpkg-deb --info) to examine archive files,
and dpkg --contents (= dpkg-deb --contents) to list their contents.
ian@pinguino:~$ dpkg -L libparted1.8-12
/.
/lib
/lib/libparted-1.8.so.12.0.0
/usr
/usr/share
/usr/share/doc
/usr/share/doc/libparted1.8-12
/usr/share/doc/libparted1.8-12/copyright
/usr/share/doc/libparted1.8-12/changelog.Debian.gz
/lib/libparted-1.8.so.12

 

要想查明哪个包包含某个文件,应该使用 dpkg 的 -S 选项,见清单 10。左边列出包的名称。


清单 10. 哪个包包含某个文件?

				
ian@pinguino:~$ dpkg -S /lib/libparted-1.8.so.12
libparted1.8-12: /lib/libparted-1.8.so.12

 

有时候,一个文件可能看起来不属于任何包。当发生这种情况时,可能需要执行额外操作以查明包的来源。安装设置步骤可能会创建不作为包内容列出 的符号链接。Linux 系统中最近增加了替代品 系统,使用 update-alternatives 命令管理它。常常为 java 命令创建替代品,它们可能是 openJDK、Sun 或 IBM 版本等等。

清单 11 演示如何使用 which 命令查明在试图运行 java 时实际上调用的是什么。然后使用 ls 命令查看 java 命令符号链接到什么。到 /etc/alternatives 目录的链接表明正在使用替代品系统,所以使用 update-alternatives 命令寻找更多信息,最后使用 dpkg -S 命令确认 java 命令来自 openjdk-6-jre-headless。替代品系统的设置是由 openjdk-6-jre-headless 包中的一个安装后脚本完成的。


清单 11. 比较复杂的 dpkg -S 用法

				
ian@pinguino:~$ which java
/usr/bin/java
ian@pinguino:~$ ls -l $(which java)
lrwxrwxrwx 1 root root 22 2010-05-03 17:51 /usr/bin/java -> /etc/alternatives/java
ian@pinguino:~$ update-alternatives --display java
java - auto mode
link currently points to /usr/lib/jvm/java-6-openjdk/jre/bin/java
/usr/lib/jvm/java-6-openjdk/jre/bin/java - priority 1061
slave java.1.gz: /usr/lib/jvm/java-6-openjdk/jre/man/man1/java.1.gz
Current `best' version is /usr/lib/jvm/java-6-openjdk/jre/bin/java.

使用 aptitude

前面提到包的状态保存在 /var/lib/dpkg/status 中。还提到 dpkg 不仅能够显示包信息。现在来看看 aptitude 命令,它为 APT 包管理功能提供基于文本的全屏幕界面(使用 ncurses)。可以使用 aptitude 安装或删除包以及控制状态标志,例如包是应该更新,还是应该保持当前状态。如果运行 aptitude 命令(作为根用户),会看到与图 2 相似的屏幕。


图 2. 运行 aptitude



Enter 展开或收拢各个选择,然后使用 ctrl-t 访问菜单栏。图 3 说明,有一个新的内核版本 2.6.31.20 可用,还有其他可用的更新。左栏中的 'i' 表示当前状态是要安装这个包。Help 菜单项解释各个选项,包括保持包的当前级别而不是更新它,删除包,或者把包标为自动安装的,让它符合自动删除的条件。记得 apt-getautoremove 选项吗?现在您知道如何检查或控制哪些包符合自动删除的条件了。使用 Help 中描述的键盘快捷键或使用 Package 菜单项修改标志。


图 3. 运行 aptitude 并检查包标志


可以使用斜杠(/)键搜索包。例如,如果希望重新安装前面删除的 gcl 包,只需输入 “/gcl” 搜索它。如果搜索找到了 gcl 之外的其他东西,比如 gcl-doc,那么按 “n” 键前进到下一个匹配。然后使用 Package 菜单把这个包标为要安装。

完成之后,选择 Actions->Install/remove packages (或按 “g”)把选择应用于系统。如果不想应用修改,也可以选择退出选项。

在任何时候,都可以使用菜单栏或按 “?”(问号)获得帮助,然后按 “q” 键退出帮助。

 

用其他工具升级 Debian

您已经看到 aptitude 如何帮助安装或删除包,以及把系统上的所有包升级到最新级别。

除了 aptitude 之外,还有另外几种用于 Debian 系统的交互式包管理界面,包括 dselect、synaptic、update-manager、gnome-apt 和 wajig。synaptic 是在 X Window System 上使用的图形化应用程序。图 4 显示 synaptic 用户界面,其中把 gcl 包标为要安装。


图 4. 使用 synaptic 安装 gcl

 

Apply 按钮安装 gcl 并更新计划要更新的所有其他包。Reload 按钮刷新包列表。如果您喜欢 GUI 界面,可能会觉得 synaptic 比 apt-get、dpkg 或 dselect 更容易使用。

您的系统可能包含 update-manager ,这也是一个 X Window System 应用程序,专门帮助您更新系统。如果安装了,它很可能会定期地自动启动,这样您就不会忘记更新。图 5 说明 update-manager 如何显示您在图 2 中见过的更新集。与 aptitude 一样,它对更新进行分类,让您知道哪些更新是重要的安全更新。

 

图 5. Update Manager 示例

 

寻找 Debian 包

最后,看看寻找包的方法。通常,apt-get 和这里讨论的其他工具已经知道可用包列表中任何 Debian 包的信息。我们还没有使用过的一个命令是 apt-cache ,它用于在您的系统上搜索包信息。apt-cache 可以使用正则表达式(关于正则表达式的更多信息,参见 学习 Linux,101:使用正则表达式搜索文本文件 )。假设要寻找包含 Linux 装载器的包。清单 12 说明如何完成这个任务。


清单 12. 用 apt-cache 搜索 Linux 装载器

				
ian@pinguino:~$ apt-cache search "linux loader"
lilo - LInux LOader - The Classic OS loader can load Linux and others
lilo-doc - Documentation for LILO (LInux LOader)

 

您在前面看到 aptitude 和 synaptic 也提供搜索工具。如果使用 synaptic,搜索菜单中还有只搜索包名或包描述的选项。

如果仍然无法找到包,可能可以在 Debian 站点上的包列表中(见 参 考资料 中的链接)或 Internet 上的其他地方找到它。

与未安装的包相比,对于已经安装的包,大多数包工具可以提供更多信息,比如包中的文件列表。如果需要寻找包含未安装的程序的包,有几种方法:

  • 可以猜测可能包含它的包,下载这个包,但是不安装。得到这个包之后,就可以查询它。
  • 可以搜索 Internet。
  • 可以试试 command-not-found 功能,参见本文后面的 “命 令未找到 ” 小节。

apt-get 命令的 -d 选项下载包,但是不安装它。还有 --print-uris 选项,它显示下载包的来源位置以及校验和。当前的校验和很可能是 SHA256 校验和,可以使用 sha256sum 命令检查下载的包的完整性。注意,如果已经下载了包,就不会显示 URI 和校验和信息,所以应该在下载包之前获取这些信息。

假设您想知道 gcl 命令是否确实包含在 gcl 包中。清单 13 说明如何使用 apt-get 只下载但不安装 gcl 包。


清单 13. 使用 apt-get 只下载

				
ian@pinguino:~$ sudo apt-get install -d gclReading package lists... Done
Building dependency tree
Reading state information... Done
The following extra packages will be installed:
libreadline5
Suggested packages:
gcl-doc
The following NEW packages will be installed:
gcl libreadline5
0 upgraded, 2 newly installed, 0 to remove and 0 not upgraded.
Need to get 47.1MB of archives.
After this operation, 157MB of additional disk space will be used.
Do you want to continue [Y/n]?
Get:1 http://us.archive.ubuntu.com karmic/main libreadline5 5.2-6 [140kB]
Get:2 http://us.archive.ubuntu.com karmic/universe gcl 2.6.7-45ubuntu1 [47.0MB]
Fetched 47.1MB in 7s (6,475kB/s)
Download complete and in download only mode

 

下载了包之后,可以使用 dpkg--info 选项显示包信息,或者使用 --contents 选项显示这个包安装的文件。下载的文件常常放在 /var/cache/apt/archives/ 中。清单 14 说明如何找到下载的文件并查明它将安装哪些二进制文件(假设它们安装在 .../bin/... 目录中)。


清单 14. 使用 dpkg 列出 .deb 文件的内容

				
ian@pinguino:~$ sudo find /var/cache -name "*.deb"
/var/cache/apt/archives/gcl_2.6.7-45ubuntu1_i386.deb
/var/cache/apt/archives/libreadline5_5.2-6_i386.deb
ian@pinguino:~$ sudo dpkg --contents /var/cache/apt/archives/gcl*.deb| grep "/bin/"
drwxr-xr-x root/root 0 2008-12-06 07:42 ./usr/bin/

命令未找到

清 单 1 中,一个消息指出安装哪个包就可以得到 gcl 命令。这是怎么实现的?如果 Bash shell 搜索一个命令,但是没有找到,那么 shell 就搜索 shell 函数 command_not_found_handle 。 清单 15 给出我的 Ubuntu 9.10 系统上这个函数的定义。


清单 15. command_not_found_handle
				
ian@pinguino:~$ type command_not_found_handle
command_not_found_handle is a function
command_not_found_handle ()
{
if [ -x /usr/lib/command-not-found ]; then
/usr/bin/python /usr/lib/command-not-found -- $1;
return $?;
else
return 127;
fi
}

如果 command_not_found_handle 函数存在,shell 以原来的命令和参数作为参数调用它,函数的退出状态成为 shell 的退出状态。如果没有定义此函数,shell 输出错误消息并返回退出状态 127。常常在系统 /etc/bash.bashrc 文件中设置此函数。如清单 15 所示,函数检查 /usr/lib/command-not-found 是否存在,如果存在,就作为 Python 脚本运行它。如果不存在(可能因为在 shell 会话启动之后删除了提供它的 command-not-found 包),那么函数模拟标准的系统行为,返回 127。


-rwxr-xr-x root/root 617 2008-12-06 07:42 ./usr/bin/gcl

 

如果使用 apt-get 之外的其他工具找到并下载一个 .deb 文件,可以使用 dpkg -i 安装它。

如果决定不把下载的包安装到 APT 存档中,可以运行 apt-get clean 清除下载的包文件。

如果用所有其他方法都找不到包,还有另一个来源。假设您找到了一个打包为 RPM 而不是 .deb 的程序。可以使用 alien 程序在包格式之间执行转换。应该仔细阅读 alien 文档,因为 alien 并不能把所有包管理系统的所有特性转换为另一种格式。

 

PackageKit

讨论包安装就不能不提到 PackageKit,这是一个为更方便地安装和更新软件而设计的系统。它的意图是把不同发行版中使用的所有软件图形化工具统一起来。 PackageKit 使用一个由系统激活的守护进程,这意味着这个守护进程只在需要时激活。Packagekit 有适用于 Gnome (gnome-packagekit) 和 KDE (KPackageKit) 的版本。

除了这里讨论的内容,Debian 包管理系统还有许多特性。除了包管理系统,Debian 还有许多其他功能。更多信息和本系列中其他文章的链接参见 参 考资料

 

参考资料

学习

获得产品和技术

讨论

关于作者


Ian Shields 参与 developerWorks Linux 专区的许多 Linux 项目。他是 IBM 北卡罗来纳州 Research Triangle Park 的一名高级程序员。他于 1973 年作为一名系统工程师加入 IBM 位于澳大利亚堪培拉的子公司。此后,在加拿大蒙特利尔和北卡罗来纳州 RTP 从事通信系统和普适计算。他拥有多项专利并发表了许多论文。他毕业于 Australian National University,取得纯数学和哲学学位。他拥有北卡罗来纳州州立大学的计算机硕士和博士学位。更多信息参见 My developerWorks 上 Ian 的个人信息

从mysql官网上下载mysql-server_5.7.21-1ubuntu14.04_amd64.deb-bundle.tar,然后进行离线安装,解压该安装,会出现11个依赖,按照顺序依次使用sudo dpkg -i 进行安装,中间会报错,显示缺少相应的依赖,具体如下: *******@ubuntu:/opt/mysql$ sudo dpkg -i mysql-community-client_5.7.21-1ubuntu14.04_amd64.deb Selecting previously unselected package mysql-community-client. (Reading database ... 208518 files and directories currently installed.) Preparing to unpack mysql-community-client_5.7.21-1ubuntu14.04_amd64.deb ... Unpacking mysql-community-client (5.7.21-1ubuntu14.04) ... dpkg: dependency problems prevent configuration of mysql-community-client: mysql-community-client depends on libaio1 (>= 0.3.93); however: Package libaio1 is not installed. dpkg: error processing package mysql-community-client (--install): dependency problems - leaving unconfigured Processing triggers for man-db (2.6.7.1-1ubuntu1) ... Errors were encountered while processing: mysql-community-client 和 ********@ubuntu:/opt/mysql$ sudo dpkg -i mysql-community-server_5.7.21-1ubuntu14.04_amd64.deb Selecting previously unselected package mysql-community-server. (Reading database ... 208598 files and directories currently installed.) Preparing to unpack mysql-community-server_5.7.21-1ubuntu14.04_amd64.deb ... Unpacking mysql-community-server (5.7.21-1ubuntu14.04) ... dpkg: dependency problems prevent configuration of mysql-community-server: mysql-community-server depends on libmecab2 (>= 0.996-1.1); however: Package libmecab2 is not installed. dpkg: error processing package mysql-community-server (--install): dependency problems - leaving unconfigured Processing triggers for man-db (2.6.7.1-1ubuntu1) ... Processing triggers for ureadahead (0.100.0-16) ... ureadahead will be reprofiled on next reboot Errors were encountered while processing: mysql-community-server 附上依赖安装顺序: 1.mysql-common_5.7.21-1ubuntu14.04_amd64.deb 2.libmysqlclient20_5.7.21-1ubuntu14.04_amd64.deb 3.libmysqlclient-dev_5.7.21-1ubuntu14.04_amd64.deb 4.libmysqld-dev_5.7.21-1ubuntu14.04_amd64.deb
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值