DEV Community

Cover image for Upgrade Ubuntu Version on WSL
Camilo Martinez
Camilo Martinez

Posted on • Updated on

Upgrade Ubuntu Version on WSL

Prerequisites

If we are using a username different than root, we need to know it running the whoami command (WSL side):

who am I

Also we need to know the distro name and WSL version, running the wsl --list --verbose command on PowerShell (Windows side):

wsl list

Knowing the username: equiman, the linux distribution: Ubuntu, and confirm that we are using the WSL 2 version, we are ready to...

Backup

All next steep need to be done on PowerShell (Windows side).

First, we need to close all terminals that are using WSL, and then shut it down to avoid data corruption running the wsl --shutdown command.

Next we are going to create a backup from our Ubuntu linux distribution.

mkdir D:\backup
wsl --export Ubuntu D:\backup\ubuntu.tar
Enter fullscreen mode Exit fullscreen mode

Don't worry if you do not see progress indicators. This can take a while depending on how much data you have. Maybe it's a good idea to take a rest.

waiting

Relaunch

When the backup is finished, relaunch WSL by running the command wsl and running these commands.

All next steep need to be done on WSL (Linux side).

Update the packages' source and update the outdated installed applications.

sudo apt update
sudo apt list --upgradable
sudo apt upgrade -y
Enter fullscreen mode Exit fullscreen mode

It’s important to install this update manager core package this will trick the system to think there is a new LTS available and allow you to do an in-place upgrade.

sudo apt install update-manager-core
Enter fullscreen mode Exit fullscreen mode

Install the new version with

sudo do-release-upgrade
Enter fullscreen mode Exit fullscreen mode

If you are using an LTS version (recommended) and found that there is no new LTS version available it will show this message

Checking for a new Ubuntu release
There is no development version of an LTS available.
To upgrade to the latest non-LTS development release 
set Prompt=normal in /etc/update-manager/release-upgrades.
Enter fullscreen mode Exit fullscreen mode

⚠ If it's not relevant to you to not use LTS versions, you can change the Prompt to normal in /etc/update-manager/release-upgrades file and rerun the previous command.


Another alternative is to use -d flag added with the command

sudo do-release-upgrade -d
Enter fullscreen mode Exit fullscreen mode

This is done because an upgrade to the latest LTS is not directly available till July of the year of release of the LTS. However, the -d flag forces the command to upgrade to the latest development release, and it also considers an LTS release as a development release.


Then, accept all packages you have to download, and it will take a while depending on your network and disk speed mostly.

Finally, run the command sudo reboot

Recover

If something goes wrong in the process, don't worry, we can restore our previous WSL disk from the backup.

Didn't You skip the backup step? right?

right

All next steep need to be done on PowerShell (Windows side).

First, we need to unregister the Linux distribution

⚠️ Beware that everything that was stored on the WSL drive will be lost, and the command won't ask for a confirmation, so make sure you backup everything you need first!

wsl --unregister Ubuntu
Enter fullscreen mode Exit fullscreen mode

And then, import again on the desired path. I'll use D: disk as an example.

mkdir D:\WSL
wsl --import Ubuntu D:\WSL\ D:\backup\ubuntu.tar
Enter fullscreen mode Exit fullscreen mode

By default Ubuntu will use root as default user, to switch to our user we need to run these commands.

cd $env:USERPROFILE\AppData\Local\Microsoft\WindowsApps
ubuntu config --default-user equiman
Enter fullscreen mode Exit fullscreen mode

Ensure replacing equiman with yours.

Restart WSL running wsl command and it's done.


That’s All Folks!
Happy Coding 🖖

ko-fi

Discussion (2)

Collapse
cavo789 profile image
Christophe Avonture

Very nice step by step guide, wow !

Bookmarked so I can upgrade my Ubuntu 20.04 soon... Thanks a million.