WSL 2
Learn to install WSL, an easy Linux environment on Windows, and configure it for code development.
Last updated
Learn to install WSL, an easy Linux environment on Windows, and configure it for code development.
Last updated
After reboot, updates are applied, and the terminal is automatically started to continue Ubuntu installation on WSL.
It will ask you to setup your Unix user and password.
If everything went well running wsl
in terminal will open the connection to Ubuntu.
Then create a folder where the new instance will be used, and run the command to import it.
Where:
<distribution-name>
: Is the name you want to give this new Linux instance.
<installation-folder>
: Is the folder you want to install the new Linux instance.
<wsl2-tar-file-path>
: Is the path to the downloaded .tar.gz
file.
After the import verify the installation:
Imported distributions will log as root
user. Manually create another user.
Bring up the instance with wsl -d <distro-name>
.
Set this user as the default login user by creating or editing /etc/wsl.conf
.
Also add to the /etc/wsl.conf
a new hostname so that the instance's hostname reflect the distro name.
After update the /etc/hosts
and add the new hostname with 127.0.0.1
.
After, shutdown the instance with wsl --terminate <distro-name>
. Log with the created user and finish the installation with:
When using your WSL, do NOT run things on /mnt/c/*
because it will lose performance.
Accessing Windows files from inside Linux is possible by accessing from /mnt/c
.
On Windows the Linux filesystem is basically a network folder as \\wsl$
.
wsl
Start the default distribution.
wsl -l -v
List all distributions with detailed information.
wsl --shutdown
Shutdown ALL distributions.
wsl -d <distro-name>
Connect to a specific distribution, in case you have more than one.
wsl --terminate <distro-name>
Shutdown specific distribution.
wsl --manage <distro-name> --move <new-location>
Move the distribution to a new location.
wsl --set-default <distro-name>
Sets the distribution as the default.
Will have a *
beside their name.
wsl --unregister <distro-name>
Unregisters the distribution and deletes the root filesystem.
wsl --help
Show all wsl options and arguments.
.wslconfig
vs wsl.conf
The .wslconfig
is a global configuration file that applies configuration to ALL the distros.
This file is not automatically created.
Must be placed to your Windows User root folder. (%UserProfile%
or C:\Users\<UserName>
)
WSL will detect the existence of this file every time it is initiated. If the file is incorrect, WSL will initiate without the specified configurations.
If you want to setup configurations for specific distros then use the wsl.conf
file.
You will place this file in /etc
folder of the desired distribution. (/etc/wsl.conf
)
.vhdx
fileThe WSL2 virtual disk is a vhdx
file that grows as you use your linux distro. If you exclude files, the disk will not shrink automatically. (vhdx
always grows)
To make it auto reduce, active the sparse
mode of WSL2 on the .wslconfig
. (Only for future created virtual disks)
For virtual disks that were already created you can convert them with:
If even after that the disk size does not decrease, you can manually shrink with this third-party tool.
Configure the WSL instance hostname to reflect the <distro-name>
given. This will make easier to identify which instance is running in the terminal, and it is not supposed to have any impact.
See here how to configure Setup instance hostname.
Your code project would stay inside Linux, but you will code from Windows.
Before openning projects inside WSL in VSCode, make sure you have the Remote Development
plugin installed in VSCode.
.vhdx
fileYou can find the .vhdx
file, which is the virtual machine hard drive file here:
You could save this entire file to backup the entire Linux.
If you format your computer, on your new install:
Install wsl again.
Configure same user and password (maybe not needed).
Shutdown the Linux virtual machine.
Substitute the .vhdx
file with the backup one.
Download the tar.gz
file of a .