Essential Commands for Managing WSL Environments
Enabling Required Windows Features
Before using WSL, the subsystem and virtual machine platform must be activated.
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
Installing and Updating WSL
WSL can be set up using the --install flag, and updated to the latest version with --update.
wsl --install
wsl --update
Setting the WSL Default Version
Switching the default version to WSL 2 ensures better performance and full system call compatibility.
wsl --set-default-version 2
Checking WSL Status and Version Information
Review the current state of WSL and the installed client version.
wsl --status
wsl --version
Working with Individual Distributions
Start a specific distribution by name.
wsl -d <DistributionName>
End a session within a distribution using exit or logout.
exit
View all installed distributions along with their WSL version and running status.
wsl --list --verbose
# or the short form:
wsl -l -v
Managing Distribution Lifecycles
List distributions available from the online store.
wsl --list --online
Install a new distribution by specifying its name.
wsl --install -d <DistributionName>
Set a distribution as the default target for wsl commands (example with Ubuntu 22.04).
wsl --set-default Ubuntu-22.04
Remove a distribution completely.
wsl --unregister <DistributionName>
Shutting Down WSL
Terminate all running WSL instances and the background VM.
wsl --shutdown
Relocating a Distribution to a Different Drive
Moving a WSL 2 instance to another location involves exoprting, unregistering, and importing.
- Verify the target distribution exists with
wsl -l -v. - Export the distribution to a tar file (example with Ubuntu 22.04).
wsl --export Ubuntu-22.04 D:\Envs\wsl\ubuntu-22.04.tar - Unregister the original instance.
wsl --unregister Ubuntu-22.04 - Import the tar file into a new location, specifying WSL 2.
wsl --import Ubuntu-22.04 D:\Envs\wsl\ubuntu-22.04 D:\Envs\wsl\ubuntu-22.04.tar --version 2 - Launch the relocated instance.
Integrating with Visual Studio Code
With the WSL extension installed in VS Code, open any directory inside a WSL distribution by executing code . from the Linux shell. VS Code will automatically connect to that WSL instance.