TNS
VOXPOP
As a JavaScript developer, what non-React tools do you use most often?
Angular
0%
Astro
0%
Svelte
0%
Vue.js
0%
Other
0%
I only use React
0%
I don't use JavaScript
0%
NEW! Try Stackie AI
Linux / Operations

How To Safely Upgrade Ubuntu 22.04 to Ubuntu 24.04

The Ubuntu upgrade process has been one of the more reliable on the market. And given you get the latest packages and security patches, it's often worth the effort.
Sep 22nd, 2024 6:00am by
Featued image for: How To Safely Upgrade Ubuntu 22.04 to Ubuntu 24.04

Ubuntu 22.04 will be supported by Canonical until April 2027. Although that’s still roughly three years away, that’ll arrive faster than you think. On the other hand, Ubuntu 24.04 (which is another LTS release) is supported until April 2029, which is a more comfortable five-year window of support.

You can, of course, purchase an extra five years of support via the Expanded Security Maintenance program. But when you can upgrade to a newer release for free, why not?

I know what you’re thinking: you have a server running Ubuntu 22.04 with crucial services running (such as websites, databases, containers, etc.), so why would you want to upgrade?

In my experience, the Ubuntu upgrade process has been one of the more reliable on the market. It’s a very rare occasion that I see something go wrong with an upgrade. And given you get the latest packages and security patches, it’s often worth the effort.

That doesn’t mean everyone should jump onto the bandwagon and upgrade. You might have a situation where Ubuntu has been deployed such that upgrading to 24.04 might break something. You certainly don’t want that. This can happen when you’ve built an app or service that doesn’t support newer versions of dependencies. That, of course, can get you into trouble because your servers could be running libraries with security vulnerabilities. That’s a big reason to upgrade.

Either way, you might be curious as to how to undertake this process.

Let me walk you through it.

What You’ll Need

You’ll need three things for this:

  • A running instance of Ubuntu 22.04.
  • A user with sudo privileges.
  • An external drive to backup important data and/or configurations.

Run a Backup

Before you do anything, I would suggest you create a backup of either crucial data or directories. Even better you could consider doing a byte-for-byte copy of the drive (using the dd command) to an external.

To create a disk clone with dd, here’s the process:

  1. Connect your external drive.
  2. Make sure you know the name of the destination drive (which you can find with thelsblk command).
  3. Run the command sudo dd if=/dev/SOURCE of=/dev/DESTINATION bs=64K conv=noerror, sync (where SOURCE is the drive to be copied and DESTINATION is the drive to which the image will be copied).

When the command completes, the destination drive should mirror the source drive.

If you don’t need a clone of the entire drive, you could always just back up important directories, such as /etc, /srv, /var, /home, and any other directory that contains data required for the machine to run as needed.

Once you have the backup taken care of, it’s time to upgrade. I would recommend running this process during off hours when the server isn’t in use.

Update and Upgrade Ubuntu

The first thing we’re going to do is run an update and do a basic upgrade, so all the installed software is at the latest release version.

Update apt with:

sudo apt-get update

When this is complete, upgrade Ubuntu with:

sudo apt-get upgrade -y

Once that is taken care of, run a dist-upgrade, which intelligently handles changing dependencies with new versions of packages. For this, the command is:

sudo apt-get dist-upgrade

This process can take a bit longer than the average upgrade, but it depends on how out-of-date things are.

Upgrade the OS

Now that all of the installed software is updated to the latest versions available to the current distribution release, it’s time to upgrade the operating system itself. For that, we use a special command, which is:

sudo do-release-upgrade

The above command checks to see if there’s a new version of the OS available. If so, it’ll offer you a y/n option. Type y to continue.

If you’ve connected to the machine via SSH, the update process will ask if you want to continue. If so, SSH will be moved from port 22 to port 1022. I would recommend running the upgrade directly from the machine, instead of remotely because if something goes awry, you’re there to deal with it. In a remote situation, you could be kicked out of the SSH session and not be able to regain access.

At this point, if there’s an issue, the command will inform you of the problem and will not continue. You can then deal with whatever do-release-upgrade discovered and then run the command again. When do-release-upgrade detects no issues, the OS upgrade will begin. You’ll be prompted to okay the disabling of third-party repositories by pressing Enter on your keyboard when prompted. Next, you’ll be prompted to type y to continue or n to abort.

Type y and hit Enter on your keyboard. After that, you can sit back and watch the magic happen because, at some point, you’ll have to accept configurations such as the default language and keyboard layout. You might also find that the process asks if you want to keep certain default configurations or upgrade. What you do in such instances will depend on your needs. Finally, you’ll be asked if you want the installer to remove any/all packages that are no longer needed. Type y when prompted and allow that process to complete.

When do-release-upgrade finishes (it’ll take some time), you’ll be prompted to reboot the server. Once you’ve rebooted, log in and issue the following command to verify the upgrade was a success:

lsb_release -a

The output of the above command should include an entry like:

Release: 24.04

Congratulations, you’ve successfully upgraded Ubuntu 22.04 to 24.04. Make sure everything works as expected. If not, you have a cloned drive that you know works, so you should be able to swap out the drives and be back to square one.

Group Created with Sketch.
TNS DAILY NEWSLETTER Receive a free roundup of the most recent TNS articles in your inbox each day.