Linux apt 包管理工具

点击访问 Linux 命令大全 >>

前面章节介绍了 rpm 和 dpkg 软件包管理命令,它们大大减少了安装软件的工作量。遗憾的是,rpm 和 dpkg 工具并没有有效的解决依赖性问题。为了安装某个软件,管理员不得不常常陷入“A 依赖 B,B 依赖 C,C 依赖 D……”这类无休止的纠缠中。为了解决依赖问题,以 APT、yum 等高级软件包管理工具应运而生。本章节将介绍 APT 软件包管理工具。

APT 简介

APT 全称为“Advanced Package Tool”即高级软件包工具;这是现今最成熟的软件包管理系统,它可以自动检测软件依赖问题,下载和安装所有文件;甚至只需要一条命令,就可以更新整个系统上所有的软件包。

APT 最初被设计运行于 Debian 系统上,只支持 .deb 格式的软件包文件。如今,APT 已经被移植到使用 PRM 软件包机制的发行版上。可以从 apt-rpm.org 获得 APT 的 RPM 版本。

APT 工具最常用的有两个命令:

  • apt-get:用于执行和软件包安装有关的所有操作;

  • apt-cache:用于查找软件包的相关信息;

下载和安装软件包

系统第一次启动时,需要运行 apt-get update 更新的当前 apt-get 缓存中的软件包信息。此后,就可以使用 apt-get install 命令安装软件包了。

(1)使用 apt-get update 更新软件包信息。如下:

snow@ubuntu:~$ sudo apt-get update
Get:1 https://security.ubuntu.com/ubuntu focal-security InRelease [107 kB]
Hit:2 https://us.archive.ubuntu.com/ubuntu focal InRelease
Get:3 https://us.archive.ubuntu.com/ubuntu focal-updates InRelease [111 kB]
Get:4 https://security.ubuntu.com/ubuntu focal-security/main amd64 DEP-11 Metadata [21.2 kB]
Get:5 https://security.ubuntu.com/ubuntu focal-security/universe amd64 DEP-11 Metadata [35.7 kB]
Get:6 https://us.archive.ubuntu.com/ubuntu focal-backports InRelease [98.3 kB]
Get:7 https://us.archive.ubuntu.com/ubuntu focal-updates/main amd64 Packages [315 kB]
Get:8 https://us.archive.ubuntu.com/ubuntu focal-updates/main amd64 DEP-11 Metadata [196 kB]
Get:9 https://us.archive.ubuntu.com/ubuntu focal-updates/main amd64 c-n-f Metadata [7,964 B]
Get:10 https://us.archive.ubuntu.com/ubuntu focal-updates/universe amd64 Packages [146 kB]
Get:11 https://us.archive.ubuntu.com/ubuntu focal-updates/universe amd64 DEP-11 Metadata [176 kB]
Get:12 https://us.archive.ubuntu.com/ubuntu focal-updates/universe amd64 c-n-f Metadata [4,920 B]
Get:13 https://us.archive.ubuntu.com/ubuntu focal-updates/multiverse amd64 DEP-11 Metadata [2,468 B]
Ign:11 https://us.archive.ubuntu.com/ubuntu focal-updates/universe amd64 DEP-11 Metadata
Get:14 https://us.archive.ubuntu.com/ubuntu focal-backports/universe amd64 DEP-11 Metadata [1,976 B]
Get:11 https://us.archive.ubuntu.com/ubuntu focal-updates/universe amd64 DEP-11 Metadata [232 kB]
Fetched 1,167 kB in 18s (63.6 kB/s)
Reading package lists... Done

(2)使用 apt-get install 安装 tree 软件。如下:

## 查看 tree 命令是否存在
snow@ubuntu:~$ tree
-bash: /usr/bin/tree: No such file or directory

## 使用 sudo 权限安装 tree 软件
snow@ubuntu:~$ sudo apt-get install tree
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following NEW packages will be installed:
  tree
0 upgraded, 1 newly installed, 0 to remove and 152 not upgraded.
Need to get 43.0 kB of archives.
After this operation, 115 kB of additional disk space will be used.
Get:1 https://us.archive.ubuntu.com/ubuntu focal/universe amd64 tree amd64 1.8.0-1 [43.0 kB]
Fetched 43.0 kB in 15s (2,864 B/s)
Selecting previously unselected package tree.
(Reading database ... 189829 files and directories currently installed.)
Preparing to unpack .../tree_1.8.0-1_amd64.deb ...
Unpacking tree (1.8.0-1) ...
Setting up tree (1.8.0-1) ...
Processing triggers for man-db (2.9.1-1) ...

