Install and configure WSL2 on Windows (Ubuntu 22.04) (Please refer to the official documentation directly)

Microsoft Official Documentation:

What is WSL?#

Windows Subsystem for Linux (WSL) is a feature of Windows that allows you to run a Linux environment on a Windows computer without the need for a separate virtual machine or dual boot. WSL is designed to provide a seamless and efficient experience for developers who want to use both Windows and Linux simultaneously.

What is WSL2?#

When installing a Linux distribution, WSL 2 is the default distribution type. WSL 2 runs the Linux kernel in a lightweight utility virtual machine (VM) using virtualization technology. Linux distributions run as isolated containers within the WSL 2 hosted VM. Linux distributions running through WSL 2 share the same network namespace, device tree (except /dev/pts), CPU/kernel/memory/swap space, and /init binary, but have their own PID namespace, mount namespace, user namespace, cgroup namespace, and init process.

Comparing WSL Versions#

WSL 2 improves file system performance and adds full system call compatibility compared to the WSL 1 architecture. The main differences between WSL 1 and WSL 2 are the use of an actual Linux kernel within the hosted VM, support for full system call compatibility, and performance across Linux and Windows operating systems. WSL 2 is the current default version when installing a Linux distribution, running the Linux kernel within a lightweight utility virtual machine (VM) using the latest and greatest virtualization technology. WSL 2 runs Linux distributions as isolated containers within the hosted VM.

Pasted image 20240122093059

From the comparison table above, it can be seen that the WSL 2 architecture is superior to WSL 1 in several aspects, except for the performance of cross-OS file systems. For this situation, it can be handled by storing project files on the same operating system as the tools used for processing the project.

Installing WSL2#

Quick Install#

You must be running Windows 10 version 2004 or higher (build 19041 or higher) or Windows 11 to use the following commands. If you are using an earlier version, please use the manual installation method.

  • Open PowerShell or Windows Command Prompt in administrator mode.
  • View the list of available distributions.
wsl --list --online
  • Run wsl --install -d <DistroName> to install a distribution, and restart your computer after installation.

New Linux installations installed using the wsl --install command will be set to WSL 2 by default without any additional steps.

When you launch a newly installed Linux distribution for the first time, a console window will open and ask you to wait for files to be decompressed and stored on your computer. All future launches should take less than a second.

You can list installed Linux distributions and check the WSL version for each distribution by entering the following command in PowerShell or Windows Command Prompt: wsl -l -v.

To set the default version to WSL 1 or WSL 2 when installing a new Linux distribution, use the command wsl --set-default-version <Version#>, replacing <Version#> with either 1 or 2.

Manual Installation for Older Versions#

  • Open PowerShell as an administrator and run the following command to enable the WSL feature.
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
  • Before installing WSL 2, you must run the following command to enable the "Virtual Machine Platform" optional feature.
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
  • To ensure that all related components are in place, you need to restart your system, otherwise, you may find that things are not going as expected.
    Restart your computer to complete the WSL installation and update to WSL 2.

  • Download the Linux kernel update package:
    (If you have an ARM64 computer, click on the provided ARM64 package in the document)

  • Run the update package and set WSL 2 as the default version in PowerShell.

wsl --set-default-version 2
  • Open the Microsoft Store, select your preferred Linux distribution, and download it.
    When you launch a newly installed Linux distribution for the first time, a console window will open and ask you to wait for one or two minutes for files to be decompressed and stored on your computer. All future launches should take less than a second.

Then, you need to create a user account and password for the new Linux distribution.

It is recommended to use Windows Terminal or VSCode to open WSL 2.
Windows Terminal:

Reference Documentation:

This installation also taught me a lesson. I used to rely on tutorials written by others, but today I read Microsoft's installation documentation, which is very detailed. And the previous installation tutorial I read was almost a copy of the official documentation (including mine... so there is no need to write it next time). The correct process is probably to read the official documentation first and then search for other solutions when encountering problems.

Read The Fucking Document!

Ownership of this post data is guaranteed by blockchain and smart contracts to the creator alone.