如何在Linux中使用Apt-Get命令?
高级软件包工具 (APT) 是一款功能强大的命令行工具,用于基于 Debian 的系统(如 Ubuntu、Linux Mint 等)。apt-get 命令是与 APT 交互最常用的方法之一。它用于处理软件包,允许您安装、升级和删除 Linux 系统上的软件。
在本指南中,我们将引导您了解 apt-get 命令的基础知识,并附带示例及其输出。
1. 更新软件包列表:apt-get update
您应该了解的第一个命令是 apt-get update。此命令检索有关最新版本的软件包及其依赖项的信息。它不会安装或升级任何软件包,但会更新软件包列表以进行升级和新软件包安装。
示例
sudo apt−get update
输出
Hit:1 http://archive.ubuntu.com/ubuntu bionic InRelease Get:2 http://archive.ubuntu.com/ubuntu bionic−updates InRelease [88.7 kB] ... Reading package lists... Done
2. 升级软件包:apt-get upgrade
更新软件包列表后,您可以使用 apt-get upgrade 升级已安装的软件包。此命令将安装系统上当前安装的所有软件包的最新版本。
示例
sudo apt−get upgrade
输出
Reading package lists... Done Building dependency tree Reading state information... Done Calculating upgrade... Done The following packages will be upgraded: libssl1.1 openssl 2 upgraded, 0 newly installed, 0 to remove and 0 not upgraded. Need to get 1,374 kB of archives. After this operation, 1,024 B of additional disk space will be used. Do you want to continue? [Y/n]
3. 安装软件包:apt-get install
要安装新软件包,请使用 apt-get install 命令,后跟软件包名称。
示例
sudo apt−get install firefox
输出
Reading package lists... Done Building dependency tree Reading state information... Done The following additional packages will be installed: firefox−locale−en Suggested packages: fonts−lyx The following NEW packages will be installed: firefox firefox−locale−en 0 upgraded, 2 newly installed, 0 to remove and 0 not upgraded. Need to get 49.4 MB of archives. After this operation, 182 MB of additional disk space will be used. Do you want to continue? [Y/n]
4. 删除软件包:apt-get remove
如果要删除软件包但保留其配置文件,请使用 apt-get remove 命令。
示例
sudo apt−get remove firefox
输出
Reading package lists... Done Building dependency tree Reading state information... Done The following packages will be removed: firefox firefox−locale−en 0 upgraded, 0 newly installed, 2 to remove and 0 not upgraded. After this operation, 182 MB disk space will be freed. Do you want to continue? [Y/n]
5. 清除软件包:apt-get purge
如果要删除软件包及其配置文件,请使用 apt-get purge 命令。
示例
sudo apt−get purge firefox
输出
Reading package lists... Done Building dependency tree Reading state information... Done The following packages will be removed: firefox* firefox−locale−en* 0 upgraded, 0 newly installed, 2 to remove and 0 not upgraded. After this operation, 182 MB disk space will be freed. Do you want to continue? [Y/n]
6. 自动移除:apt-get autoremove
安装软件包时,它可能依赖于其他软件包。删除软件包后,其依赖项可能不再需要。要删除这些不必要的软件包,可以使用 apt-get autoremove 命令。
示例
sudo apt−get autoremove
输出
Reading package lists... Done Building dependency tree Reading state information... Done The following packages will be REMOVED: libfreetype6 libjpeg−turbo8 libjpeg8 libjbig0 libtiff5 0 upgraded, 0 newly installed, 5 to remove and 0 not upgraded. After this operation, 1,525 kB disk space will be freed. Do you want to continue? [Y/n]
7. 清理软件包缓存:apt-get clean
apt-get clean 命令用于通过清理本地存储库中检索到的 .deb 文件来释放空间。
示例
sudo apt−get clean
输出
// No output, but .deb files are removed from /var/cache/apt/archives/
8. 安装软件包的特定版本:apt-get install package=version
有时,您可能需要安装软件包的特定版本。可以通过在 apt-get install 命令中的软件包名称后附加 =version 来实现。
示例
sudo apt−get install apache2=2.4.29−1ubuntu4.14
输出
Reading package lists... Done Building dependency tree Reading state information... Done The following additional packages will be installed: apache2−bin apache2−data apache2−utils libapr1 libaprutil1 libaprutil1−dbd−sqlite3 libaprutil1−ldap liblua5.2−0 Suggested packages: www−browser apache2−doc apache2−suexec−pristine | apache2−suexec−custom The following NEW packages will be installed: apache2 apache2−bin apache2−data apache2−utils libapr1 libaprutil1 libaprutil1−dbd−sqlite3 libaprutil1−ldap liblua5.2−0 0 upgraded, 9 newly installed, 0 to remove and 0 not upgraded. Need to get 1,358 kB of archives. After this operation, 5,353 kB of additional disk space will be used. Do you want to continue? [Y/n]
9. 下载软件包而不安装:apt-get download
如果要下载 .deb 软件包而不安装它,可以使用 apt-get download 命令。
示例
apt−get download apache2
输出
Get:1 http://archive.ubuntu.com/ubuntu bionic−updates/main amd64 apache2 amd64 2.4.29−1ubuntu4.14 [95.1 kB] Fetched 95.1 kB in 1s (67.8 kB/s)
10. 检查是否安装了软件包:dpkg -l
虽然不是 apt-get 命令,但 dpkg -l 通常与 apt-get 结合使用以检查是否安装了软件包。
示例
dpkg −l | grep apache2
输出
ii apache2 2.4.29−1ubuntu4.14 amd64 Apache HTTP Server ii apache2−bin 2.4.29−1ubuntu4.14 amd64 Apache HTTP Server (modules and other binary files) ii apache2−data 2.4.29−1ubuntu4.14 all Apache HTTP Server (common files)
11. 列出所有已安装的软件包:dpkg --get-selections
同样,虽然不是 apt-get 命令,但 dpkg --get-selections 是列出所有已安装软件包的有用命令。
示例
dpkg −−get−selections
输出
adduser install apache2 install apache2−bin install apache2−data install apt install ...
请记住,apt-get 命令是您 Linux 工具库中的强大工具。始终谨慎使用它,您会发现它使系统管理变得容易得多。
结论
apt-get 命令是用于管理 Linux 系统上软件包的强大工具。使用它,您可以轻松安装、升级和删除软件。请记住,在每个命令之前使用 sudo 以确保您拥有必要的权限。删除软件包时务必小心,以免意外删除重要内容。祝您 Linux 使用愉快!