How to update your Ubuntu server packages

By

Published · Updated

Tagged: Computer

Learn how to keep an Ubuntu server up to date: apt update, apt upgrade vs full-upgrade, when to reboot, and how automatic security updates with unattended-upgrades work.

I have a few VPS based on Ubuntu and I have an alert on my calendar to log in from time to time and update the packages and keep up with security fixes.

Here’s the sequence I use (it’s mostly a note to myself for the next time I forget)

The commands

Connect to the server with an account that can use sudo, then refresh the package index:

sudo apt update

This downloads the latest package metadata. It does not upgrade anything yet.

At the end, apt tells you how many packages can be upgraded. To see which ones before you install anything:

apt list --upgradable

Now install the available upgrades:

sudo apt upgrade

Read the proposed changes before confirming.

Remove dependencies that are no longer needed with:

sudo apt autoremove

Then, if needed (depends on the kind of packages that needed an update), reboot the system:

sudo reboot

upgrade vs full-upgrade

apt upgrade is the safe option. It can install new packages when an update needs them, but it never removes an installed package. If upgrading a package would require removing another one, apt skips that upgrade.

apt full-upgrade does the same, but it’s allowed to remove installed packages when that’s needed to upgrade the system as a whole:

sudo apt full-upgrade

So when apt upgrade leaves some packages behind, full-upgrade is usually the next step. Read the list of packages it wants to remove before you say yes, especially on a server running something important.

Neither command moves you to a new Ubuntu release, like from 22.04 to 24.04. They only install updates for the release you’re on.

apt or apt-get?

Both work. Ubuntu’s docs say apt is meant for interactive use, when you type commands yourself. In scripts, use apt-get, because apt may change its behavior and output between versions, while apt-get keeps them stable.

In a script, add -y to answer yes automatically:

sudo apt-get update
sudo apt-get -y upgrade

Do you need to reboot?

Not every update needs a reboot, but a kernel update does. Ubuntu usually creates /var/run/reboot-required when an installed update requires a reboot. You can check it with:

test -f /var/run/reboot-required && echo "Reboot required"

Services like nginx or PostgreSQL also need a restart to use an updated library. You don’t usually have to do it by hand: starting with Ubuntu 24.04, needrestart runs after an upgrade and restarts the affected services automatically.

If you don’t want it to restart a specific service, don’t edit /etc/needrestart/needrestart.conf. Add your override in a file under /etc/needrestart/conf.d/ instead.

Automatic security updates

You might not need to log in for security fixes at all. Ubuntu installs the unattended-upgrades package by default, and it applies security updates automatically once a day.

Two files control it:

20auto-upgrades looks like this:

APT::Periodic::Update-Package-Lists "1";
APT::Periodic::Unattended-Upgrade "1";

Set a value to "0" to disable that step.

By default it only installs updates from the security pocket (plus Expanded Security Maintenance, if your system has it). Regular bug fix updates still wait for you to run apt upgrade.

It also doesn’t reboot by default, so a kernel fix is installed but not running until you reboot. To let it reboot on its own when an update needs it, set this in 50unattended-upgrades:

Unattended-Upgrade::Automatic-Reboot "true";
Unattended-Upgrade::Automatic-Reboot-Time "04:00";

The server then reboots without asking you, at the time you set, so only turn it on if a few minutes of downtime are fine.

To check what it would do without changing anything:

sudo unattended-upgrade -v --dry-run

To see what it installed, look at the logs in /var/log/unattended-upgrades/.

Want me to talk about your product? You can sponsor this site.

Related posts about computers: