介绍
在本实验中,你将学习如何有效使用 apt
命令,这是一个用于 Ubuntu 和基于 Debian 的 Linux 发行版的强大包管理工具。实验涵盖了 apt
命令的结构和基本用法,以及安装、更新、搜索和删除软件包的实际示例。通过本实验,你将掌握如何使用 apt
命令在 Linux 系统上管理软件包。
在本实验中,你将学习如何有效使用 apt
命令,这是一个用于 Ubuntu 和基于 Debian 的 Linux 发行版的强大包管理工具。实验涵盖了 apt
命令的结构和基本用法,以及安装、更新、搜索和删除软件包的实际示例。通过本实验,你将掌握如何使用 apt
命令在 Linux 系统上管理软件包。
apt
命令的结构在这一步中,我们将探讨 Linux 中 apt
命令的结构和基本用法。apt
命令是一个强大的包管理工具,用于在 Ubuntu 和基于 Debian 的 Linux 发行版上安装、更新、搜索和删除软件包。
让我们从理解 apt
命令的一般结构开始:
sudo apt [command] [options] [package]
以下是命令中每个部分的含义:
sudo
:以超级用户(root)权限运行命令,这是大多数包管理操作所必需的。apt
:主要的包管理命令。[command]
:要执行的具体操作,例如 install
、update
、search
或 remove
。[options]
:用于自定义命令行为的附加标志或参数。[package]
:你想要安装、更新、搜索或删除的软件包名称。示例:要安装 htop
软件包,你可以运行:
sudo apt install htop
示例输出:
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following NEW packages will be installed:
htop
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 0 B/83.0 kB of archives.
After this operation, 282 kB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Selecting previously unselected package htop.
(Reading database ... 115729 files and directories currently installed.)
Preparing to unpack .../htop_3.0.5-7_amd64.deb ...
Unpacking htop (3.0.5-7) ...
Setting up htop (3.0.5-7) ...
在接下来的步骤中,我们将探索 apt
命令的更高级用法,例如更新软件包、搜索软件包和删除软件包。
apt
安装和更新软件包在这一步中,我们将学习如何使用 apt
命令安装新软件包以及更新现有软件包。
首先,让我们更新软件包索引,以确保我们拥有关于可用软件包的最新信息:
sudo apt update
示例输出:
Hit:1 http://archive.ubuntu.com/ubuntu jammy InRelease
Get:2 http://security.ubuntu.com/ubuntu jammy-security InRelease [110 kB]
Get:3 http://archive.ubuntu.com/ubuntu jammy-updates InRelease [114 kB]
Get:4 http://archive.ubuntu.com/ubuntu jammy-backports InRelease [99.8 kB]
Fetched 324 kB in 1s (324 kB/s)
Reading package lists... Done
接下来,让我们安装一个新软件包,例如 tree
命令,它以树状格式显示目录内容:
sudo apt install tree
示例输出:
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following NEW packages will be installed:
tree
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 55.9 kB of archives.
After this operation, 184 kB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 http://archive.ubuntu.com/ubuntu jammy/main amd64 tree amd64 1.8.0-1build1 [55.9 kB]
Fetched 55.9 kB in 0s (0 B/s)
Selecting previously unselected package tree.
(Reading database ... 116022 files and directories currently installed.)
Preparing to unpack .../tree_1.8.0-1build1_amd64.deb ...
Unpacking tree (1.8.0-1build1) ...
Setting up tree (1.8.0-1build1) ...
要更新软件包,你可以使用 apt upgrade
命令。这将把所有已安装的软件包升级到最新版本:
sudo apt upgrade
示例输出:
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Calculating upgrade... Done
The following packages will be upgraded:
libc-bin libc6 libssl3 openssl
4 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
Need to get 3,007 kB of archives.
After this operation, 0 B of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 libc-bin amd64 2.35-0ubuntu3.1 [1,114 kB]
Get:2 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 libc6 amd64 2.35-0ubuntu3.1 [1,693 kB]
Get:3 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 libssl3 amd64 3.0.2-0ubuntu1.8 [124 kB]
Get:4 http://archive.ubuntu.com/ubuntu jammy-updates/main amd64 openssl amd64 3.0.2-0ubuntu1.8 [76.0 kB]
Fetched 3,007 kB in 1s (2,280 kB/s)
(Reading database ... 116027 files and directories currently installed.)
Preparing to unpack .../libc-bin_2.35-0ubuntu3.1_amd64.deb ...
Unpacking libc-bin (2.35-0ubuntu3.1) over (2.35-0ubuntu3) ...
Preparing to unpack .../libc6_2.35-0ubuntu3.1_amd64.deb ...
Unpacking libc6 (2.35-0ubuntu3.1) over (2.35-0ubuntu3) ...
Preparing to unpack .../libssl3_3.0.2-0ubuntu1.8_amd64.deb ...
Unpacking libssl3 (3.0.2-0ubuntu1.8) over (3.0.2-0ubuntu1.7) ...
Preparing to unpack .../openssl_3.0.2-0ubuntu1.8_amd64.deb ...
Unpacking openssl (3.0.2-0ubuntu1.8) over (3.0.2-0ubuntu1.7) ...
Setting up libc-bin (2.35-0ubuntu3.1) ...
Setting up libc6 (2.35-0ubuntu3.1) ...
Setting up libssl3 (3.0.2-0ubuntu1.8) ...
Setting up openssl (3.0.2-0ubuntu1.8) ...
在下一步中,我们将学习如何使用 apt
命令搜索软件包以及删除已安装的软件包。
apt
搜索和删除软件包在这一步中,我们将学习如何使用 apt
命令搜索软件包以及删除已安装的软件包。
要搜索软件包,可以使用 apt search
命令。例如,让我们搜索 "vim" 软件包:
sudo apt search vim
示例输出:
Sorting... Done
Full Text Search... Done
vim/jammy 2:8.2.3995-1ubuntu2 amd64
Vi IMproved - enhanced vi editor
vim-common/jammy 2:8.2.3995-1ubuntu2 amd64
Vi IMproved - Common files
vim-gtk3/jammy 2:8.2.3995-1ubuntu2 amd64
Vi IMproved - GTK3 GUI version
vim-nox/jammy 2:8.2.3995-1ubuntu2 amd64
Vi IMproved - enhanced vi editor (without GUI)
vim-runtime/jammy 2:8.2.3995-1ubuntu2 all
Vi IMproved - Runtime files
vim-tiny/jammy 2:8.2.3995-1ubuntu2 amd64
Vi IMproved - enhanced vi editor - compact version
搜索结果显示了多个可用的 vim
软件包,包括完整的 vim
软件包,以及更专门的变体,如 vim-gtk3
和 vim-tiny
。
要删除已安装的软件包,可以使用 apt remove
命令。例如,让我们删除在上一步中安装的 tree
软件包:
sudo apt remove tree
示例输出:
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following packages will be REMOVED:
tree
0 upgraded, 0 newly installed, 1 to remove and 0 not upgraded.
After this operation, 184 kB of disk space will be freed.
Do you want to continue? [Y/n] Y
(Reading database ... 116032 files and directories currently installed.)
Removing tree (1.8.0-1build1) ...
如果你想删除软件包及其相关的配置文件,可以使用 apt purge
命令代替 apt remove
:
sudo apt purge tree
这将删除软件包及其配置文件,系统中不会留下该软件包的痕迹。
在接下来的步骤中,我们将探索使用 apt
命令进行更高级的包管理任务。
在本实验中,你首先学习了 apt
命令的结构和基本用法,包括命令的各个部分以及如何使用它来安装软件包。接着,你探索了如何使用 apt
更新软件包索引、安装新软件包以及更新现有软件包。最后,你学习了如何使用 apt
搜索和删除软件包。在整个实验过程中,我们提供了实际示例,以帮助你更好地理解 apt
命令的功能。