如何在基于 Ubuntu/Debian 的 Linux 发行版中检查包的依赖关系

在 Ubuntu/Debian 中通过命令行安装应用程序非常容易。 您需要做的就是使用 apt install package_name。

但是如果你想在安装之前或之后知道一个包的依赖关系怎么办?

在本教程中,我将向您展示在 Ubuntu 和其他基于 Debian 的 Linux 发行版中查看软件包依赖项的各种方法 APT包管理系统.

Ubuntu 中的包依赖是什么?

如果您还不知道,当您在 Linux 中安装软件包时,有时需要其他软件包才能正常运行。 这些附加包称为依赖项。 如果系统上没有安装这些依赖包,通常会随包自动安装。

例如,用于转换视频格式的 GUI 工具 HandBrake 需要 FFmpeg, GStreamer. 所以对于 HandBrake,FFmpeg 和 GStreamer 是依赖。

如果您的系统上没有安装这些软件包,当您在 Ubuntu 上安装 HandBrake 时,它​​们会自动安装。

在基于 Ubuntu 和 Debian 的发行版中检查软件包的依赖关系

正如在 Linux 中经常发生的那样,实现相同结果的方法不止一种。 让我们看看各种方式来查看包的依赖关系。

使用 apt show 检查依赖项

您可以使用 apt show 命令来显示包的详细信息。 此信息的一部分是依赖关系,您可以在以 Depends 开头的行中看到它。

例如,这是它为 ubuntu-restricted-extras 包显示的内容。

[email protected]:~$ apt show ubuntu-restricted-extras  Package: ubuntu-restricted-extras Version: 67 Priority: optional Section: multiverse/metapackages Origin: Ubuntu Maintainer: Ubuntu Developers <[email protected]> Bugs: https://bugs.launchpad.net/ubuntu/+filebug Installed-Size: 14.3 kB Depends: ubuntu-restricted-addons Recommends: libavcodec-extra, ttf-mscorefonts-installer, unrar Download-Size: 3,200 B APT-Manual-Installed: yes APT-Sources: https://us.archive.ubuntu.com/ubuntu focal/multiverse amd64 Packages Description: Commonly used media codecs and fonts for Ubuntu  This collection of packages includes:   - MP3 and other audio codec software to play various audio formats     (GStreamer plugins)   - software to install the Microsoft Web fonts   - the Adobe Flash plugin   - LAME, software to create compressed audio files.  .  This software does not include libdvdcss2, and will not let you play  encrypted DVDs. For more information, see  https://help.ubuntu.com/community/RestrictedFormats/PlayingDVDs  .  These software packages are from the Multiverse channel, restricted by  copyright or legal issues in some countries. For more information, see  https://www.ubuntu.com/ubuntu/licensing

如您所见,ubuntu-restricted-extras 包依赖于 ubuntu-restricted-addons 包。

这是一个问题! 依赖包也可能依赖于其他一些包,并且链条可以继续。 幸运的是,APT 包管理器通过自动安装所有依赖项(大部分时间)来为您处理这个问题。

什么是推荐套餐?

您是否注意到以上输出中以 Recommends 开头的行?

推荐的包不是包的直接依赖项,但它们启用了附加功能。

如您所见,ubuntu-restricted-extras 有 ttf-mscorefonts-installer 作为在 Ubuntu 上安装 Microsoft Fonts 的推荐包。

默认情况下也会安装推荐的软件包,如果您明确要禁止安装推荐的软件包,请使用 –no-install-recommends 标志,如下所示:

sudo apt install –no-install-recommends package_name

使用 apt-cache 只获取依赖信息

apt 节目的信息太多了。 如果您想获取脚本中的依赖项,apt-cache 命令可为您提供更好、更干净的输出。

apt-cache depends package_name

输出看起来很干净,不是吗?

使用 dpkg 检查 DEB 文件的依赖关系

apt 和 apt-cache 命令都适用于存储库中可用的包。 但是如果你下载一个 DEB 文件,这些命令将不起作用。

在这种情况下,您可以使用带有 -I 或 –info 选项的 dpkg 命令。

dpkg -I path_to_deb_file

依赖关系可以在以 Depends 开头的行中看到。

使用 apt-rdepends 检查依赖项和反向依赖项

如果您想了解有关依赖项的更多详细信息,可以使用 apt-rdepends 工具。 此工具创建完整的依赖关系树。 因此,您将获得包的依赖项以及依赖项的依赖项。

它不是常规的 apt 命令,您必须从 Universe 存储库安装它:

sudo apt install apt-rdepends

根据依赖关系树,输出通常非常大。

Reading package lists... Done Building dependency tree        Reading state information... Done shutter     Depends: procps   Depends: xdg-utils imagemagick   Depends: imagemagick-6.q16 (>= 8:6.9.2.10+dfsg-2~) imagemagick-6.q16   Depends: hicolor-icon-theme   Depends: libc6 (>= 2.4)   Depends: libmagickcore-6.q16-6 (>= 8:6.9.10.2)   Depends: libmagickwand-6.q16-6 (>= 8:6.9.10.2) hicolor-icon-theme libc6   Depends: libcrypt1 (>= 1:4.4.10-10ubuntu4)   Depends: libgcc-s1 libcrypt1   Depends: libc6 (>= 2.25)

apt-rdepends 工具非常通用。 它还可以计算反向依赖关系。 这意味着,您可以看到其他包依赖于某个包。

apt-rdepends -r package_name

输出可能非常大,因为它将打印反向依赖树。

[email protected]:~$ apt-rdepends -r ffmpeg Reading package lists... Done Building dependency tree        Reading state information... Done ffmpeg   Reverse Depends: ardour-video-timeline (>= 1:5.12.0-3ubuntu4)   Reverse Depends: deepin-screen-recorder (5.0.0-1build2)   Reverse Depends: devede (4.15.0-2)   Reverse Depends: dvd-slideshow (0.8.6.1-1)   Reverse Depends: green-recorder (>= 3.2.3)

我希望这个快速教程对提高您的命令行知识有所帮助。 请继续关注更多此类提示。