在 Ubuntu 中使用 apt 命令列出可升级的软件包
apt
![](/data/attachment/album/202207/24/230954qjko0c0sn55ohjf0.jpg)


[apt 命令](https://itsfoss.com/apt-command-guide/) 用于 Debian 和 Ubuntu 中的包管理。虽然你可能已经熟悉安装和删除选项，但 `apt` 还提供了一些额外的功能。


其中之一是能够查看系统上所有可升级的软件包。要显示它们，你所要做的就是在终端中使用以下命令：



```
apt list --upgradable

```

如你所见，你甚至不需要使用 `sudo` 来列出可更新的包。它只是列出了可以更新的包，而不会更新它们。


实际上，当你运行 `sudo apt update` 命令更新本地包仓库缓存时，`apt` 命令会添加此提示。



```
Fetched 1,243 kB in 17s (71.4 kB/s)           
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
30 packages can be upgraded. Run 'apt list --upgradable' to see them.

```

我不记得在旧的 `apt-get` 命令中有任何类似的直接选项来列出所有可升级的包。这是 `apt` 在旧的 `apt-get` 命令之上添加的几个新功能之一。


让我们更详细地讨论一下。


### 列出所有可升级的包


你在这里应该知道的是**你只能列出通过 APT 包管理器可用的更新**。因此，如果你已将 PPA 或 [外部仓库](https://itsfoss.com/adding-external-repositories-ubuntu/) 添加到系统的 `sources.list`，你也将看到来自它们的更新。


但是你不会在这里获得 AppImage、Flatpak、Snap 或一些其他打包格式的更新。


换句话说，它只适用于 APT 包。


因此，要列出 Ubuntu 或 Debian 系统上的所有可升级包，你应该首先更新本地包缓存：



```
sudo apt update

```

然后你的系统将知道可用的软件包更新。`apt` 命令告诉你在 update 命令结束时可以升级多少个软件包：


![The apt command shows the number of upgradable packages at the bottom of the apt update command output](/data/attachment/album/202207/24/231221a8i85r8zvn4z8xbe.png)


要查看可以升级的软件包，请运行以下命令：



```
apt list --upgradable

```

你应该看到这样的输出：



```
~$ apt list --upgradable 
Listing... Done
apparmor/jammy-updates 3.0.4-2ubuntu2.1 amd64 [upgradable from: 3.0.4-2ubuntu2]
brave-browser/stable 1.40.113 amd64 [upgradable from: 1.40.107]
evolution-data-server-common/jammy-updates,jammy-updates 3.44.2-0ubuntu1 all [upgradable from: 3.44.1-0ubuntu2]
evolution-data-server/jammy-updates 3.44.2-0ubuntu1 amd64 [upgradable from: 3.44.1-0ubuntu2]

```

![Listing all the upgradable packages](/data/attachment/album/202207/24/231248le90whaikrr0r600.jpg)


它**按字母顺序列出所有可升级的软件包**以及有关当前安装版本和新可用软件包版本的信息。



```
brave-browser/stable 1.40.113 amd64 [upgradable from: 1.40.107]

```

例如，这显示我系统上安装了 Brave 浏览器，版本 1.40.107，并且版本 1.40.113 可用。


你能用这些信息做什么？让我分享一些我能想到的事情。


### 升级所有包


这可能是大多数普通 Ubuntu 用户所做的。你可以使用以下命令升级所有可升级包：



```
sudo apt upgrade

```

它列出了将要升级的软件包，然后要求按回车或 `Y` 确认升级。


![Upgrade all packages](/data/attachment/album/202207/24/231300l7adqi7j3kd77ham.jpg)


如果你确定要升级所有软件包，则可以通过在命令中添加 `-y` 来跳过 “Do you want to continue” 部分。



```
sudo apt upgrade -y

```

### 模拟升级（但不升级任何包）


这是人们在 `apt list` 命令之前所做的。使用模拟选项，你实际上不会进行任何更改。它仅显示运行升级时将安装或升级的软件包。



```
apt -s upgrade

```

你不需要使用 `sudo`（即使我在下面的截图中使用了它）。


![Running an upgrade simulation with apt command](/data/attachment/album/202207/24/231336xtkp8p772zr61kl6.jpg)


### 仅升级选定的包


如果你正在管理一个 Ubuntu 服务器，并且你不想升级所有软件包，而只想升级少数选定的软件包中的一个（如 MySQL/Ngnix），你可以使用 `apt` 命令轻松完成。



```
sudo apt --only-upgrade install package_name

```

实际上，如果你在已安装且有可用更新的软件包上运行 `apt install` 命令，它将升级该软件包。


使用 `--only-upgrade` 标志，你可以确保**仅升级**软件包（如果已安装）。如果尚未安装，它将不会安装给定的包。


你还可以通过提供名称来升级选定的几个包：



```
sudo apt --only-upgrade install package1 package2

```

你也可以做相反的事情，[升级时保留选定的软件包](https://itsfoss.com/prevent-package-update-ubuntu/)。



```
sudo apt-mark hold package_name

```

这样，当你升级所有系统包时，将不会升级给定的包。


你可以使用以下命令删除保留设置：



```
sudo apt-mark unhold package_name

```

### 是否显示内核升级？


这有点棘手。


当你运行 `apt list –upgradable` 命令时，它会显示所有可以升级的包。


但是如果有新的内核版本可用，它们可能不会显示，因为内核包名称以 `linux-headers-x-y` 开头。这是因为系统将它们视为新包，而不是对已安装的包 `linux-headers-a-b` 的升级。


但是，你仍然会在可升级包列表中看到 `linux-generic-hwe` 类型的包，因为该软件包将被升级（使用较新的内核）。


### 总结


列出可升级包的能力是 `apt` 命令为旧的 `apt-get` 命令带来的几个新功能之一。有关此主题的更多信息，你可以阅读我 [解释 apt 和 apt-get 命令之间的区别](https://itsfoss.com/apt-vs-apt-get-difference/) 的文章。


作为桌面用户，我并不总是检查可以升级的软件包。我直接去升级。但是，当我管理服务器时，我更喜欢查看可用的更新，然后决定是否进行升级。


你呢？你觉得这个功能对你自己有用吗？




---


via: <https://itsfoss.com/apt-list-upgradable/>


作者：[Abhishek Prakash](https://itsfoss.com/author/abhishek/) 选题：[lkxed](https://github.com/lkxed) 译者：[geekpi](https://github.com/geekpi) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
![Warp Terminal](/assets/images/warp-terminal.webp)


![Warp Terminal](/assets/images/warp-terminal.webp)

The [apt command](https://itsfoss.com/apt-command-guide/) is used for package management in Debian and Ubuntu. While you are probably already familiar with the install and remove options, apt provides a few extra features as well.

One of them is the ability to see all the upgradable packages on your system. And to display them, all you have to do is to use this command in the terminal:

`apt list --upgradable`


As you can notice, you don’t even need sudo to list the updatable packages. It just lists the packages that can be updated. It doesn’t update them.

In fact, the apt command adds this hint when you run the `sudo apt update`

command to update the local package repository cache.

```
Fetched 1,243 kB in 17s (71.4 kB/s)
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
30 packages can be upgraded. Run 'apt list --upgradable' to see them.
```


I don’t recall any similar direct option in the older apt-get command to list all the upgradable packages. That’s one of the several new features apt has added on top of the older apt-get command.

Let’s talk about it in a bit more detail.

## Listing all the upgradable packages

What you should know here is that **you only get to list the updates available through the APT package manager.** So, if you have added PPAs or [external repositories](https://itsfoss.com/adding-external-repositories-ubuntu/) to your system’s sources.list, you’ll see the updates from them.

But you won’t get updates for AppImage, Flatpak, Snap or some other packaging formats here.

In other words, it works with apt packages only.

So, to list all the upgradable packages on your Ubuntu or Debian system, you should update the local package cache first:

`sudo apt update`


And then your system will be aware of the available package updates. The apt command tells you how many packages can be upgraded at the end of the update command:

![The apt command shows the number of upgradable packages at the bottom of the apt update command output](https://itsfoss.com/content/images/wordpress/2022/07/update-package-cache-ubuntu-800x461.png)


![The apt command shows the number of upgradable packages at the bottom of the apt update command output](https://itsfoss.com/content/images/wordpress/2022/07/update-package-cache-ubuntu-800x461.png)

To see what package can be upgraded, run the command:

`apt list --upgradable`


You should see an output like this:

```
abhishek@itsfoss:~$ apt list --upgradable
Listing... Done
apparmor/jammy-updates 3.0.4-2ubuntu2.1 amd64 [upgradable from: 3.0.4-2ubuntu2]
brave-browser/stable 1.40.113 amd64 [upgradable from: 1.40.107]
evolution-data-server-common/jammy-updates,jammy-updates 3.44.2-0ubuntu1 all [upgradable from: 3.44.1-0ubuntu2]
evolution-data-server/jammy-updates 3.44.2-0ubuntu1 amd64 [upgradable from: 3.44.1-0ubuntu2]
```


![Listing all the upgradable packages in Ubuntu](https://itsfoss.com/content/images/wordpress/2022/07/apt-list-upgradable-packages-ubuntu-800x776.webp)


![Listing all the upgradable packages in Ubuntu](https://itsfoss.com/content/images/wordpress/2022/07/apt-list-upgradable-packages-ubuntu-800x776.webp)

It **lists all the upgradable packages in alphabetical order** with the information on the currently installed version and the new available package version.

`brave-browser/stable 1.40.113 amd64 [upgradable from: 1.40.107]`


For example, It shows that I have Brave browser version 1.40.107 installed on the system, and version 1.40.113 is available.

What can you do with this information? Let me share a few things I can think of.

## Upgrade all the packages

This is probably what most casual Ubuntu users do. You can upgrade all the upgradable packages with the following command:

`sudo apt upgrade`


It lists what packages will be upgraded and then asks to confirm the upgrade by pressing enter or Y.

![Upgrade all packages in Ubuntu with apt command](https://itsfoss.com/content/images/wordpress/2022/07/upgrade-all-packages-ubuntu-800x413.webp)


![Upgrade all packages in Ubuntu with apt command](https://itsfoss.com/content/images/wordpress/2022/07/upgrade-all-packages-ubuntu-800x413.webp)

If you are sure about upgrading all the packages, you can skip the ‘Do you want to continue’ part by giving it the go ahead by adding -y to the command.

`sudo apt upgrade -y`


## Simulate an upgrade (but don’t upgrade any packages)

This is what people did before the apt list command. With the simulate option, you don’t actually make any changes. It just shows what packages will be installed or upgraded if you run the upgrade.

`apt -s upgrade`


You don’t need to use sudo (even though I have used it in the screenshot below).

![Running an upgrade simulation with apt command in Ubuntu](https://itsfoss.com/content/images/wordpress/2022/07/run-an-upgrade-simulation-apt-ubuntu-800x478.webp)


![Running an upgrade simulation with apt command in Ubuntu](https://itsfoss.com/content/images/wordpress/2022/07/run-an-upgrade-simulation-apt-ubuntu-800x478.webp)

## Upgrade only the selected packages

If you are managing an Ubuntu server and you don’t want to upgrade all the packages but only one of a few selected ones (like MySQL/Ngnix), you can do that easily with the apt command.

`sudo apt --only-upgrade install package_name`


Actually, if you run the apt install command on an already installed package for which an update is available, it will upgrade the package.

With the `--only-upgrade`

flag, you ensure that a package is only upgraded (if it is already installed). It won’t install the given package if it is not already installed.

You can also upgrade selected few packages by providing their name:

`sudo apt --only-upgrade install package1 package2`


You can also do the opposite and [hold selected packages from the upgrade](https://itsfoss.com/prevent-package-update-ubuntu/).

`sudo apt-mark hold package_name`


With that, the given package won’t be upgraded when you upgrade all the system packages.

You can remove the hold with this command:

`sudo apt-mark unhold package_name`


## Does it show the kernel upgrades?

This is kind of tricky.

When you run the ‘apt list –upgradable’ command it shows all the packages that can be upgraded.

But if there are new kernel versions available, they might not be shown since the kernel package name starts with linux-headers-x-y. It’s because the system treats them as new packages, not an upgrade on already installed package linux-headers-a-b.

However, you would still see “linux-generic-hwe” kind of package in the list of upgradable packages. Because that package will be upgraded (with the newer kernel).

## Conclusion

The ability to list upgradable packages is one of the several new features the apt command brought over the older apt-get command.

[Using apt Commands in Linux [Ultimate Guide]This guide shows you how to use apt commands in Linux with examples so that you can manage packages effectively.](https://itsfoss.com/apt-command-guide/)![](https://itsfoss.com/content/images/wordpress/2017/07/apt-commands-example.png)


![](https://itsfoss.com/content/images/wordpress/2017/07/apt-commands-example.png)

For more on this topic, you can read my article [explaining the difference between the apt and apt-get commands](https://itsfoss.com/apt-vs-apt-get-difference/).

[Difference Between apt and apt-get Commands [Explained]This article explains the difference between apt and apt-get commands of Linux. It also lists some of the most commonly used apt commands that replace the older apt-get commands.](https://itsfoss.com/apt-vs-apt-get-difference/)![](https://itsfoss.com/content/images/wordpress/2017/07/apt-vs-apt-get-featured.png)


![](https://itsfoss.com/content/images/wordpress/2017/07/apt-vs-apt-get-featured.png)

As a desktop user, I don’t always check the packages that can be upgraded. I go for the upgrade straightaway. However, when I am managing a server, I prefer to see what updates are available and then decide whether or not I am going for an upgrade.

How about you? Do you see a good use for this feature for yourself?