## 使用 tree 软件查看当前目录的树形结构
snow@ubuntu:~$ tree
.
├── deb
│   ├── net-tools_1.60-25ubuntu2_amd64.deb
│   ├── tree_1.6.0-1_amd64.deb
│   └── vim_7.4.052-1ubuntu3_amd64.deb
├── Desktop
├── Documents
├── Downloads
├── Music
├── Pictures
├── Public
├── snap
│   └── snap-store
│       ├── 433
│       ├── 467
│       ├── common
│       └── current -> 467
├── Templates
└── Videos

15 directories, 3 files

更新软件包

可使用 apt-get upgrade 命令将指定的软件更新到最新版本。例如:

snow@ubuntu:~$ sudo apt-get upgrade tree
Reading package lists... Done
Building dependency tree
Reading state information... Done
Calculating upgrade... Done
The following package was automatically installed and is no longer required:
  libfreetype6
Use 'sudo apt autoremove' to remove it.
The following packages have been kept back:
  lxd lxd-client netplan.io
The following packages will be upgraded:
......

卸载软件包

可使用 apt-get remove 命令卸载特定的软件包。例如:

## 卸载 tree 软件
snow@ubuntu:~$ sudo apt-get remove tree
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following package was automatically installed and is no longer required:
  libfreetype6
Use 'sudo apt autoremove' to remove it.
The following packages will be REMOVED:
  tree
0 upgraded, 0 newly installed, 1 to remove and 240 not upgraded.
After this operation, 105 kB disk space will be freed.
Do you want to continue? [Y/n] Y
(Reading database ... 34128 files and directories currently installed.)
Removing tree (1.7.0-5) ...
Processing triggers for man-db (2.8.3-2) ...

删除软件包

可使用 apt-get clean 命令删除所有已下载的软件包文件。例如:

snow@ubuntu:~$ sudo apt-get clean

查询软件包信息

apt-cache search 命令可以搜索软件包列表中特定的软件包。例如:

## 查询 tree 软件包
snow@ubuntu:~$ sudo apt-cache search tree
augeas-doc - Augeas lenses documentation
augeas-lenses - Set of lenses needed by libaugeas0 to parse config files
baobab - GNOME disk usage analyzer
bcache-tools - bcache userspace tools
blt - graphics extension library for Tcl/Tk - run-time
blt-dev - graphics extension library for Tcl/Tk - development files
bzrtools - Collection of tools for bzr
device-tree-compiler - Device Tree Compiler for Flat Device Trees
devscripts - scripts to make the life of a Debian Package maintainer easier
dnsutils - Clients provided with BIND
erlang-syntax-tools - Erlang/OTP modules for handling abstract Erlang syntax trees
genisoimage - Creates ISO-9660 CD-ROM filesystem images
git - fast, scalable, distributed revision control system
...... 太多了 ......

## 结合 grep 命令,可以精确查找到我们需要的软件包 tree
snow@ubuntu:~$ sudo apt-cache search tree | grep ^tree
tree - displays an indented directory tree, in color
tree-ppuzzle - Parallelized reconstruction of phylogenetic trees by maximum likelihood
tree-puzzle - Reconstruction of phylogenetic trees by maximum likelihood
tree-puzzle-doc - Reconstruction of phylogenetic trees by maximum likelihood (doc)
treeline - versatile tree-like structured custom data manager
treesheets - Data organizer that covers spreadsheets, mind mappers, and small databases
treetop - Ruby-based text parsing and interpretation (command-line utility)
treeviewx - Displays and prints phylogenetic trees
treeview - Java re-implementation of Michael Eisen's TreeView

查看帮助信息

可以使用 apt-get -h 命令查看 apt-get 命令的帮助信息。例如:

snow@ubuntu:~$ apt-get -h
apt 2.0.2ubuntu0.1 (amd64)
Usage: apt-get [options] command
       apt-get [options] install|remove pkg1 [pkg2 ...]
       apt-get [options] source pkg1 [pkg2 ...]

apt-get is a command line interface for retrieval of packages
and information about them from authenticated sources and
for installation, upgrade and removal of packages together
with their dependencies.

