Upgrade Ubuntu Version on WSL
Tags: terminal, wsl, linux, ubuntu
Published: 2022-04-28 17:21:00

## Prerequisites

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

```bash
# Linux Terminal
whoami
```

![who am I](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/e6kvdb7puzscbb96q0k5.png)

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

```bash
#PowerShell
wsl --list --verbose
```

![wsl list](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/w2xke436r85ci0t2aqgs.png)

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.

```bash
# PowerShell
wsl --shutdown
```

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

```bash
# PowerShell
mkdir D:\backup
wsl --export Ubuntu D:\backup\ubuntu.tar
```

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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/inlo80w5ccpkaxjcnv4d.gif)

When finished, it is very important **ensure** that the `D:\backup\ubuntu.tar`file was created.

# Relaunch 

Relaunch WSL by running the command:

```bash
# PowerShell
wsl --distribution Ubuntu
```

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

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

```bash
# Linux Terminal
sudo apt update
sudo apt list --upgradable
sudo apt upgrade -y
```

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.

```bash
# Linux Terminal
sudo apt install update-manager-core
```

Install the new version with

```bash
# Linux Terminal
sudo do-release-upgrade
```

---

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

```bash
# Linux Terminal
sudo do-release-upgrade -d
```

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

 ```bash
# Linux Terminal
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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/t27z7tqemo0j75hh8ayw.gif)

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

```bash
# PowerShell
wsl --unregister Ubuntu
```

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

```bash
# PowerShell
mkdir D:\WSL
wsl --import Ubuntu D:\WSL\ D:\backup\ubuntu.tar
```

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

```bash
# PowerShell
cd $env:USERPROFILE\AppData\Local\Microsoft\WindowsApps
ubuntu config --default-user equiman
```
> Ensure replacing `equiman` with yours.

Restart WSL running the command:

```bash
# PowerShell
wsl --distribution Ubuntu
```

---

# FAQ

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

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

**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](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/mkgsx4aw6qn9i9ax6as9.png)](https://github.com/sponsors/deinsoftware)