Master WSL2 on Windows: Step‑by‑Step Installation, Configuration, and Optimization
This guide walks you through what WSL2 is, compares it with WSL1, explains why to use it, and provides detailed step‑by‑step instructions for enabling the required Windows features, installing Ubuntu, customizing the terminal, updating package sources, and even uninstalling the subsystem.
1 What is WSL2
WSL (Windows Subsystem for Linux) lets you run a GNU/Linux environment directly on Windows, including most command‑line tools, utilities, and applications.
WSL1 vs WSL2 comparison :
Both integrate Windows and Linux and have short startup times.
WSL2 provides a full Linux kernel, complete system‑call compatibility, and a hosted VM.
WSL2 uses more resources than WSL1 but offers better performance for many workloads.
Both can run alongside VMware or VirtualBox.
Why use WSL2 : It gives you the Windows GUI you love while providing a native‑like Linux command line, enabling shared file systems, Docker containers, and low‑overhead development.
2 Install WSL2
Prerequisites : Windows 10 version 2004 (or later) or Windows 11, and a machine that supports Hyper‑V virtualization.
Enable the "Virtual Machine Platform" feature (required for WSL2) with administrator PowerShell:
<code>dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart</code>For older Windows 10 builds (1903/1909) use:
<code>Enable-WindowsOptionalFeature -Online -FeatureName VirtualMachinePlatform -NoRestart</code>After enabling, restart the computer.
Install the default Linux distribution (Ubuntu) with:
<code>wsl --install</code>The command enables the optional WSL components, downloads the latest Linux kernel, sets WSL2 as default, and installs Ubuntu.
If WSL is already installed, list available distributions and install a specific one:
<code>wsl --list --online</code> <code>wsl --install -d Ubuntu-20.04</code>Check the installed version:
<code>wsl -l -v</code>If needed, set Ubuntu to use WSL2:
<code>wsl --set-version Ubuntu-20.04 2</code>Launch Ubuntu, create a user and password, then you can start using it with
wslfrom PowerShell.
2.3 Install Ubuntu on a different drive (e.g., D:)
Create a folder such as
D:\Linuxand download the Ubuntu .appx package:
<code>Invoke-WebRequest -Uri https://wsldownload.azureedge.net/Ubuntu_2004.2020.424.0_x64.appx -OutFile Ubuntu20.04.appx -UseBasicParsing</code>Rename, extract, and run the installer:
<code>Rename-Item .\Ubuntu20.04.appx Ubuntu.zip
Expand-Archive .\Ubuntu.zip -Verbose
cd .\Ubuntu\
.\ubuntu2004.exe</code>3 Update Linux package sources
Replace the default
/etc/apt/sources.listwith a faster mirror (e.g., Aliyun or Tsinghua):
<code># Example Aliyun mirror entries
deb https://mirrors.aliyun.com/ubuntu/ focal main restricted universe multiverse
... (other lines omitted for brevity)</code>If you encounter certificate verification errors, change
httpsto
http, reinstall
ca-certificates, then revert the URLs and run
sudo apt update && sudo apt upgrade.
4 Install Windows Terminal
Download Windows Terminal from the Microsoft Store (search for "Windows Terminal"). After installation, set Ubuntu as the default profile by editing the generated
profiles.jsonand replacing
defaultProfilewith Ubuntu's GUID.
4.1 Install oh‑my‑zsh
In Ubuntu, install
zshand
git:
<code>sudo apt update
sudo apt install git zsh -y</code>Change the default shell:
<code>chsh -s /bin/zsh</code>Install Powerline fonts:
<code>git clone https://github.com/powerline/fonts.git
cd fonts
./install.sh</code>Install oh‑my‑zsh (using a Chinese mirror if needed):
<code>wget https://gitee.com/mirrors/oh-my-zsh/raw/master/tools/install.sh
chmod +x install.sh
./install.sh</code>4.2 Configure theme
Edit
~/.zshrcto set the theme to
agnosteror
robbyrussell, then tweak the theme file
~/.oh-my-zsh/themes/agnoster.zsh-themeto customize colors.
4.3 Add plugins
Install autosuggestions and syntax‑highlighting plugins:
<code>git clone git://github.com/zsh-users/zsh-autosuggestions $ZSH_CUSTOM/plugins/zsh-autosuggestions
git clone git://github.com/zsh-users/zsh-syntax-highlighting $ZSH_CUSTOM/plugins/zsh-syntax-highlighting</code>Add them to the
pluginsarray in
~/.zshrcand reload:
<code>source ~/.zshrc</code>5 Uninstall WSL
List installed distributions:
<code>wsl --list --all</code>Unregister a distribution (replace
<System_name>with the actual name):
<code>wsl --unregister <System_name></code>Delete the remaining files from the installation path if necessary.
Ops Development Stories
Maintained by a like‑minded team, covering both operations and development. Topics span Linux ops, DevOps toolchain, Kubernetes containerization, monitoring, log collection, network security, and Python or Go development. Team members: Qiao Ke, wanger, Dong Ge, Su Xin, Hua Zai, Zheng Ge, Teacher Xia.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.