apt命令详解

  • apt(Advanced Packaging Tool)是一个在 Debian 和 Ubuntu 中的 Shell 前端软件包管理器。
  • apt 命令提供了查找、安装、升级、删除某一个、一组甚至全部软件包的命令,而且命令简洁而又好记。
  • apt 命令执行需要超级管理员权限(root)。

apt语法

apt [options] [command] [package ...]
options:可选,选项包括 -h(帮助),-y(当安装过程提示选择全部为"yes"),-q(不显示安装的过程)等等。
command:要进行的操作。
package:安装的包名

apt常用命令

列出所有可更新的软件清单命令:sudo apt update
升级软件包:sudo apt upgrade
列出可更新的软件包及版本信息:apt list --upgradeable
升级软件包,升级前先删除需要更新软件包:sudo apt full-upgrade
安装指定的软件命令:sudo apt install <package_name>
安装多个软件包:sudo apt install <package_1> <package_2> <package_3>
更新指定的软件命令:sudo apt update <package_name>
删除软件包命令:sudo apt remove <package_name>
清理不再使用的依赖和库文件: sudo apt autoremove
移除软件包及配置文件: sudo apt purge <package_name>
查找软件包命令:sudo apt search <keyword>
列出所有已安装的包:apt list --installed
列出所有已安装的包的版本信息:apt list --all-versions

apt实例

查看一些可更新的包:

# sudo apt update

hulk@DESKTOP-JC1UBQV:~$ sudo apt update
Hit:1 http://mirrors.aliyun.com/ubuntu focal InRelease
Hit:2 http://mirrors.aliyun.com/ubuntu focal-security InRelease
Hit:3 http://mirrors.aliyun.com/ubuntu focal-updates InRelease
Hit:4 http://mirrors.aliyun.com/ubuntu focal-proposed InRelease
Hit:5 http://mirrors.aliyun.com/ubuntu focal-backports InRelease
Reading package lists... Done
Building dependency tree
Reading state information... Done
33 packages can be upgraded. Run 'apt list --upgradable' to see them.

升级安装包:

# sudo apt upgrade

hulk@DESKTOP-JC1UBQV:~$ sudo apt upgrade
Reading package lists... Done
Building dependency tree
Reading state information... Done
Calculating upgrade... Done
The following package was automatically installed and is no longer required:
  libfwupdplugin1
Use 'sudo apt autoremove' to remove it.
The following NEW packages will be installed:
  libfwupdplugin5 libmbim-glib4 libmbim-proxy libmm-glib0 libqmi-glib5 libqmi-proxy modemmanager usb-modeswitch usb-modeswitch-data
The following packages will be upgraded:
  base-files curl dpkg fwupd fwupd-signed kmod libarchive13 libc-bin libc6 libcurl3-gnutls libcurl4 libexpat1 libfwupd2 libjcat1 libkmod2 libpolkit-agent-1-0 libpolkit-gobject-1-0 libsasl2-2 libsasl2-modules libsasl2-modules-db libssl1.1
  libx11-6 libx11-data libx11-xcb1 locales motd-news-config open-vm-tools openssl policykit-1 rsync secureboot-db snapd sosreport
33 upgraded, 9 newly installed, 0 to remove and 0 not upgraded.
12 standard security updates
Need to get 52.6 MB of archives.
After this operation, 12.6 MB of additional disk space will be used.
Do you want to continue? [Y/n] y
# 在以上交互式输入字母 Y 即可开始升级。
# 可以将以下两个命令组合起来,一键升级:

# sudo apt update && sudo apt upgrade -y

安装某个package包:

# sudo apt install curl

hulk@DESKTOP-JC1UBQV:~$ sudo apt install curl
Reading package lists... Done
Building dependency tree
Reading state information... Done
curl is already the newest version (7.68.0-1ubuntu2.8).
curl set to manually installed.
The following package was automatically installed and is no longer required:
  libfwupdplugin1
Use 'sudo apt autoremove' to remove it.
0 upgraded, 0 newly installed, 0 to remove and 0 not upgraded.

补全package名

如过不太记得完整的包名,我们可以只输入前半部分的包名,然后按下 Tab 键,会列出相关的包名:

hulk@DESKTOP-JC1UBQV:~$ sudo apt install ansi
ansible                ansible-doc            ansible-lint           ansible-tower-cli      ansible-tower-cli-doc  ansilove               ansiweather

安装,如果已有则不升级

如果我们想安装一个软件包,但如果软件包已经存在,则不要升级它,可以使用 –no-upgrade 选项:

sudo apt install <package_name> --no-upgrade

升级package包

如果只想升级,不要安装可以使用 --only-upgrade 参数

sudo apt install mplayer --no-upgrade

安装指定版本

如果需要设置指定版本,语法格式如下:

sudo apt install <package_name>=<version_number>
package_name 为包名,version_number 为版本号。

移除package包

移除包可以使用 remove 命令:

sudo apt remove package

查找指定package包

查找名为 libimobile 的相关包:

apt search libimobile

查看指定package包信息

查看 pinta 包的相关信息:

apt show pinta

列出可更新的软件包:

apt list --upgradeable

清理不再使用的依赖和库文件:

sudo apt autoremove
在以上交互式输入字母 Y 即可开始清理。
Last modification:March 2nd, 2022 at 10:36 am