Linux apt-get 命令实用示例

LinuxLinuxBeginner
立即练习

💡 本教程由 AI 辅助翻译自英文原版。如需查看原文,您可以 切换至英文原版

介绍

在本实验中,你将学习如何使用 apt-get 命令,这是 Linux 中一个强大的包管理工具。你将了解 apt-get 命令的基础知识,包括如何在系统中安装、更新和删除软件包。实验涵盖了使用 apt-get 命令高效管理软件包的实用示例。apt-get 命令提供了一种简单直接的方式来管理软件包并保持系统的最新状态。

Linux 命令速查表


Skills Graph

%%%%{init: {'theme':'neutral'}}%%%% flowchart RL linux(("Linux")) -.-> linux/UserandGroupManagementGroup(["User and Group Management"]) linux(("Linux")) -.-> linux/PackagesandSoftwaresGroup(["Packages and Softwares"]) linux/UserandGroupManagementGroup -.-> linux/sudo("Privilege Granting") linux/PackagesandSoftwaresGroup -.-> linux/apt("Package Handling") subgraph Lab Skills linux/sudo -.-> lab-422550{{"Linux apt-get 命令实用示例"}} linux/apt -.-> lab-422550{{"Linux apt-get 命令实用示例"}} end

了解 apt-get 命令的基础知识

在这一步中,你将学习 apt-get 命令的基础知识,这是 Linux 中一个强大的包管理工具。apt-get 命令允许你在系统中安装、更新、删除和管理软件包。

首先,让我们了解 apt-get 命令的一般语法:

sudo apt-get [operation] [package_name]

可用的操作包括:

  • install:安装一个或多个软件包
  • update:更新包索引
  • upgrade:将已安装的软件包升级到最新版本
  • remove:删除一个或多个软件包
  • purge:删除软件包及其配置文件
  • clean:删除已下载的包文件
  • autoremove:删除不再需要的自动安装的软件包

现在,让我们尝试一些基本的 apt-get 命令:

## 更新包索引
sudo apt-get update
示例输出:
Hit:1 http://archive.ubuntu.com/ubuntu jammy InRelease
Get:2 http://security.ubuntu.com/ubuntu jammy-security InRelease [110 kB]
...

## 安装一个软件包
sudo apt-get 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/86.0 kB of archives.
After this operation, 296 kB of additional disk space will be used.
Do you want to continue? [Y/n] Y
...

## 删除一个软件包
sudo apt-get remove htop
示例输出:
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following packages will be REMOVED:
  htop
0 upgraded, 0 newly installed, 1 to remove and 0 not upgraded.
After this operation, 296 kB disk space will be freed.
Do you want to continue? [Y/n] Y

在上面的示例中,我们演示了如何更新包索引、安装一个软件包(htop)以及删除一个软件包(htop)。apt-get 命令提供了一种简单直接的方式来管理你的 Ubuntu 系统中的软件包。

使用 apt-get 安装和更新软件包

在这一步中,你将学习如何使用 apt-get 命令安装和更新软件包。

首先,让我们安装一个新软件包 tree,这是一个命令行工具,用于以树状格式显示目录结构。

sudo apt-get 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 0 B/56.0 kB of archives.
After this operation, 152 kB of additional disk space will be used.
Do you want to continue? [Y/n] Y

接下来,让我们将系统中所有已安装的软件包更新到最新版本。

sudo apt-get upgrade
示例输出:
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Calculating upgrade... Done
The following packages will be upgraded:
  base-files base-passwd bash bsdutils coreutils dash dbus dbus-user-session dbus-x11 ...
57 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.
Need to get 29.1 MB of archives.
After this operation, 3,772 kB of additional disk space will be used.
Do you want to continue? [Y/n] Y

在上面的示例中,我们首先安装了 tree 软件包,然后将系统中所有已安装的软件包升级到了最新版本。

使用 apt-get 删除软件包并清理系统

在这一步中,你将学习如何使用 apt-get 命令删除软件包并清理系统。

首先,让我们删除在上一步中安装的 tree 软件包:

sudo apt-get 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, 152 kB of disk space will be freed.
Do you want to continue? [Y/n] Y

在上面的示例中,我们使用 remove 操作卸载了 tree 软件包。

接下来,让我们清理已下载的包文件以释放磁盘空间:

sudo apt-get clean
示例输出:
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done

clean 操作会从本地仓库中删除已下载的包文件,从而释放系统上的磁盘空间。

最后,让我们使用 autoremove 操作删除不再需要的软件包:

sudo apt-get autoremove
示例输出:
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
The following packages will be REMOVED:
  linux-headers-5.15.0-46 linux-headers-5.15.0-46-generic linux-image-5.15.0-46-generic
  linux-modules-5.15.0-46-generic linux-modules-extra-5.15.0-46-generic
0 upgraded, 0 newly installed, 5 to remove and 0 not upgraded.
After this operation, 321 MB of disk space will be freed.
Do you want to continue? [Y/n] Y

autoremove 操作会识别并删除那些作为依赖项自动安装且不再需要的软件包。

总结

在本实验中,你学习了 apt-get 命令的基础知识,这是 Linux 中一个强大的包管理工具。你探索了 apt-get 命令的一般语法及其可用操作,例如安装、更新、删除和清理软件包。你还练习了运行常见的 apt-get 命令,包括更新包索引、安装软件包(htop)以及删除软件包(htop)。apt-get 命令提供了一种简单直接的方式来管理你的 Linux 系统中的软件包。

Linux 命令速查表