如何在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 update
更新软件包列表后,您可以使用 apt-get update 升级已安装的软件包。此命令将安装系统上当前安装的所有软件包的最新版本。
例子
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
同样,虽然 dpkg --get-selections 不是 apt-get 命令,但它是列出所有已安装软件包的有用命令。
例子
dpkg −−get−selections
输出
adduser install
apache2 install
apache2−bin install
apache2−data install
apt install
...
请记住,apt-get 命令是 Linux 武器库中的一个强大工具。始终小心使用它,您会发现它使管理您的系统变得更加容易。
结论
apt-get 命令是用于管理 Linux 系统上的软件包的强大工具。有了它,您可以轻松安装、升级和删除软件。请记住在每个命令之前使用 sudo 以确保您拥有必要的权限。删除软件包时请务必小心,以免意外删除重要的内容。 Linux 快乐!