本页内容

  • 环境要求
  • 安装与更新软件包
  • 使用 Apt 移除软件包
  • 使用 Apt-Cache 搜索软件包
  • APT 高级用法

APT(又称 Advanced Packaging Tool)是用于管理 Debian 系发行版(如 Ubuntu 16.04、Ubuntu 18.04、Debian 8、Debian 9 等)中软件包的命令行工具。APT 简化了软件包的安装、移除与升级流程,甚至可用于通过命令行界面升级整个操作系统。

本教程将讲解如何在 Ubuntu 上使用 APT 命令行工具管理软件包。相关命令已在 Ubuntu 22.04 至 16.04 版本上测试通过。

环境要求

  • 一台运行 Ubuntu Server 或 Desktop 的服务器
  • 对该服务器拥有 root 或 sudo 访问权限。

以下所有命令均以 root 用户身份运行。可在 shell 中直接以 root 用户登录,或运行:

sudo -s

以切换为 root 用户。另一种方式是在所有命令前加上 'sudo '。

安装与更新软件包

apt-get 从不同来源获取信息并存储在本地数据库中。update 命令会从各来源获取软件包并将其更新到较新版本。

apt-get update -y

运行上述命令后,你的数据库应该已是最新状态。

upgrade 命令用于将所有当前已安装的软件包升级到较新版本。

apt-get upgrade -y

你也可以改用 dist-upgrade 命令升级软件包,它会以智能冲突解决方式改变软件包依赖关系。

apt-get dist-upgrade -y

数据库更新完成后,你可以运行以下命令安装任意软件包:

apt-get install package1 package2

例如,你可以运行以下命令安装 Nginx web server 软件包:

apt-get install nginx

如果你只想下载软件包文件而不安装,可以运行以下命令:

apt-get install -d package1

上述命令会将软件包文件下载到 /var/cache/apt/archives 目录。

要重新安装某个软件包到较新版本,运行以下命令:

apt-get install package1 --reinstall

使用 Apt 移除软件包

要从系统中移除某个软件包,运行以下命令:

apt-get remove package1

上述命令只会移除软件包,但保留其配置文件。

要连同配置文件一起移除软件包,运行以下命令:

apt-get purge package1

你也可以运行以下命令移除所有不再需要的软件包并清理数据库:

apt-get autoremove
apt-get clean

使用 Apt-Cache 搜索软件包

apt-cache 命令用于搜索软件包。

要根据软件包描述进行查找,运行以下命令:

apt-cache search proftpd

你将看到如下输出:

resource-agents - Cluster Resource Agents
fail2ban - ban hosts that cause multiple authentication errors
ftpd - File Transfer Protocol (FTP) server
gadmin-proftpd - GTK+ configuration tool for proftpd
gadmin-proftpd-dbg - GTK+ configuration tool for proftpd debug package
gadmintools - GTK+ server administration tools (meta-package)
proftpd-basic - Versatile, virtual-hosting FTP daemon - binaries
proftpd-dev - Versatile, virtual-hosting FTP daemon - development files
proftpd-doc - Versatile, virtual-hosting FTP daemon - documentation
proftpd-mod-autohost - ProFTPD module mod_autohost
proftpd-mod-case - ProFTPD module mod_case
proftpd-mod-clamav - ProFTPD module mod_clamav
proftpd-mod-dnsbl - ProFTPD module mod_dnsbl
proftpd-mod-fsync - ProFTPD module mod_fsync
proftpd-mod-geoip - Versatile, virtual-hosting FTP daemon - GeoIP module
proftpd-mod-ldap - Versatile, virtual-hosting FTP daemon - LDAP module
proftpd-mod-msg - ProFTPD module mod_msg
proftpd-mod-mysql - Versatile, virtual-hosting FTP daemon - MySQL module
proftpd-mod-odbc - Versatile, virtual-hosting FTP daemon - ODBC module
proftpd-mod-pgsql - Versatile, virtual-hosting FTP daemon - PostgreSQL module
proftpd-mod-sqlite - Versatile, virtual-hosting FTP daemon - SQLite3 module
proftpd-mod-tar - ProFTPD module mod_tar
proftpd-mod-vroot - ProFTPD module mod_vroot

