# Ubuntu : apt-get 命令

apt-get 命令是 Ubuntu 系统中的包管理工具，可以用来安装、卸载包，也可以用来升级包，还可以用来把系统升级到新的版本。本文介绍 apt-get 命令的基本用法，演示环境为 Ubuntu 18.04。

apt-get [options] command

update
update 命令用于重新同步包索引文件，/etc/apt/sources.list 文件中的配置指定了包索引文件的来源。更新了包索引文件后就可以得到可用的包的更新信息和新的包信息。这样我们本地就有了这样的信息：有哪些软件的哪些版本可以从什么地方(源)安装。
update 命令应该总是在安装或升级包之前执行。

install
install 命令用来安装或者升级包。每个包都有一个包名，而不是一个完全限定的文件名(例如，在 Debian 系统中，提供的参数是 apt-utils，而不是 apt-utils_1.6.1_amd64.deb)。被安装的包依赖的包也将被安装。配置文件 /etc/apt/sources.list 中包含了用于获取包的源(服务器)。install 命令还可以用来更新指定的包。

upgrade 命令用于从 /etc/apt/sources.list 中列出的源安装系统上当前安装的所有包的最新版本。在任何情况下，当前安装的软件包都不会被删除，尚未安装的软件包也不会被检索和安装。如果当前安装的包的新版本不能在不更改另一个包的安装状态的情况下升级，则将保留当前版本。必须提前执行 update 命令以便 apt-get 知道已安装的包是否有新版本可用。

[En]

In addition to performing upgrade functions, dist-upgrade intelligently handles changes in dependencies with new versions of the package. Apt-get has a “smart” conflict resolution system that, if necessary, will try to upgrade the most important packages at the expense of less important packages. Therefore, the distr-upgrade command may delete some packages. Therefore, when updating packages in the system, it is recommended that you execute the following commands in order:

$apt-get update$ apt-get upgrade -y

remove
remove 与 install 类似，不同之处是删除包而不是安装包。注意，使用 remove 命令删除一个包会将其配置文件留在系统上。

purge
purge 命令与 remove 命令类似，purge 命令在删除包的同时也删除了包的配置文件。

autoremove
autoremove 命令用于删除自动安装的软件包，这些软件包当初是为了满足其他软件包对它的依赖关系而安装的，而现在已经不再需要了。

clean
clean 命令清除在本地库中检索到的包。它从 /var/cache/apt/archives/ 和 /var/cache/apt/archives/partial/ 目录删除除锁文件之外的所有内容。

autoclean

source
source 命令下载包的源代码。默认会下载最新可用版本的源代码到当前目录中。

changelog
changelog 命令尝试下载并显示包的更新日志。

[En]

Most packages need to interact with the user before installation and continue the installation after the user confirms it. There is no way to interact with users in automated tasks. The-y option can work in such scenarios as if the user had confirmed the installation:

[En]

If we think a package is not working properly, we can try to reinstall it and add the– reinstall option to the install command. In addition, if the installed package has an updated or new version, you can also use this method to upgrade the package to the latest version:

[En]

It is strange that updating the specified package is not done through the upgrade command, but using the intall command. Note: it is the install command:

[En]

You can view the version of the installed package or the package to be installed with the following command:

[En]

Here is an example of an installed package:

[En]

Here is an example of an uninstalled package:

[En]

Note that this refers to the version number of the package:

remove 命令的特点是只删除程序文件，保留相关的配置文件：

[En]

If you want to clear the package completely, you can use the purge command, which deletes both the program file and its configuration file:

autoremove 命令用于删除自动安装的软件包，这些软件包当初是为了满足其他软件包对它的依赖关系而安装的，而现在已经不再需要了。因此在删除包后执行一下 autoremove 是个不错的选择：

[En]

The process of installing the package is actually downloading the package installation file to the cache directory, and then performing the installation. Over time, there will be a large number of useless package installation files in the system, and the clean command can clear these cached package installation files:

clean 命令从 /var/cache/apt/archives/ 和 /var/cache/apt/archives/partial/ 目录删除除锁文件之外的所有内容。

autoclean 命令与 clean 命令类似。不同之处在于，它只删除不能再下载的软件包文件，而且这些文件在很大程度上是无用的。这允许长时间维护缓存，而不至于大小失控：

[En]

Now that you can add detailed output information, you can also flexibly use the-Q option to reduce the output information:

apt-get source 命令用来下载包的源代码。要让 apt-get source 命令起作用需要先更新 /etc/apt/sources.list 文件，添加 deb-src 配置，其实就是取消掉原本被注释掉的以 deb-src 开头的行。在 desktop 版的系统中，还可以通过 “Software & Updates” UI 来做同样的事情，在 “Ubuntu Software” 选项卡中选中 “Source code” 项即可：

[En]

It’s so weird that what is shown here is apt. Go on to see:

Original: https://www.cnblogs.com/sparkdev/p/11339231.html
Author: sparkdev
Title: Ubuntu : apt-get 命令

(0)