如何在 Ubuntu 18.04 LTS 上使用 APT 管理包
在此页
- 要求
- 安装和更新包
- 使用 Apt 删除包
- 使用 Apt-Cache 搜索包
- APT 高级用法
APT 也称为高级打包工具,是用于管理基于 Debian 的发行版(如 Ubuntu 16.04、Ubuntu 18.04、Debian 8、Debian 9 等)中的包的命令行工具。 APT 简化了安装、删除、升级软件包的过程,甚至用于通过命令行界面升级整个操作系统
在本教程中,我们将解释如何在 Ubuntu 18.04 LTS 服务器上使用 APT 命令行工具管理包。
要求
- 一台运行 Ubuntu 18.04 LTS 的服务器。
- 服务器上的 root 或 sudo 访问权限。
以下所有命令均以 root 用户身份运行。在 shell 上以 root 用户身份登录或运行:
sudo -s
成为根用户。作为替代方案,您可以在所有命令前加上 sudo。
安装和更新包
Apt-get 通过从不同来源获取信息并将其存储在本地数据库中来工作。更新命令从它们的位置获取包并将包更新到更新版本。
apt-get update -y
运行上述命令后,您的数据库应该是最新的。
upgrade 命令用于将当前安装的所有软件包升级到更新的版本。
apt-get upgrade -y
您也可以使用 dist-upgrade 命令升级包,但它使用智能冲突解决方法更改包依赖关系。
apt-get dist-upgrade -y
更新数据库后,您可以通过运行以下命令来安装任何包:
apt-get install package1 package2
例如,您可以通过运行以下命令来安装 Nginx Web 服务器包:
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 <>
Original-Maintainer: Jordi Mallach <>
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:
使用以下命令检查软件包是否已安装以及它属于哪个存储库:
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 <>
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.