This article is transcoded by SimpRead, original source zhuanlan.zhihu.com
If you are a Windows user and find using Linux inconvenient, this article can provide you with a relatively comfortable Linux development environment on Windows 10 that does not require high specifications and can run smoothly. (My laptop only has 8GB of RAM and works perfectly fine.)
What is wsl2? Simply put, it allows you to use Linux on Windows and supports Docker.
Generally, 16GB or more RAM is best. My 8GB laptop is just barely enough to run it, but I feel the performance is still better than VMware. So for Windows users who want to use Linux, wsl2 is a good choice!
This article starts from zero installing wsl2 on Windows 10 and provides a script to switch to Tsinghua mirror. It also connects to VSCode. The operation is quite simple: basically copy commands to execute + restart + copy commands to execute again.
PowerShell
Search for PowerShell in the search bar and open it as administrator.

wsl Installation
Enter the following four commands:
wsl --install
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
wsl --set-default-version 2
Then restart your computer!!! The installation of wsl is complete.
On Windows 10 (11) after version 2022.3, after restarting, a new Ubuntu window will open where you just enter your new username and password. That means Ubuntu 20.04 is automatically installed on drive C and you can use it directly. If you need to install other distributions or want to install Ubuntu 20.04 on drive D, continue reading. (But you need to uninstall Ubuntu 20.04 first.)

For Windows 10 (11) after 2022.3, skip this step directly.
Download the Linux kernel update package (this step updates wsl1 to wsl2 Comparison of WSL 2 and WSL 1)
-
Download the latest package:
-
Run the update package downloaded in the previous step. (Double-click to run - the system will prompt you for elevated privileges, select “Yes” to approve the installation.)
(Newbies can ignore this. PS: If you use wget in PowerShell (it works in bash) to download, the status code and data size all show 200 OK, but the file is missing. I don’t know the reason. Please let me know if you can solve it.)
wget https://wslstorestorage.blob.core.windows.net/wslblob/wsl_update_x64.msi
Ubuntu 20.04 (Drive C)
Installing on drive C is simple, just download Ubuntu 20.04 from the Windows Store and install.
Ubuntu 20.04 (Drive D)
Below shows how to install Ubuntu 20.04 on drive D:
First, create a folder on your preferred drive, for example D:\Linux. I prefer drive D because even if I reinstall the system, I don’t need to reinstall software.
Then go into this folder to download Ubuntu 20.04:
Invoke-WebRequest -Uri https://wsldownload.azureedge.net/Ubuntu_2004.2020.424.0_x64.appx -OutFile Ubuntu20.04.appx -UseBasicParsing
My example path is d:\xiazai\linux1, wait until download completes; the file size is over 4GB, waiting a while is normal.

Then execute the following four commands, as shown in the picture:
Rename-Item .\Ubuntu20.04.appx Ubuntu.zip
Expand-Archive .\Ubuntu.zip -Verbose
cd .\Ubuntu\
.\ubuntu2004.exe

This screenshot shows I directly entered wsl because I have installed it before. For your first installation, a black window will pop up for a few minutes, then you just input your desired username and password.
At this point, errors may appear, any kind of errors can happen—I have encountered many!
I found the best solution is to repeat the first few steps described at the start of this article (make sure nothing is missed), then restart, you must restart! After that, try again; usually this will fix your issue.
Finally, you can check the installed version in PowerShell:
wsl -l -v

Switch mirror source
Because domestic access to the internet is slow, I usually switch to the Tsinghua mirror. Also note, the Ubuntu certificates in wsl are expired. If you want to change the source manually, remember to update the certificates first.
But I have already written a script for this.
Just run it inside Linux to switch to the Tsinghua mirror:
wget https://gitee.com/lin-xi-269/tools/raw/master/os/QHubuntu20.04 && bash QHubuntu20.04
vscode + wsl
Just download this extension.

The result is approximately like this, very convenient.

Memory control
If you have a large amount of memory, please skip this~
A simple way is like the picture below. My laptop has 8GB RAM, so I only set 2GB for wsl, which is enough for me.

[wsl2]
processors=8
memory=2GB
localhostForwarding=true
There are two other ways; refer to the following article:
Limit wsl2 from using too much memory
Reference articles:
Manual Installation Steps for Older WSL Versions
WSL2 install ubuntu 20.04 and change default installation location