Fundamentals 14 min read

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.

Ops Development Stories
Ops Development Stories
Ops Development Stories
Master WSL2 on Windows: Step‑by‑Step Installation, Configuration, and Optimization

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

wsl

from PowerShell.

2.3 Install Ubuntu on a different drive (e.g., D:)

Create a folder such as

D:\Linux

and 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.list

with 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

https

to

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.json

and replacing

defaultProfile

with Ubuntu's GUID.

4.1 Install oh‑my‑zsh

In Ubuntu, install

zsh

and

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

~/.zshrc

to set the theme to

agnoster

or

robbyrussell

, then tweak the theme file

~/.oh-my-zsh/themes/agnoster.zsh-theme

to 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

plugins

array in

~/.zshrc

and reload:

<code>source ~/.zshrc</code>

5 Uninstall WSL

List installed distributions:

<code>wsl --list --all</code>

Unregister a distribution (replace

&lt;System_name&gt;

with the actual name):

<code>wsl --unregister &lt;System_name&gt;</code>

Delete the remaining files from the installation path if necessary.

Development EnvironmentTerminalPowerShellWindows Subsystem for LinuxWSL2Linux on Windows
Ops Development Stories
Written by

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.

0 followers
Reader feedback

How this landed with the community

login Sign in to like

Rate this article

Was this worth your time?

Sign in to rate
Discussion

0 Comments

Thoughtful readers leave field notes, pushback, and hard-won operational detail here.