要查找所有以 proftpd 开头的软件包,运行以下命令:

apt-cache pkgnames proftpd

你将看到如下输出:

apt-cache pkgnames proftpd
proftpd-mod-dnsbl
proftpd-mod-odbc
proftpd-mod-pgsql
proftpd-doc
proftpd-mod-tar
proftpd-mod-ldap
proftpd-mod-case
proftpd-mod-geoip
proftpd-mod-mysql
proftpd-basic
proftpd-mod-vroot
proftpd-mod-clamav
proftpd-mod-autohost
proftpd-mod-fsync
proftpd-mod-sqlite
proftpd-mod-msg
proftpd-dev

你可以使用以下命令查看任意软件包(此处以 nano 为例)的完整信息:

apt-cache show nano

输出:

Package: nano
Priority: standard
Section: editors
Installed-Size: 600
Maintainer: Ubuntu Developers <
[email protected]
>
Original-Maintainer: Jordi Mallach <
[email protected]
>
Architecture: amd64
Version: 2.2.6-1ubuntu1
Replaces: pico
Provides: editor
Depends: libc6 (>= 2.14), libncursesw5 (>= 5.6+20070908), libtinfo5, dpkg (>= 1.15.4) | install-info
Suggests: spell
Conflicts: pico
Breaks: alpine-pico (<= 2.00+dfsg-5)
Filename: pool/main/n/nano/nano_2.2.6-1ubuntu1_amd64.deb
Size: 194060
MD5sum: c97dc062e9941bfe13b6b303cf8ed639
SHA1: ee93fcfd1f2ecd601b0a8f8932319848043f4f0f
SHA256: f20d8cca5c30b90ebf68301d126f86e473ac83e7d6fdc36f59bcd685c2eb4020
Description-en: small, friendly text editor inspired by Pico
 GNU nano is an easy-to-use text editor originally designed as a replacement
 for Pico, the ncurses-based editor from the non-free mailer package Pine
 (itself now available under the Apache License as Alpine).
 .
 However, nano also implements many features missing in pico, including:
  - feature toggles;
  - interactive search and replace (with regular expression support);
  - go to line (and column) command;
  - auto-indentation and color syntax-highlighting;
  - filename tab-completion and support for multiple buffers;
  - full internationalization support.
Description-md5: b7e1d8c3d831118724cfe8ea3996b595
Homepage: http://www.nano-editor.org/
Bugs: https://bugs.launchpad.net/ubuntu/+filebug
Origin: Ubuntu
Supported: 5y
Task: standard, kubuntu-active, kubuntu-active

要查看某个特定软件包的依赖关系,运行以下命令:

apt-cache showpkg htop

输出:

Package: htop
Versions: 
2.1.0-3 (/var/lib/apt/lists/archive.ubuntu.com_ubuntu_dists_bionic_main_binary-amd64_Packages) (/var/lib/dpkg/status)
 Description Language: 
                 File: /var/lib/apt/lists/archive.ubuntu.com_ubuntu_dists_bionic_main_binary-amd64_Packages
                  MD5: 8eb5aa19b3c92a975dc78e2165f6688d
 Description Language: en
                 File: /var/lib/apt/lists/archive.ubuntu.com_ubuntu_dists_bionic_main_i18n_Translation-en
                  MD5: 8eb5aa19b3c92a975dc78e2165f6688d


Reverse Depends: 
  ubuntu-server,htop
  lubuntu-qt-desktop,htop
  lubuntu-gtk-desktop,htop
  lubuntu-desktop,htop
  util-vserver,htop
  ubuntu-benchmark-tools,htop
  lubuntu-qt-desktop,htop
  lubuntu-gtk-desktop,htop
  lubuntu-desktop,htop
  hollywood,htop
  freedombox-setup,htop
Dependencies: 
2.1.0-3 - libc6 (2 2.15) libncursesw5 (2 6) libtinfo5 (2 6) lsof (0 (null)) strace (0 (null)) 
Provides: 
2.1.0-3 - 
Reverse Provides: 

要检查某个软件包是否已安装以及它属于哪个 repository,运行以下命令:

apt-cache policy apache2

