Linux 黑话解释：什么是包管理器？它是如何工作的？
包管理器
![](/data/attachment/album/202010/13/094827peunhns3nn3qceno.jpg)


[Linux 发行版之间有什么不同](https://itsfoss.com/what-is-linux/)的要点之一是包管理。在这篇 Linux 黑话解释中，你将了解 Linux 中的打包和包管理器。你将了解什么是包，什么是包管理器，它们是如何工作的，以及有什么包管理器。


### 什么是包管理器？


简单来说，“<ruby> 包管理器 <rt>  package manager </rt></ruby>”（或“软件包管理器”）是一种工具，它允许用户在操作系统上安装、删除、升级、配置和管理软件包。软件包管理器可以是像“软件中心”这样的图形化应用，也可以是像 [apt-get](https://itsfoss.com/apt-vs-apt-get-difference/) 或 [pacman](https://itsfoss.com/pacman-command/) 这样的命令行工具。


你会发现我经常在教程和文章中使用“包”这个词。要了解包管理器，你必须了解什么是包。


### 什么是包？


一个“<ruby> 包 <rt>  package </rt></ruby>”（或“软件包”）通常指的是一个应用程序，它可以是一个 GUI 应用程序、命令行工具或（其他软件程序需要的）软件库。包本质上是一个存档文件，包含二进制可执行文件、配置文件，有时还包含依赖关系的信息。


在旧时代，[软件曾经是从它的源代码安装的](https://itsfoss.com/install-software-from-source-code/)。你会参考一个文件（通常命名为 `README`），看看它需要什么软件组件、二进制文件的位置。它通常包括一个配置脚本或 `Makefile`。你必须自己编译该软件或自己处理所有的依赖关系（有些软件需要安装其他软件）。


为了摆脱这种复杂性，Linux 发行版创建了自己的打包格式，为终端用户提供随时可用的二进制文件（预编译软件），以便安装软件，同时提供一些[元数据](https://www.computerhope.com/jargon/m/metadata.htm)（版本号、描述）和依赖关系。


这就像烤蛋糕与买蛋糕一样。


![](/data/attachment/album/202010/13/094921ghir7i7gotg9mrio.png)


大约在上世纪 90 年代中期，Debian 创建了 DEB 打包格式（`.deb`），Red Hat Linux 创建了 RPM（Red Hat Package Manager 的缩写）打包系统（`.rpm`）。编译源代码的方式仍然存在，但现在是可选的。


要与打包系统交互或使用打包系统，你需要一个包管理器。


### 包管理器是如何工作的？


请记住，包管理器是一个通用的概念，它并不是 Linux 独有的。你会经常发现各种软件或编程语言的包管理器。有[只是针对 Python 包的 PIP 包管理器](https://itsfoss.com/install-pip-ubuntu/)。甚至 [Atom 编辑器也有自己的包管理器](https://itsfoss.com/install-packages-in-atom/)。


由于本文的重点是 Linux，所以我会从 Linux 的角度出发。不过，这里的大部分解释也可以应用于一般的包管理器。


我创建了这个图（基于 SUSE Wiki），这样你就可以很容易理解包管理器是如何工作的。


![](/data/attachment/album/202010/13/094925rqvfolwqzpjeqopk.png)


几乎所有的 Linux 发行版都有“<ruby> 软件仓库 <rt>  software repository </rt></ruby>”，它基本上是软件包的集合。是的，可以有不止一个软件库。软件库包含不同种类的软件包。


软件仓库也有元数据文件，其中包含了软件包的信息，如软件包的名称、版本号、软件包的描述和软件仓库名称等。这就是你在 Ubuntu/Debian 中使用 [apt show 命令](https://itsfoss.com/apt-search-command/)所看到的。


你的系统上的包管理器首先会与元数据进行交互。包管理器在你的系统上创建了一个元数据的本地缓存。当你运行包管理器的更新选项（例如 `apt update`）时，它会通过引用仓库中的元数据来更新本地元数据缓存。


当你运行软件包管理器的安装命令（例如 `apt install package_name`）时，软件包管理器会引用这个缓存。如果它在缓存中找到了包的信息，它就会使用互联网连接到相应的仓库，并在你的系统上安装之前先下载包。


一个包可能有依赖关系。意思是说，它可能需要安装其他软件包。软件包管理器通常会处理这些依赖关系，并将其与你正在安装的软件包一起自动安装。


![Linux 中包管理器会处理依赖关系](/data/attachment/album/202010/13/094927hyaf66qj9s0lwfpq.png)


同样，当你使用包管理器删除一个包时，它要么自动删除，要么通知你系统有未使用的包可以清理。


除了安装、删除这些显而易见的任务外，你还可以使用包管理器对包进行配置，并根据自己的需要进行管理。例如，你可以在常规的系统更新中[防止升级某个包的版本](https://itsfoss.com/prevent-package-update-ubuntu/)。你的包管理器可能还能做很多事情。


### 不同种类的包管理器


包管理器因打包系统而异，但同一打包系统却可能有多个包管理器。


例如，RPM 有 [Yum](https://fedoraproject.org/wiki/Yum) 和 [DNF](https://fedoraproject.org/wiki/DNF) 包管理器。对于 DEB，你有 `apt-get`、[aptitude](https://wiki.debian.org/Aptitude) 等基于命令行的包管理器。


![Synaptic 包管理器](/data/attachment/album/202010/13/094934affzuucfz0stkk8u.png)


软件包管理器不一定是基于命令行的，也有图形化的软件包管理工具，比如 [Synaptic](https://itsfoss.com/synaptic-package-manager/)。你的发行版的“软件中心”也是一个软件包管理器，即使它在底层运行的是 `apt-get` 或 DNF。


### 结论


我不想进一步详细介绍这个话题，虽然我可以继续说下去，但这将偏离本主题的目标 —— 即让你对 Linux 中的包管理器有一个基本的了解。


我暂时忽略了新的通用打包格式，比如 Snap 和 Flatpak。


我希望你对 Linux 中的包管理系统有更好的理解。如果你还有困惑，或者你对这个主题有一些问题，请发表评论。我会尽量回答你的问题，如果需要的话，我会在本文中更新新的内容。




---


via: <https://itsfoss.com/package-manager/>


作者：[Abhishek Prakash](https://itsfoss.com/author/abhishek/) 选题：[lujun9972](https://github.com/lujun9972) 译者：[wxy](https://github.com/wxy) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
![Warp Terminal](/assets/images/warp-terminal.webp)


![Warp Terminal](/assets/images/warp-terminal.webp)

One of the main points [how Linux distributions differ from each other](https://itsfoss.com/what-is-linux/) is the package management. In this part of the Linux jargon buster series, you’ll learn about packaging and package managers in Linux. You’ll learn what are packages, what are package managers and how do they work and what kind of package managers available.

## What is a package manager in Linux?

In simpler words, a package manager is a tool that allows users to install, remove, upgrade, configure and manage software packages on an operating system. The package manager can be a graphical application like a software center or a command line tool like [apt-get](https://itsfoss.com/apt-vs-apt-get-difference/) or [pacman](https://itsfoss.com/pacman-command/).

You’ll often find me using the term ‘package’ in tutorials and articles on It’s FOSS. To understand package manager, you must understand what a package is.

## What is a package?

A package is usually referred to an application but it could be a GUI application, command line tool or a software library (required by other software programs). A package is essentially an archive file containing the binary executable, configuration file and sometimes information about the dependencies.

In older days, [software used to installed from its source code](https://itsfoss.com/install-software-from-source-code/). You would refer to a file (usually named readme) and see what software components it needs, location of binaries. A configure script or makefile is often included. You will have to compile the software or on your own along with handling all the dependencies (some software require installation of other software) on your own.

To get rid of this complexity, Linux distributions created their own packaging format to provide the end users ready-to-use binary files (precompiled software) for installing software along with some [metadata](https://www.computerhope.com/jargon/m/metadata.htm?ref=itsfoss.com) (version number, description) and dependencies.

It is like baking a cake versus buying a cake.

![Source Code Compilation Vs Packaging in Linux](https://itsfoss.com/content/images/wordpress/2020/10/source-code-comilation-vs-packaging.png)

Around mid 90s, Debian created .deb or DEB packaging format and Red Hat Linux created .rpm or RPM (short for Red Hat Package Manager) packaging system. Compiling source code still exists but it is optional now.

To interact with or use the packaging systems, you need a package manager.

## How does the package manager work?

Please keep in mind that package manager is a generic concept and it’s not exclusive to Linux. You’ll often find package manager for different software or programming languages. There is [PIP package manager just for Python packages](https://itsfoss.com/install-pip-ubuntu/). Even [Atom editor has its own package manager](https://itsfoss.com/install-packages-in-atom/).

Since the focus in this article is on Linux, I’ll take things from Linux’s perspective. However, most of the explanation here could be applied to package manager in general as well.

I have created this diagram (based on SUSE Wiki) so that you can easily understand how a package manager works.

![Linux Package Manager Explanation](https://itsfoss.com/content/images/wordpress/2020/10/linux-package-manager-explanation.png)

Almost all Linux distributions have software repositories which is basically collection of software packages. Yes, there could be more than one repository. The repositories contain software packages of different kind.

Repositories also have metadata files that contain information about the packages such as the name of the package, version number, description of package and the repository name etc. This is what you see if you use the [apt show command](https://itsfoss.com/apt-search-command/) in Ubuntu/Debian.

Your system’s package manager first interacts with the metadata. The package manager creates a local cache of metadata on your system. When you run the update option of the package manager (for example apt update), it updates this local cache of metadata by referring to metadata from the repository.

When you run the installation command of your package manager (for example apt install package_name), the package manager refers to this cache. If it finds the package information in the cache, it uses the internet connection to connect to the appropriate repository and downloads the package first before installing on your system.

A package may have dependencies. Meaning that it may require other packages to be installed. The package manager often takes care of the dependencies and installs it automatically along with the package you are installing.

![Package Manager Handling Dependencies In Linux](https://itsfoss.com/content/images/wordpress/2020/10/package-manager-handling-dependencies-in-linux.png)

Similarly, when you remove a package using the package manager, it either automatically removes or informs you that your system has unused packages that can be cleaned.

Apart from the obvious tasks of installing, removing, you can use the package manager to configure the packages and manage them as per your need. For example, you can [prevent the upgrade of a package version](https://itsfoss.com/prevent-package-update-ubuntu/) from the regular system updates. There are many more things your package manager might be capable of.

## Different kinds of package managers

Package Managers differ based on packaging system but same packaging system may have more than one package manager.

For example, RPM has [Yum](https://fedoraproject.org/wiki/Yum?ref=itsfoss.com) and [DNF](https://fedoraproject.org/wiki/DNF?ref=itsfoss.com) package managers. For DEB, you have apt-get, [aptitude](https://wiki.debian.org/Aptitude?ref=itsfoss.com) command line based package managers.

![See Packages By Repositories Synaptic](https://itsfoss.com/content/images/wordpress/2020/06/see-packages-by-repositories-synaptic.png)

Package managers are not necessarily command line based. You have graphical package managing tools like [Synaptic](https://itsfoss.com/synaptic-package-manager/). Your distribution’s software center is also a package manager even if it runs apt-get or DNF underneath.

## Conclusion

I don’t want to go in further detail on this topic because I can go on and on. But it will deviate from the objective of the topic which is to give you a basic understanding of package manager in Linux.

I have omitted the new universal packaging formats like Snap and Flatpak for now.

I do hope that you have a bit better understanding of the package management system in Linux. If you are still confused or if you have some questions on this topic, please use the comment system. I’ll try to answer your questions and if required, update this article with new points.