DEV Community

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

Posted on • Edited on

Upgrade Ubuntu Version on WSL

Prerequisites

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



# Linux Terminal
whoami


Enter fullscreen mode Exit fullscreen mode

who am I

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



#PowerShell
wsl --list --verbose


Enter fullscreen mode Exit fullscreen mode

wsl list

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

Backup

All next steps 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 command.



# PowerShell
wsl --shutdown


Enter fullscreen mode Exit fullscreen mode

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



# PowerShell
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

When finished, it is very important ensure that the D:\backup\ubuntu.tarfile was created.

Relaunch

Relaunch WSL by running the command:



# PowerShell
wsl --distribution Ubuntu


Enter fullscreen mode Exit fullscreen mode

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

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



# Linux Terminal
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.



# Linux Terminal
sudo apt install update-manager-core


Enter fullscreen mode Exit fullscreen mode

Install the new version with



# Linux Terminal
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.

⚠ 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 the -d flag added with the command



# Linux Terminal
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



# Linux Terminal
sudo reboot


Enter fullscreen mode Exit fullscreen mode

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 steps 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!



# PowerShell
wsl --unregister Ubuntu


Enter fullscreen mode Exit fullscreen mode

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



# PowerShell
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 the default user, to switch to our user we need to run these commands.



# PowerShell
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 the command:



# PowerShell
wsl --distribution Ubuntu


Enter fullscreen mode Exit fullscreen mode

FAQ

Answer: Once restarting the terminal, got this error message:



grep: /proc/sys/fs/binfmt_misc/WSLInterop: No such file or directory


Enter fullscreen mode Exit fullscreen mode

Solution: You miss running the command sudo reboot after updating Ubuntu. If this still does not work, restart WSL by running wsl --shutdown and then wsl --distribution Ubuntu


That’s All Folks!
Happy Coding 🖖

beer

Top comments (8)

Collapse
 
chriscthomas profile image
chris thomas

Thanks! This was helpful especially with regards to changing the upgrade prompt for LTS to non-LTS versions.

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.

Collapse
 
jortiz12 profile image
jortiz12

First, many thanks for such an useful guide.
However, when running the command sudo apt update, my WSL session gets stuck, trying to connect to Ubuntu's severs. It shows the following message,
0% [Connecting to security.ubuntu.com]
, and after a while it warns you abt failure to connect.

Have googled this issue and, among the alternative solutions found, nothing works.

Any advice? Thanks vm in advance,
Jose

Collapse
 
equiman profile image
Camilo Martinez

Ubuntu version?
WSL version?

Collapse
 
sokenbicha profile image
sokenbicha • Edited

I've followed the steps, then at the end when I try to sudo reboot I get the error:

System has not been booted with systemd as init system (PID 1). Can't operate.
Failed to connect to bus: Host is down
Failed to talk to init daemon.

Any advice?

WSL2
Previous: Ubuntu 20.04.5 LTS
Updated: Ubuntu 22.04.1 LTS

Collapse
 
equiman profile image
Camilo Martinez

Maybe this article can help

partitionwizard.com/partitionmanag...

Collapse
 
anverg profile image
anverg

Thanks. Great guide! I get stuck after installing update-manager-core.
Trying to install a new version with sudo do-release-upgrade, I get ModuleNotFoundError: No module named 'apt_pkg'
Ubuntu version 20.04
WSL 2

Collapse
 
equiman profile image
Camilo Martinez

Seem to be something related to Python

stackoverflow.com/questions/562185...