你将看到如下输出:

apache2:
  Installed: 2.4.29-1ubuntu4.5
  Candidate: 2.4.29-1ubuntu4.5
  Version table:
 *** 2.4.29-1ubuntu4.5 500
        500 http://archive.ubuntu.com/ubuntu bionic-updates/main amd64 Packages
        100 /var/lib/dpkg/status
     2.4.29-1ubuntu4.4 500
        500 http://security.ubuntu.com/ubuntu bionic-security/main amd64 Packages
     2.4.29-1ubuntu4 500
        500 http://archive.ubuntu.com/ubuntu bionic/main amd64 Packages

APT 高级用法

你也可以运行以下命令检查损坏的依赖关系。

apt-get check

你将看到如下输出:

Reading package lists... Done
Building dependency tree       
Reading state information... Done

要搜索并安装某个软件包的构建依赖(此处以 vsftpd 为例),使用以下命令:

apt-get build-dep vsftpd

在以下输出中你将看到 vsftpd 软件包所需的全部依赖:

Reading package lists... Done
Building dependency tree       
Reading state information... Done
Note, selecting 'libcap-dev' instead of 'libcap2-dev'
The following NEW packages will be installed:
  debhelper dh-apparmor dh-apport libcap-dev libpam0g-dev po-debconf
The following packages will be upgraded:
  libpam0g
1 upgraded, 6 newly installed, 0 to remove and 595 not upgraded.
1 not fully installed or removed.
Need to get 1,020 kB of archives.
After this operation, 2,238 kB of additional disk space will be used.
Do you want to continue? [Y/n] 

要下载任意软件包的源代码,并解包、编译该软件包,运行以下命令:

apt-get --compile source htop

你将看到如下输出:

Reading package lists... Done
NOTICE: 'htop' packaging is maintained in the 'Git' version control system at:
https://salsa.debian.org/debian/htop.git
Please use:
git clone https://salsa.debian.org/debian/htop.git
to retrieve the latest (possibly unreleased) updates to the package.
Need to get 314 kB of source archives.
Get:1 http://archive.ubuntu.com/ubuntu bionic/main htop 2.1.0-3 (dsc) [1,961 B]
Get:2 http://archive.ubuntu.com/ubuntu bionic/main htop 2.1.0-3 (tar) [303 kB]                                                                        
Get:3 http://archive.ubuntu.com/ubuntu bionic/main htop 2.1.0-3 (diff) [8,824 B]                                                                      
Fetched 314 kB in 35s (8,877 B/s)                                                                                                                     
dpkg-source: info: extracting htop in htop-2.1.0
dpkg-source: info: unpacking htop_2.1.0.orig.tar.gz
dpkg-source: info: unpacking htop_2.1.0-3.debian.tar.xz
dpkg-source: info: applying 601-openvz-new-ctid-vpid.patch
dpkg-source: info: applying fix-small-terminals.patch
dpkg-source: info: applying fix-ldflags.patch
dpkg-source: info: applying fix-isalnum-crash.patch
dpkg-buildpackage: info: source package htop
dpkg-buildpackage: info: source version 2.1.0-3
dpkg-buildpackage: info: source distribution unstable
dpkg-buildpackage: info: source changed by Graham Inggs <
[email protected]
>
dpkg-buildpackage: info: host architecture amd64

要查看 APT 的版本,运行以下命令:

apt-get -v

输出:

apt 1.6.1 (amd64)
Supported modules:
*Ver: Standard .deb
*Pkg:  Debian dpkg interface (Priority 30)
 Pkg:  Debian APT solver interface (Priority -1000)
 Pkg:  Debian APT planner interface (Priority -1000)
 S.L: 'deb' Debian binary tree
 S.L: 'deb-src' Debian source tree
 Idx: Debian Source Index
 Idx: Debian Package Index
 Idx: Debian Translation Index
 Idx: Debian dpkg status file
 Idx: Debian deb file
 Idx: Debian dsc file
 Idx: Debian control file
 Idx: EDSP scenario file
 Idx: EIPP scenario file

你可以运行以下命令列出 APT 提供的所有选项:

apt-get -h

输出:

apt 1.6.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)
  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
  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.