This is a resource for the WSL Playlist.
-
[Optional] Check version
- Update Windows.
- Press
Win+R. - Check Windows version by typing
winverand 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
OptionalFeaturesand pressEnter. - Enable:
Windows Subsystem for Linuxfor WSL1Virtual Machine Platformfor 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
Winkey. - Type in
storeto search for theMicrosoft Storeapplication 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
wtand pressEnter(thewt.exeis in this directory:%USERPROFILE%\AppData\Local\Microsoft\WindowsAppswhich 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
Winkey. - Type in
storeto search for theMicrosoft Storeapplication 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 sayLauncheven 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_backupthat 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
Getbutton in the Windows Store turns into aLaunchbutton - 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
importstep (see above) by adding the--version 1or--version 2flags respectively or simply by setting the version to the desired one by typingwsl --set-version DISTRIBUTION_NAME 1orwsl --set-version DISTRIBUTION_NAME 2but behind the scenes anexportand a consequentimportwill 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->importhoops as described above, which can be easily scripted. -
[WSL
2only] 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
Winkey. - Type in
powershell. - Right click on
Windows PowerShelland 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.04withDISTRIBUTION_FROM_STEP_2if necessary) (the-dis only necessary if multiple distributions are installed; otherwise simply typingwslshould 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.conffile 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
hostnamecannot be changed but you can configure your shell (the example shown is forbash) to simply display whatever you prefer. Open.bashrcand 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
wtand pressEnter. -
Type in
your_distribution_nameor whatever you prefer and pressEnter. -
Right click on your shortcut ->
Properties. -
Change the
Targetby adding-p your_distribution_name:Before:
C:\Users\agilesteel\AppData\Local\Microsoft\WindowsApps\wt.exeAfter:
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
Nexta 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:startupand 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.conffile. 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=unnamedplusneovim setting is enabled in~/.config/nvim/init.vimor similar. - This together with the X Server will allow you to share the clipboard with Windows even in
NORMALmode.
- 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 useIPv6by default. Use the following system property to force it to useIPv4instead: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.exewill find the Windows explorer program and open it. The same goes forcodewhich 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 - WSLextension. Do it. This will install theserverpart in Linux and theclientpart in Windows. - Some of your Windows VS Code extensions need to be installed on the
serverwhich can easily be done from the VS Code extensions page.