Installing and Running Linux Distributions with WSL2 on Windows
The Windows Subsystem for Linux (WSL) is a Windows feature that enables developers to install and run Linux distributions directly on Windows without the overhead of a traditional virtual machine or dual-boot setup.
WSL offers several conveniences:
- Launch a Linux terminal direct from the Windows terminal without starting a full virtual machine.
- Access Windows files from Linux without configuring shared folders.
This guide covers enabling WSL2 and installing a Linux distribution.
Enabling the WSL2 Feature
System Requirements for WSL2
- Windows 10:
- x64 systems: Version 1903 or later, Build 18362.1049 or higher.
- ARM64 systems: Version 2004 or later, Build 19041 or higher.
- Windows 11: All versions are supported.
To check your Windows version:
- Press
Win + Rto open the Run dialog. - Type
winverand press Enter.
Enabling Required Windows Features (Windows 11 Example)
- Open the Control Panel by pressing
Win + R, typingcontrol, and pressing Enter. - Click Programs.
- Click Turn Windows features on or off.
- Scroll down and check the boxes for Windows Subsystem for Linux and Virtual Machine Platform.
- Click OK and restart your computer when prompted.
Setting WSL2 as the Default Version and Updating
Open a terminal (Command Prompt or PowerShell) as Administrator and run the following commands:
- Set WSL2 as the default version:
wsl --set-default-version 2
- Update the WSL kernel:
wsl --update
Verification: Run wsl -v to confirm the installation was successful.
Installing a Linux Distribution
You can install Linux distributions designed for WSL. Microsoft provides several methods.
Method 1: Microsoft Store
- Open the Microsoft Store.
- Search for your desired distribution (e.g., "Ubuntu 22.04").
- Click Get or Install.
After installation, launch the distribution from the Start Menu. On first launch, you will be prompted to create a new username and password.
Method 2: Using WSL Command Line
You can view available distributions with:
wsl --list --online
To install a specific distribution (e.g., Ubuntu 24.04):
wsl --install -d Ubuntu-24.04
If the --list --online command fails due to network issues, using the Microsoft Store is recommended.
Managing Linux Distributions with WSL Commands
Listing Installed Distributions
wsl --list --verbose
The output shows installed distributions, their state (Running/Stopped), and the WSL version (1 or 2). The default distribution is marked with an asterisk (*).
Running Linux
- To start the default distribution:
wsl - To set a new default distribution:
wsl --set-default <DistributionName>
- To start a specific distribution with a specific user:
wsl --distribution <DistributionName> --user <UserName>
Changing the Default User for a Distribution
After initial setup, the default user is the one you created. To change it:
<DistributionName> config --default-user <UserName>
Example for Ubuntu 22.04:
ubuntu2204 config --default-user root
Exporting and Importing Linux Distributions
To move a distribution to free up space on the system drive (e.g., C:), you can export and import it.
- Shut down WSL:
wsl --shutdown
- Export the distribution to a tar file:
wsl --export <DistributionName> <ExportFilePath>
Example:
wsl --export Ubuntu-22.04 E:\Backup\ubuntu2204.tar
- Unregister (uninstall) the original distribution:
wsl --unregister <DistributionName>
- Import the distribution from the tar file to a new location:
wsl --import <DistributionName> <InstallLocation> <ExportFilePath>
Example:
wsl --import Ubuntu-22.04 E:\WSL\Ubuntu22 E:\Backup\ubuntu2204.tar
After a successful import, you can delete the original .tar export file.
Note: The imported distribution will use root as the default user. To set a different default user, you must modify the /etc/wsl.conf file within the distribution.
- Start the distribution.
- Run the following command, replacing
<YourUsername>with an existing username:
echo -e "[user]\ndefault=<YourUsername>" >> /etc/wsl.conf
- Cloce the terminal and restart the distribution for the change to take effect.