This is a resource for the WSL Playlist.
-
[Optional] Check version
- Update Windows.
- Press
Win+R
. - Check Windows version by typing
winver
and pressingEnter
. You should see at leastVersion 1909 (OS Build 18363.1049)
. If you don't see this just wing it by trying to enable WSL anyway (as described below).
-
Enable WSL
- Press
Win+R
. - Type in
OptionalFeatures
and pressEnter
. - Enable:
Windows Subsystem for Linux
for WSL1Virtual Machine Platform
for WSL2 (you need both a and b)- On Windows Pro Edition
Hyper-V
(Only required for WSL2)
- Press
OK
. - Restart your machine.
- Press
-
[Optional] Install a decent terminal (recommended: Windows Terminal)
- Open the start menu by pressing the
Win
key. - Type in
store
to search for theMicrosoft Store
application and open it. - In the top right corner search for
Windows Terminal
. - Click
Get
.
- Open the start menu by pressing the
-
Open Windows Terminal
- Press
Win+R
. - Type in
wt
and pressEnter
(thewt.exe
is in this directory:%USERPROFILE%\AppData\Local\Microsoft\WindowsApps
which is on the%PATH%
).
- Press
-
[Optional but recommended] Change the default WSL version from 1 to 2
- Open Windows Terminal as described above.
- Type in
wsl --set-default-version 2
.
-
Install a distribution
- Open the start menu by pressing the
Win
key. - Type in
store
to search for theMicrosoft Store
application and open it. - In the top right corner search for
WSL
. - Click on (for example)
Ubuntu 20.04 LTS
. - Click
Get
(if already installed the button will showLaunch
). Note: the button is buggy and might sayLaunch
even if the distribution in question is not installed.
- Open the start menu by pressing the
-
Changing install location of a WSL distribution (can be used for backups). The default location is:
%USERPROFILE%\AppData\Local\Packages
- Open Windows Terminal as described above.
- List your distributions by typing
wsl -l -v
. - Export your distribution to wherever you like and wait for it to finish the export:
wsl --export DISTRIBUTION_FROM_STEP_2 DISTRIBUTION_FROM_STEP_2_backup
- You now have a file called
DISTRIBUTION_FROM_STEP_2_backup
that can be used as a backup - Remove/unregister the distribution (you have a backup):
wsl --unregister DISTRIBUTION_FROM_STEP_2
- Import your backup into a new location/distribution:
NEW_NAME can be the same as before but should not be if you want to install the same distribution multiple times. WSL differentiates distributions by name. This is also why the
wsl --import NEW_NAME path\to\new\location\NEW_NAME DISTRIBUTION_FROM_STEP_2_backup
Get
button in the Windows Store turns into aLaunch
button - it simply compares names.
-
Change version of a WSL distribution. Changing the version basically means converting from a regular file system to a virtual drive in case of going from WSL1 to WSL2 or the other way around in case of WSL2 to WSL1. This can either be done during the
import
step (see above) by adding the--version 1
or--version 2
flags respectively or simply by setting the version to the desired one by typingwsl --set-version DISTRIBUTION_NAME 1
orwsl --set-version DISTRIBUTION_NAME 2
but behind the scenes anexport
and a consequentimport
will happen for you, which is why it takes roughly the same amount of time. -
Change name of a WSL distribution. Unfortunately there is no shortcut that will do this for you. You need to jump thorugh the
export
->unregister
->import
hoops as described above, which can be easily scripted. -
[WSL
2
only] Free unused space by compressing the virtual hard drive. Similar to most databases, virtual hard drives don't free up the unused space after allocation. Note the size of the hard drive -> Copy over a large file -> Remove the file. Note the size of the hard drive. It didn't free the space of the removed large file.- Press the
Win
key. - Type in
powershell
. - Right click on
Windows PowerShell
and click on "Run as administrator". - Shutdown all wsl instances by typing
wsl --shutdown
. - Optimize the virtual hard drive:
Optimize-VHD -Path path\to\ext4.vhdx -Mode full
- Press the
These steps assume Ubuntu 20.04 LTS
which runs on WSL2
but should work under other distributions as well.
-
Open Windows Terminal as described above.
-
List your distributions by typing
wsl -l -v
. -
Connect to your distribution by typing
wsl -d Ubuntu-20.04
(replaceUbuntu-20.04
withDISTRIBUTION_FROM_STEP_2
if necessary) (the-d
is only necessary if multiple distributions are installed; otherwise simply typingwsl
should suffice). -
From here basic linux knowledge is assumed and most steps are optional so feel free to pick and choose what you need.
-
Update your distribution (this never hurts) by typing in something similar to:
sudo apt update sudo apt -y full-upgrade sudo apt -y autoremove
-
Make sure that the
/etc/wsl.conf
file looks like this:[user] default=your_user_name
-
You might be interested in using the following as your starting directory (the example shown is for Windows Terminal):
"startingDirectory": "\\\\wsl$\\playground\\home\\your_user_name"
-
The
hostname
cannot be changed but you can configure your shell (the example shown is forbash
) to simply display whatever you prefer. Open.bashrc
and replace the\h
(around line 60) with whatever you prefer.Before:
if [ "$color_prompt" = yes ]; then PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@\h\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ ' else PS1='${debian_chroot:+($debian_chroot)}\u@\h:\w\$ ' fi
After:
if [ "$color_prompt" = yes ]; then PS1='${debian_chroot:+($debian_chroot)}\[\033[01;32m\]\u@your_machine_name\[\033[00m\]:\[\033[01;34m\]\w\[\033[00m\]\$ ' else PS1='${debian_chroot:+($debian_chroot)}\u@your_machine_name:\w\$ ' fi
-
Create a shortcut that will open your terminal (the example shown is for Windows Terminal) and automatically connect to your distribution:
-
Right click on an empty space ->
New
->Shortcut
. -
Type in
wt
and pressEnter
. -
Type in
your_distribution_name
or whatever you prefer and pressEnter
. -
Right click on your shortcut ->
Properties
. -
Change the
Target
by adding-p your_distribution_name
:Before:
C:\Users\agilesteel\AppData\Local\Microsoft\WindowsApps\wt.exe
After:
C:\Users\agilesteel\AppData\Local\Microsoft\WindowsApps\wt.exe -p your_distribution_name
-
Press
OK
.
-
-
Cache your ssh key with keychain:
sudo apt install -y keychain
- put the following into your
~/.zshrc
(do NOT put it in your .profile):if [[ $(command -v keychain) ]]; then eval `keychain --eval --quiet id_rsa` fi
-
Install and configure an X Server (for GUI apps and clipboard sharing).
- There are multiple to choose from. I tried Xming and had no issues with it. Get it here and install it by clicking
Next
a bunch of times. The default settings work just fine so there is no need to fiddle with them. - During the last step of the installation wizard you will be offered to launch the server. Feel free to do so. The following program is executed behind the scenes for you:
This program allows you to configure and more importantly to save the configuration at the last step of the configuration wizard. The configuration is an executable.
C:\Program Files (x86)\Xming\XLaunch.exe
- The default configuration is fine except for one flag -
No Access Control
. Make sure to set it to true. - Save the configuration in your startup directory so that it will be run on Windows startup and you can thus forget about it:
- Press
Win+R
. - Type in
shell:startup
and pressEnter
(this is the startup directory).
- Press
- There are multiple to choose from. I tried Xming and had no issues with it. Get it here and install it by clicking
-
Configure the X Client (your WSL distribution). Your distribution(s) (the client(s)) need(s) to send GUI information to the server. The server (Windows) ip is dynamic but at least it is kept for you in the
/etc/resolv.conf
file. Put the following into your~/.profile
:export DISPLAY=$(awk '/nameserver / {print $2; exit}' /etc/resolv.conf 2>/dev/null):0 export LIBGL_ALWAYS_INDIRECT=1
-
Configure neovim clipboard sharing:
- Install xclip:
sudo apt install -y xclip
- Make sure that the
set clipboard=unnamedplus
neovim setting is enabled in~/.config/nvim/init.vim
or similar. - This together with the X Server will allow you to share the clipboard with Windows even in
NORMAL
mode.
- Install xclip:
-
[Not required when run from VS Code] Run Java server applications. WSL automatically forwards all ports to Windows. However it does not yet support
IPv6
. That said Java applications useIPv6
by default. Use the following system property to force it to useIPv4
instead:export _JAVA_OPTIONS="-Djava.net.preferIPv4Stack=true" # or export JAVA_TOOL_OPTIONS="-Djava.net.preferIPv4Stack=true"
- The
%PATH%
(Windows) and$PATH
(Linux) are mixed together so runningexplorer.exe
will find the Windows explorer program and open it. The same goes forcode
which would run Visual Studio Code in Windows assuming you have it installed. - VS Code will notice that it is being called from WSL and will offer you to install the
Remote - WSL
extension. Do it. This will install theserver
part in Linux and theclient
part in Windows. - Some of your Windows VS Code extensions need to be installed on the
server
which can easily be done from the VS Code extensions page.