DEV Community

Aditya Kanekar
Aditya Kanekar

Posted on • Updated on

Upgrading from WSL1 to WSL2

Background

In my last post I talked about how to get started with developing applications on Linux on Windows 10 using WSL 2 and VS Code. Some of you might have installed the previous version of WSL1 on your Windows 10 machines. This article guides on how to upgrade from WSL to WSL2.

Install WSL2 Linux Kernel

You need at least 1903 version of Windows 10. You can check the minimum requirement for upgrading from WSL1 here To check windows version, click Windows + R Key and type,

winver
Enter fullscreen mode Exit fullscreen mode

You will be able to see the build version of your windows e.g. 1903.

Download the installer for the WSL2 kernel from,
https://docs.microsoft.com/en-us/windows/wsl/wsl2-kernel

Run the installer to install WSL2 Kernel for Linux. Once the installation is successful. Open PowerShell or command prompt and run,

wsl --list 
Enter fullscreen mode Exit fullscreen mode

This will list all the distros and the version of WSL in the version column. The list will look somewhat similar to this,

Alt Text

Since you were running WSL1 previously on your machine the distro is still using WSL1. No magic tricks here, none of your distros will be migrated to WSL2 by default.

Upgrading from WSL1 to WSL2

To upgrade your current distro(s) from WSL1 to WSL2. Run following command in PowerShell or command prompt,

wsl --set-version <distro-name> 2
Enter fullscreen mode Exit fullscreen mode

Replace the distro-name with the Name displayed in the last section. So in our case the command will look like,

wsl --set-version Ubuntu-20.04 2
Enter fullscreen mode Exit fullscreen mode

This will take some time to upgrade, in my experience all your data will be retained as it was previously. Once the upgrade is complete you can run the list distros command to verify whether the version was successfully upgraded.

wsl -l -v
Enter fullscreen mode Exit fullscreen mode

You should be able to see output similar to,

Alt Text

Now you can launch Ubuntu-20.04 from Start menu or from the command prompt run,

wsl -d Ubuntu-18.04
Enter fullscreen mode Exit fullscreen mode

This will take you to the Ubuntu-18.04 shell. The * denotes the default distribution. What does that mean? To check run following command,

wsl
Enter fullscreen mode Exit fullscreen mode

This will take you to Ubuntu-20.04 shell without mentioning the distro. If you want to change the default distro run,

wsl -s Ubuntu-18.04
wsl -l -v
Enter fullscreen mode Exit fullscreen mode

You will notice the default distribution changed to Ubuntu-18.04.

NAME STATE VERSION

  • Ubuntu-18.04 Stopped 2 Ubuntu-20.04 Stopped 2

Now if you run,

wsl
Enter fullscreen mode Exit fullscreen mode

Ubuntu-18.04 shell will launch. To verify if the right version is launched. Run following command,

lsb_release -a
Enter fullscreen mode Exit fullscreen mode

You should get output similar to,

No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 18.04.5 LTS
Release: 18.04
Codename: bionic

Now open another PowerShell or command prompt window and run,

wsl -l -v
Enter fullscreen mode Exit fullscreen mode

You will notice Ubuntu-18.04 is in Running state. IF you close the wsl window or run exit the state will change to Stopped.

Alt Text

Hopefully this will get you started on WSL2. In my next articles we will use WSL for running tools supported on Linux environment.

Till then, happy coding!

Top comments (9)

Collapse
 
jereje profile image
jereje

Thanks! Worked like a charm

Collapse
 
abodftw profile image
Abod

Thanks for the amazing article, I've put upgrading for months because I thought it was going to delete all the distro files, but after reading the article and your experience, I went for it, and all is good.

Collapse
 
mayukhchatterjee profile image
Mayukh Chatterjee

what if i upgrade wsl1 to wsl2, will i lost the other downloded tools like GCC, valgrind,gdb?

Collapse
 
adityakanekar profile image
Aditya Kanekar

None of my files were lost in the process, so I assume it will retain the tools. However I am not so sure on this one, but you can always reinstall the tools.

Collapse
 
ahmadwahyudi profile image
Ahmad

Thanks a lot brother, you've help me..
Firstly, I little confused read official docs about this topic. until I found this

Collapse
 
verlaine_devnet profile image
Verlaine_Devnet

Excellent Article !

Collapse
 
jpdev profile image
jpdev

thanks... it took around 10-15 minutes here

Collapse
 
abodftw profile image
Abod

NOTE: if you have configured docker for WSL 1, and now you're upgrading make sure to remove the DOCKER_HOST variable from your Linux distro config

Collapse
 
pratap360 profile image
Pratap Parui

Thanks Aditya it's help a lot