Most used commands:
  update - Retrieve new lists of packages
  upgrade - Perform an upgrade
  install - Install new packages (pkg is libc6 not libc6.deb)
  reinstall - Reinstall packages (pkg is libc6 not libc6.deb)
  remove - Remove packages
  purge - Remove packages and config files
  autoremove - Remove automatically all unused packages
  dist-upgrade - Distribution upgrade, see apt-get(8)
  dselect-upgrade - Follow dselect selections
  build-dep - Configure build-dependencies for source packages
  satisfy - Satisfy dependency strings
  clean - Erase downloaded archive files
  autoclean - Erase old downloaded archive files
  check - Verify that there are no broken dependencies
  source - Download source archives
  download - Download the binary package into the current directory
  changelog - Download and display the changelog for the given package

See apt-get(8) for more information about the available commands.
Configuration options and syntax is detailed in apt.conf(5).
Information about how to configure sources can be found in sources.list(5).
Package and version choices can be expressed via apt_preferences(5).
Security details are available in apt-secure(8).
                                        This APT has Super Cow Powers.

配置 apt-get

你知道 apt-get 从哪里下载的软件吗?所有 apt-get 用于下载软件的地址称为“安装源”,apt-get 的安装源全部放在 /etc/apt/sources.list 文件中。该文件内容如下:

snow@ubuntu:/etc/apt$ cat /etc/apt/sources.list
deb cdrom:[Ubuntu 20.04 LTS _Focal Fossa_ - Release amd64 (20200423)]/ focal main restricted
deb-src https://archive.ubuntu.com/ubuntu focal main restricted #Added by software-properties

# See https://help.ubuntu.com/community/UpgradeNotes for how to upgrade to
# newer versions of the distribution.
deb https://us.archive.ubuntu.com/ubuntu/ focal main restricted
deb-src https://us.archive.ubuntu.com/ubuntu/ focal main multiverse restricted universe

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

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

## N.B. software from this repository is ENTIRELY UNSUPPORTED by the Ubuntu
## team, and may not be under a free licence. Please satisfy yourself as to
## your rights to use the software. Also, please note that software in
## multiverse WILL NOT receive any review or updates from the Ubuntu
## security team.
deb https://us.archive.ubuntu.com/ubuntu/ focal multiverse
# deb-src https://us.archive.ubuntu.com/ubuntu/ focal multiverse
deb https://us.archive.ubuntu.com/ubuntu/ focal-updates multiverse
# deb-src https://us.archive.ubuntu.com/ubuntu/ focal-updates multiverse

## N.B. software from this repository may not have been tested as
## extensively as that contained in the main release, although it includes
## newer versions of some applications which may provide useful features.
## Also, please note that software in backports WILL NOT receive any review
## or updates from the Ubuntu security team.
deb https://us.archive.ubuntu.com/ubuntu/ focal-backports main restricted universe multiverse
deb-src https://us.archive.ubuntu.com/ubuntu/ focal-backports main restricted universe multiverse

## Uncomment the following two lines to add software from Canonical's
## 'partner' repository.
## This software is not part of Ubuntu, but is offered by Canonical and the
## respective vendors as a service to Ubuntu users.
# deb https://archive.canonical.com/ubuntu focal partner
# deb-src https://archive.canonical.com/ubuntu focal partner

deb https://security.ubuntu.com/ubuntu focal-security main restricted
deb-src https://security.ubuntu.com/ubuntu focal-security main multiverse restricted universe
deb https://security.ubuntu.com/ubuntu focal-security universe
# deb-src https://security.ubuntu.com/ubuntu focal-security universe
deb https://security.ubuntu.com/ubuntu focal-security multiverse
# deb-src https://security.ubuntu.com/ubuntu focal-security multiverse

# This system was installed using small removable media
# (e.g. netinst, live or single CD). The matching "deb cdrom"
# entries were disabled at the end of the installation process.
# For information about how to configure apt package sources,
# see the sources.list(5) manual.

上面的文档说明:

  • deb 和 deb-src:表示软件包类型,Debian 类型的软件包使用 deb 或 deb-src。如果是 RPM 软件包,则使用 rpm 和 rpm-src。注意:其中 src 表示源代码。

  • URL:表示指向 CD-ROM、HTTP 或者 FTP 服务地址,即从哪里下载软件包。

  • # 开头的行表示这一行是注释行,注释行在 apt-get 软件看来,是不存在,注释行是给我们看的。

说说我的看法
全部评论(
没有评论
关于
本网站属于个人的非赢利性网站,转载的文章遵循原作者的版权声明,如果原文没有版权声明,请来信告知:hxstrive@outlook.com
公众号