## # 软件包管理

If we spend any time in the Linux community, we hear many opinions as to which of the many Linux distributions is "best." Often, these discussions get really silly, focusing on such things as the prettiness of the desktop background (some people won't use Ubuntu because its default color scheme is brown!) and other trivial matters.

The most important determinant of distribution quality is the packaging system and the vitality of the distribution's support community. As we spend more time with Linux, we see that its software landscape is extremely dynamic. Things are constantly changing. Most of the top-tier Linux distributions release new versions every six months and many individual program updates every day. To keep up with this blizzard of software, we need good tools for package management.

Linux 发行版本质量最重要的决定因素是软件包管理系统和其支持社区的持久性。随着我们 花更多的时间在 Linux 上，我们会发现它的变化是非常快的。大多数一线 Linux 发行版每隔六个月发布一个新版本，并且许多独立的程序每天都会更新。为了能和这些 如暴风雪一般多的软件保持联系，我们需要一些好工具来进行软件包管理。

Package management is a method of installing and maintaining software on the system. Today, most people can satisfy all of their software needs by installing packages from their Linux distributor. This contrasts with the early days of Linux, when one had to download and compile source code in order to install software. Not that there is anything wrong with compiling source code; in fact, having access to source code is the great wonder of Linux. It gives us (and everybody else) the ability to examine and improve the system. It's just that having a pre-compiled package is faster and easier to deal with. In this chapter, we will look at some of the command line tools used for package management. While all of the major distributions provide powerful and sophisticated graphical programs for maintaining the system, it is important to learn about the command line programs, too. They can perform many tasks that are difficult (or impossible) to do with their graphical counterparts.

### # 打包系统

Different distributions use different packaging systems and as a general rule, a package intended for one distribution is not compatible with another distribution. Most distributions fall into one of two camps of packaging technologies: the Debian “.deb” camp and the Red Hat “.rpm” camp. There are some important exceptions such as Gentoo, Slackware, and Foresight, but most others use one of these two basic systems.

Table 15-1: Major Packaging System Families
Packaging System Distributions (Partial Listing)
Debian Style (.deb) Debian, Ubuntu, Xandros, Linspire
Red Hat Style (.rpm) Fedora, CentOS, Red Hat Enterprise Linux, OpenSUSE, Mandriva, PCLinuxOS

Debian Style (.deb) Debian, Ubuntu, Xandros, Linspire
Red Hat Style (.rpm) Fedora, CentOS, Red Hat Enterprise Linux, OpenSUSE, Mandriva, PCLinuxOS

### # 软件包管理系统是怎样工作的

The method of software distribution found in the proprietary software industry usually entails buying a piece of installation media such as an "install disk" and then running an "installation wizard" to install a new application on the system.

Linux doesn't work that way. Virtually all software for a Linux system will be found on the Internet. Most of it will be provided by the distribution vendor in the form of package files and the rest will be available in source code form that can be installed manually. We'll talk a little about how to install software by compiling source code in a later chapter.

Linux 不是这样。Linux 系统中几乎所有的软件都可以在互联网上找到。其中大多数软件由发行商以 包文件的形式提供，剩下的则以源码形式存在，可以手动安装。在后面章节里，我们将会谈谈怎样 通过编译源码来安装软件。

### # 包文件

The basic unit of software in a packaging system is the package file. A package file is a compressed collection of files that comprise the software package. A package may consist of numerous programs and data files that support the programs. In addition to the files to be installed, the package file also includes metadata about the package, such as a text description of the package and its contents. Additionally, many packages contain pre- and post-installation scripts that perform configuration tasks before and after the package installation.

Package files are created by a person known as a package maintainer, often (but not always) an employee of the distribution vendor. The package maintainer gets the software in source code form from the upstream provider (the author of the program), compiles it, and creates the package metadata and any necessary installation scripts. Often, the package maintainer will apply modifications to the original source code to improve the program's integration with the other parts of the Linux distribution.

### # 资源库

While some software projects choose to perform their own packaging and distribution, most packages today are created by the distribution vendors and interested third parties. Packages are made available to the users of a distribution in central repositories that may contain many thousands of packages, each specially built and maintained for the distribution.

A distribution may maintain several different repositories for different stages of the software development life cycle. For example, there will usually be a “testing” repository that contains packages that have just been built and are intended for use by brave souls who are looking for bugs before they are released for general distribution. A distribution will often have a “development” repository where work-in-progress packages destined for inclusion in the distribution's next major release are kept.

A distribution may also have related third-party repositories. These are often needed to supply software that, for legal reasons such as patents or DRM anti-circumvention issues, cannot be included with the distribution. Perhaps the best known case is that of encrypted DVD support, which is not legal in the United States. The third-party repositories operate in countries where software patents and anti-circumvention laws do not apply. These repositories are usually wholly independent of the distribution they support and to use them, one must know about them and manually include them in the configuration files for the package management system.

### # 依赖性

Programs seldom stand alone; rather, they rely on the presence of other software components to get their work done. Common activities, such as input/output for example, are handled by routines shared by many programs. These routines are stored in what are called shared libraries, which provide essential services to more than one program. If a package requires a shared resource such as a shared library, it is said to have a dependency. Modern package management systems all provide some method of dependency resolution to ensure that when a package is installed, all of its dependencies are installed, too.

### # 上层和底层软件包工具

Package management systems usually consist of two types of tools: low-level tools which handle tasks such as installing and removing package files, and high-level tools that perform metadata searching and dependency resolution. In this chapter, we will look at the tools supplied with Debian-style systems (such as Ubuntu and many others) and those used by recent Red Hat products. While all Red Hat-style distributions rely on the same low-level program (rpm), they use different high-level tools. For our discussion, we will cover the high-level program yum, used by Fedora, Red Hat Enterprise Linux, and CentOS. Other Red Hat-style distributions provide high-level tools with comparable features.

Table15- 2: Packaging System Tools
Distributions Low-Level Tools High-Level Tools
Debian-Style dpkg apt-get, aptitude
Fedora, Red Hat Enterprise Linux, CentOS rpm yum

Debian-Style dpkg apt-get, aptitude
Fedora, Red Hat Enterprise Linux, CentOS rpm yum

### # 常见软件包管理任务

There are many operations that can be performed with the command line package management tools. We will look at the most common. Be aware that the low-level tools also support creation of package files, an activity outside the scope of this book. In the discussion below, the term "package_name" refers to the actual name of a package rather than the term “package_file,” which is the name of the file that contains the package.

### # 查找资源库中的软件包

Using the high-level tools to search repository metadata, a package can be located based on its name or description.

Table 15-3: Package Search Commands
Style Command(s)
Debian apt-get update; apt-cache search search_string
Red Hat yum search search_string

Debian apt-get update; apt-cache search search_string
Red Hat yum search search_string

Example: To search a yum repository for the emacs text editor, this command could be used:

yum search emacs


### # 从资源库中安装一个软件包

High-level tools permit a package to be downloaded from a repository and installed with full dependency resolution.

Table 15-4: Package Installation Commands
Style Command(s)
Debian apt-get update; apt-get install package_name
Red Hat yum install package_name

Debian apt-get update; apt-get install package_name
Red Hat yum install package_name

Example: To install the emacs text editor from an apt repository:

apt-get update; apt-get install emacs


### # 通过软件包文件来安装软件

If a package file has been downloaded from a source other than a repository, it can be installed directly (though without dependency resolution) using a low-level tool.

Table 15-5: Low-Level Package Installation Commands
Style Command(s)
Debian dpkg --install package_file
Red Hat rpm -i package_file

Debian dpkg --install package_file
Red Hat rpm -i package_file

Example: If the emacs-22.1-7.fc7-i386.rpm package file had been downloaded from a non-repository site, it would be installed this way:

rpm -i emacs-22.1-7.fc7-i386.rpm


Note: Since this technique uses the low-level rpm program to perform the installation, no dependency resolution is performed. If rpm discovers a missing dependency, rpm will exit with an error.

### # 卸载软件

Packages can be uninstalled using either the high-level or low-tools. The high-level tools are shown below.

Table15-6: Package Removal Commands
Style Command(s)
Debian apt-get remove package_name
Red Hat yum erase package_name

Debian apt-get remove package_name
Red Hat yum erase package_name

Example: To uninstall the emacs package from a Debian-style system:

apt-get remove emacs


### # 经过资源库来更新软件包

The most common package management task is keeping the system up-to-date with the latest packages. The high-level tools can perform this vital task in one single step.

Table 15-7: Package Update Commands
Style Command(s)
Red Hat yum update

Red Hat yum update

Example: To apply any available updates to the installed packages on a Debian-style system:

apt-get update; apt-get upgrade


### # 经过软件包文件来升级软件

If an updated version of a package has been downloaded from a non-repository source, it can be installed, replacing the previous version:

Table 15-8: Low-Level Package Upgrade Commands
Style Command(s)
Debian dpkg --install package_file
Red Hat rpm -U package_file

Debian dpkg --install package_file
Red Hat rpm -U package_file

Example: Updating an existing installation of emacs to the version contained in the package file emacs-22.1-7.fc7-i386.rpm on a Red Hat system:

rpm -U emacs-22.1-7.fc7-i386.rpm


Note: dpkg does not have a specific option for upgrading a package versus installing one as rpm does.

### # 列出所安装的软件包

These commands can be used to display a list of all the packages installed on the system:

Table 15-9: Package Listing Commands
Style Command(s)
Debian dpkg --list
Red Hat rpm -qa

Debian dpkg --list
Red Hat rpm -qa

### # 确定是否安装了一个软件包

These low-level tools can be used to display whether a specified package is installed:

Table 15-10: Package Status Commands
Style Command(s)
Debian dpkg --status package_name
Red Hat rpm -q package_name

Debian dpkg --status package_name
Red Hat rpm -q package_name

Example: To determine if the emacs package is installed on a Debian style system:

dpkg --status emacs


### # 显示所安装软件包的信息

If the name of an installed package is known, the following commands can be used to display a description of the package:

Table 15-11: Package Information Commands
Style Command(s)
Debian apt-cache show package_name
Red Hat yum info package_name

Debian apt-cache show package_name
Red Hat yum info package_name

Example: To see a description of the emacs package on a Debian-style system:

apt-cache show emacs


### # 查找安装了某个文件的软件包

To determine what package is responsible for the installation of a particular file, the following commands can be used:

Table 15-12: Package File Identification Commands
Style Command(s)
Debian dpkg --search file_name
Red Hat rpm -qf file_name

Debian dpkg --search file_name
Red Hat rpm -qf file_name

Example: To see what package installed the /usr/bin/vim file on a Red Hat system:

rpm -qf /usr/bin/vim


### # 总结归纳

In the chapters that follow, we will explore many different programs covering a wide range of application areas. While most of these programs are commonly installed by default, we may need to install additional packages if necessary programs are not already installed on our system. With our newfound knowledge (and appreciation) of package management, we should have no problem installing and managing the programs we need.

The Linux Software Installation Myth

Linux 软件安装谣言

People migrating from other platforms sometimes fall victim to the myth that software is somehow difficult to install under Linux and that the variety of packaging schemes used by different distributions is a hindrance. Well, it is a hindrance, but only to proprietary software vendors who wish to distribute binary- only versions of their secret software.

The Linux software ecosystem is based on the idea of open source code. If a program developer releases source code for a product, it is likely that a person associated with a distribution will package the product and include it in their repository. This method ensures that the product is well integrated into the distribution and the user is given the convenience of “one-stop shopping” for software, rather than having to search for each product's web site.

Linux 软件生态系统是基于开放源代码理念。如果一个程序开发人员发布了一款产品的 源码，那么与系统发行版相关联的开发人员可能就会把这款产品打包，并把它包含在 他们的资源库中。这种方法保证了这款产品能很好地与系统发行版整合在一起，同时为用户 “一站式采购”软件提供了方便，从而用户不必去搜索每个产品的网站。

Device drivers are are handled in much the same way, except that instead of being separate items in a distribution's repository, they become part of the Linux kernel itself. Generally speaking, there is no such thing as a “driver disk” in Linux. Either the kernel supports a device or it doesn't, and the Linux kernel supports a lot of devices. Many more, in fact, than Windows does. Of course, this is of no consolation if the particular device you need is not supported. When that happens, you need to look at the cause. A lack of driver support is usually caused by one of three things:

1. The device is too new. Since many hardware vendors don't actively support Linux development, it falls upon a member of the Linux community to write the kernel driver code. This takes time.

2. The device is too exotic. Not all distributions include every possible device driver. Each distribution builds their own kernels, and since kernels are very configurable (which is what makes it possible to run Linux on everything from wristwatches to mainframes) they may have overlooked a particular device. By locating and downloading the source code for the driver, it is possible for you (yes, you) to compile and install the driver yourself. This process is not overly difficult, but it is rather involved. We'll talk about compiling software in a later chapter.

3. The hardware vendor is hiding something. They have neither released source code for a Linux driver, nor have they released the technical documentation for somebody to create one for them. This means that the hardware vendor is trying to keep the programming interfaces to the device a secret. Since we don't want secret devices in our computers, I suggest that you remove the offending hardware and pitch it into the trash, with your other useless items. ^

4. 设备太新。 因为许多硬件供应商没有积极地支持 Linux 的发展，那么编写内核 驱动代码的任务就由一些 Linux 社区来承担，而这需要花费时间。

5. 设备太奇异。 不是所有的发行版都包含每个可能的设备驱动。每个发行版会建立 它们自己的内核，因为内核是可以配置的（这使得从手表到主机的每台设备上运行 Linux 成为可能）， 这样它们可能会忽略某个特殊设备。通过定位和下载驱动程序的源码，可能需要你自己（是的，由你） 来编译和安装驱动。这个过程不是很难，而是需要参与的。我们将在随后的章节里来讨论编译软件。

6. 硬件供应商隐藏信息。 他们既不发布应用于 Linux 系统的驱动程序代码， 也不发布技术文档来让某人创建它。这意味着硬件供应商试图保密此设备的程序接口。因为我们 不想在计算机中使用保密的设备，所以我建议删除这令人厌恶的硬件， 把它和其它无用的东西都仍到垃圾桶里。

### # 拓展阅读

Spend some time getting to know the package management system for your distribution. Each distribution provides documentation for its package management tools. In addition, here are some more generic sources: