apt和apt-get的区别

文章目录

  • 环境
  • 问题
  • 背景
  • 区别
    • 进度条
    • 显示可更新包的数量
    • upgrade
  • 对比
  • apt-get 过时了吗
  • 使用apt还是apt-get
  • 总结
  • 参考

环境

  • RHEL 9.3
  • Docker Community 24.0.7
  • Ubuntu Docker image
    • jammy 22.04
    • lunar 23.04
  • Ubuntu 22.04

问题

aptapt-get 有一些相似之处。比如,如果想安装vim,下面两种方法都可以:

  • apt install vim
  • apt-get install vim

那么问题来了:

  • apt-getapt 有什么区别?
  • 如果二者差不多,为何还要引入 apt 呢?
  • 如果 aptapt-get 好,那还有必要继续使用 apt-get 吗,还是应该都使用 apt 呢?

背景

Debian系统(比如Ubuntu)使用Advanced Packaging Tool(APT)来管理系统里的包。注意这里的APT不是指 apt 命令。

在基于Debian的Linux系统里,有一系列工具可以和APT交互,用来安装、卸载、管理包, apt-get 就是其中一个,类似的还有 apt-cacheapt-config 等。

这些命令通常被称为“低级”(low-level)命令,它们包含很多用户不太常用的功能。另一方面,一些常用的包管理功能分散在各种 apt-xxx 命令里。

为了简化,引入了 apt 命令。 apt 包含了 apt-xxx 命令里最常用的功能,这样,用户就不再需要使用 apt-xxx 命令来管理包,而只需使用一个 apt 命令。

apt 的参数选项比 apt-xxx 少,毕竟是为了方便用户使用,只包含了最常用的功能。

此外, apt 的用户界面更加友好。

区别

接下来用几个示例展示 aptapt-get 的区别。

进度条

(注:本例测试环境为Ubuntu Docker image(jammy,22.04)。)

apt install vim 在安装过程中,会显示进度条:

apt和apt-get的区别_第1张图片

而使用 apt-get install vim 时,则不会显示进度条。

apt和apt-get的区别_第2张图片

可以通过配置 /etc/apt/apt.conf.d/99fancy 文件,使得 apt-get 显示进度条:

DPkg::Progress-Fancy "1";
APT::Color "1";

亲测有效。

注:可以通过配置 /etc/apt/apt.conf.d/99progressbar 文件,决定 apt 是否显示进度条:

Dpkg::Progress-Fancy"1";

但在我的测试里,无论是否有该配置, apt 都会显示进度条。我猜测这可能只对旧的Ubuntu版本有效,当时 apt 可能还没有默认显示进度条。

显示可更新包的数量

(注:使用Ubuntu Docker image(jammy,22.04)做 apt update 时,最后显示没有可更新的包。本例使用的是 ubuntu:lunar Docker image(lunar,23.04)。)

apt update 命令会在最后面,显示可更新包的数量:

apt update
Get:1 http://security.ubuntu.com/ubuntu lunar-security InRelease [109 kB]
Get:2 http://archive.ubuntu.com/ubuntu lunar InRelease [267 kB]
Get:3 http://security.ubuntu.com/ubuntu lunar-security/restricted amd64 Packages [612 kB]
Get:4 http://archive.ubuntu.com/ubuntu lunar-updates InRelease [109 kB]
Get:5 http://archive.ubuntu.com/ubuntu lunar-backports InRelease [99.9 kB]
Get:6 http://security.ubuntu.com/ubuntu lunar-security/main amd64 Packages [610 kB]
Get:7 http://security.ubuntu.com/ubuntu lunar-security/universe amd64 Packages [975 kB]
Get:8 http://archive.ubuntu.com/ubuntu lunar/restricted amd64 Packages [181 kB]
Get:9 http://security.ubuntu.com/ubuntu lunar-security/multiverse amd64 Packages [7793 B]
Get:10 http://archive.ubuntu.com/ubuntu lunar/multiverse amd64 Packages [289 kB]         
Get:11 http://archive.ubuntu.com/ubuntu lunar/universe amd64 Packages [18.7 MB]
Get:12 http://archive.ubuntu.com/ubuntu lunar/main amd64 Packages [1797 kB]                                                                                                                                                                 
Get:13 http://archive.ubuntu.com/ubuntu lunar-updates/universe amd64 Packages [1079 kB]                                                                                                                                                     
Get:14 http://archive.ubuntu.com/ubuntu lunar-updates/main amd64 Packages [712 kB]                                                                                                                                                          
Get:15 http://archive.ubuntu.com/ubuntu lunar-updates/multiverse amd64 Packages [7793 B]                                                                                                                                                    
Get:16 http://archive.ubuntu.com/ubuntu lunar-updates/restricted amd64 Packages [613 kB]                                                                                                                                                    
Get:17 http://archive.ubuntu.com/ubuntu lunar-backports/universe amd64 Packages [4201 B]                                                                                                                                                    
Fetched 26.1 MB in 12s (2207 kB/s)                                                                                                                                                                                                          
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
3 packages can be upgraded. Run 'apt list --upgradable' to see them.

apt-get update 命令则不显示可更新包的数量:

apt-get update
Get:1 http://archive.ubuntu.com/ubuntu lunar InRelease [267 kB]
Get:2 http://security.ubuntu.com/ubuntu lunar-security InRelease [109 kB]
Get:3 http://archive.ubuntu.com/ubuntu lunar-updates InRelease [109 kB]
Get:4 http://archive.ubuntu.com/ubuntu lunar-backports InRelease [99.9 kB]            
Get:5 http://security.ubuntu.com/ubuntu lunar-security/multiverse amd64 Packages [7793 B]
Get:6 http://archive.ubuntu.com/ubuntu lunar/main amd64 Packages [1797 kB]
Get:7 http://security.ubuntu.com/ubuntu lunar-security/main amd64 Packages [610 kB]
Get:8 http://archive.ubuntu.com/ubuntu lunar/universe amd64 Packages [18.7 MB]
Get:9 http://security.ubuntu.com/ubuntu lunar-security/universe amd64 Packages [975 kB]  
Get:10 http://security.ubuntu.com/ubuntu lunar-security/restricted amd64 Packages [612 kB]
Get:11 http://archive.ubuntu.com/ubuntu lunar/multiverse amd64 Packages [289 kB]                                                                                                                                                            
Get:12 http://archive.ubuntu.com/ubuntu lunar/restricted amd64 Packages [181 kB]                                                                                                                                                            
Get:13 http://archive.ubuntu.com/ubuntu lunar-updates/restricted amd64 Packages [613 kB]                                                                                                                                                    
Get:14 http://archive.ubuntu.com/ubuntu lunar-updates/universe amd64 Packages [1079 kB]                                                                                                                                                     
Get:15 http://archive.ubuntu.com/ubuntu lunar-updates/multiverse amd64 Packages [7793 B]                                                                                                                                                    
Get:16 http://archive.ubuntu.com/ubuntu lunar-updates/main amd64 Packages [712 kB]                                                                                                                                                          
Get:17 http://archive.ubuntu.com/ubuntu lunar-backports/universe amd64 Packages [4201 B]                                                                                                                                                    
Fetched 26.1 MB in 9s (3041 kB/s)                                                                                                                                                                                                           
Reading package lists... Done

upgrade

(注:本例测试环境为Ubuntu 22.04(非Docker image)。)

查看 aptapt-get 的帮助:

  • man apt

upgrade is used to install available upgrades of all packages currently installed on the system from the sources configured via sources.list(5). New packages will be installed if required to satisfy dependencies, but existing packages will never be removed. If an upgrade for a package requires the removal of an installed package the upgrade for this package isn’t performed.

  • man apt-get

upgrade is used to install the newest versions of all packages currently installed on the system from the sources enumerated in /etc/apt/sources.list. Packages currently installed with new versions available are retrieved and upgraded; under no circumstances are currently installed packages removed, or packages not already installed retrieved and installed. New versions of currently installed packages that cannot be upgraded without changing the install status of another package will be left at their current version. An update must be performed first so that apt-get knows that new versions of packages are available.

简而言之:

  • 共同点:二者都不会删除已安装的包
  • 不同点:如果升级时发现有被依赖的新包,则 apt upgrade 会安装新包,而 apt-get upgrade 则不会安装新包

例如:

apt --simulate upgrade
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Calculating upgrade... Done
The following NEW packages will be installed:
  linux-headers-5.15.0-91 linux-headers-5.15.0-91-generic linux-image-5.15.0-91-generic linux-modules-5.15.0-91-generic linux-modules-extra-5.15.0-91-generic
The following packages have been kept back:
  python3-update-manager update-manager-core
The following packages will be upgraded:
  binutils binutils-common binutils-x86-64-linux-gnu cryptsetup cryptsetup-bin cryptsetup-initramfs curl distro-info distro-info-data irqbalance kpartx libbinutils libc-bin libc6 libcryptsetup12 libctf-nobfd0 libctf0 libcurl3-gnutls libcurl4
  libsqlite3-0 libssh-4 linux-firmware linux-generic linux-headers-generic linux-image-generic locales multipath-tools openssh-client openssh-server openssh-sftp-server python3-cryptography python3-distro-info python3-software-properties
  software-properties-common systemd-hwe-hwdb tar vim vim-common vim-runtime vim-tiny xxd
41 upgraded, 5 newly installed, 0 to remove and 2 not upgraded.
apt-get --simulate upgrade
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
Calculating upgrade... Done
The following packages have been kept back:
  linux-generic linux-headers-generic linux-image-generic python3-update-manager update-manager-core
The following packages will be upgraded:
  binutils binutils-common binutils-x86-64-linux-gnu cryptsetup cryptsetup-bin cryptsetup-initramfs curl distro-info distro-info-data irqbalance kpartx libbinutils libc-bin libc6 libcryptsetup12 libctf-nobfd0 libctf0 libcurl3-gnutls libcurl4
  libsqlite3-0 libssh-4 linux-firmware locales multipath-tools openssh-client openssh-server openssh-sftp-server python3-cryptography python3-distro-info python3-software-properties software-properties-common systemd-hwe-hwdb tar vim
  vim-common vim-runtime vim-tiny xxd
38 upgraded, 0 newly installed, 0 to remove and 5 not upgraded.

注: --simulate 表示模拟操作,并没有真正升级。

对比可见, apt upgrade 安装了5个新包。这是因为,升级Linux kernel版本,实际上是安装了新的Linux kernel,其包名是不同的,所以对于系统来说,相当于安装了新的包。 apt-get upgrade 不会安装新包。如果想用 apt-get 更新kernel,需要用 dist-upgrade 选项。

对比

虽然 aptapt-get 有一些相似的命令选项,但 apt 并没有设计为向后兼容 apt-get 。这就意味着如果把 apt-get xxx 里的 apt-get 替换为 apt ,则无法保证修改后的命令是正确的。

aptapt-xxx 的简单对比如下:

apt 命令 替代的 apt-xxx 命令 功能
apt install apt-get install 安装包
apt remove apt-get remove 卸载包
apt update apt-get update 更新仓库索引
apt upgrade apt-get upgrade 更新所有可更新的包
apt autoremove apt-get autoremove 卸载不想要的包
apt full-upgrade apt-get dist-upgrade 更新包并自动处理依赖
apt search apt-cache search 查找包
apt show apt-cache show 显式包详细信息

apt 也有一些新的选项:

新的 apt 命令 功能
apt list 包列表以及是否已安装,是否可升级等信息
apt edit-sources 编辑 sources.list 文件

apt-get 过时了吗

没有迹象表明 apt-get 过时了,它仍然比 apt 提供了更多的功能。

对于底层的操作,比如脚本、开发处理APT的CLI工具等, apt-get 仍然会被用到。

使用apt还是apt-get

优先使用 apt ,对于 apt 不包含的功能,再使用 apt-xxx

总结

  • aptapt-xxx 的子集
  • apt 简化了使用,并且对用户更加友好
  • 优先使用 apt

参考

  • https://itsfoss.com/apt-vs-apt-get-difference
  • https://www.baeldung.com/linux/apt-vs-apt-get

你可能感兴趣的:(Linux,ubuntu,apt,apt-get)