WSL2

11:30PMNovember 21 2020Daniel Tompkins

Archive

Mi­crosoft 💔 Linux

In 2001, Mi­crosoft's former CEO— Steve Ballmer— was quoted by the on­line tech news pub­li­ca­tion, The Reg­ister, saying:

Linux is a cancer that at­taches it­self in an in­tel­lec­tual prop­erty sense to every­thing it touches

Fast-for­ward 15 years into the fu­ture— at Mi­crosoft's de­vel­oper con­fer­ence, Build 2016— and Gates' tech be­he­moth re­veals a sudden volte-face. The cur­rent CEO, Satya Nadella, an­nounces Win­dows Sub­system for Linux. With WSL, Mi­crosoft is taking some of the most pop­ular Linux dis­tri­b­u­tions and making them avail­able within Win­dows through the Mi­crosoft Store.

Ac­cording to a W3Techs survey , Unix op­er­ating sys­tems (the under-pin­ning OS of Linux, as well as MacOS) make up 71% of the Web, the re­maining 29% being Win­dows. Ad­di­tion­ally, every An­droid phone, tablet and smart TV runs on a mod­i­fied ver­sion of the Linux kernel. So, I guess if you can't beat 'em, join 'em?

Bar graph with two data points, Unix (71%) and Windows (29%). These are the percentages of wabsites using these operating systems (based on the October 2020 survey.
Stats taken from W3Techs

Whether or not its be­cause of Mi­crosoft's good graces or some ul­te­rior mo­tive, I know having an easily ac­ces­sible Unix-type en­vi­ron­ment avail­able on Win­dows has been a god­send for me and for so many other de­vel­opers.

What's so great about WSL?

Be­fore WSL, de­vel­opers run­ning Win­dows had two op­tions:

  1. A vir­tual ma­chine (VM)
  2. Dual-booted op­er­ating sys­tems

Run­ning a vir­tual ma­chine uses up more re­sources than WSL. It can also be dif­fi­cult to in­te­grate hard­ware and files be­tween the host ma­chine and the VM. Dual-booting al­lows for a full-fledged in­stall on a sep­a­rate disk par­ti­tion; but it re­quires a restart any time you want to switch be­tween OS's.

Win­dows Sub­system on Linux doesn't in­te­grate with the host's hard­ware per­fectly— for ex­ample, NVIDIA is still working on CUDA dri­vers that will take ad­van­tage of GPU re­sources from within WSL. How­ever, for Linux de­vel­opers who are fre­quently run­ning CAD soft­ware or Adobe Suite (which are dif­fi­cult-to-im­pos­sible to in­stall on Linux), WSL can be a fan­tastic partner.

WSL1 vs WSL2

More re­cently, Mi­crosoft an­nounced WSL2— an up­date that al­lows for a more com­plete Linux kernel to run on a Win­dows ma­chine. This made it much easier to in­stall a va­riety of soft­ware that had been dif­fi­cult to run on the pre­vious, WSL1. WSL2 is very sim­ilar to run­ning a vir­tual ma­chine (in fact it uses Mi­crosoft's hy­perV vir­tual ma­chines).

How­ever, using WSL2 (as op­posed to in­stalling a Linux distro through Vir­tu­alBox, or an­other VM man­ager) pro­vides some minor per­for­mance ben­e­fits since Mi­crosoft has op­ti­mized it to in­te­grate with Win­dows' ser­vices. If you want, I rec­om­mend reading Mi­crosoft's own WSL1-vs-WSL2 fea­ture com­par­ison docs.

Al­right, so how do I in­stall WSL2?

Mi­crosoft has clean, straight-for­ward in­stal­la­tion doc­u­men­ta­tion for WSL and WSL2. You can refer to that tu­to­rial if you get stuck, or just follow the steps out­lined below. Be­fore starting, make sure you up­date your Win­dows 10 in­stal­la­tion with the most re­cent build.

I'll also be going one-step fur­ther, and showing you how to run a Linux GUI using WSL2 and VcXsrv (dis­play for­warding). If you're more of a vi­sual-learner, I've also in­cluded an in­stal­la­tion speedrun video that fol­lows the same steps out­lined below (edit: I forgot step 11 in the video, and it's a crit­ical one! Make sure you do that!).

1. En­able WSL Fea­ture

First you need to en­able the Win­dows Sub­system on Linux fea­ture by right-clicking on Pow­er­shell from the start menu and clicking "Run as Ad­min­is­trator".

Then, paste the fol­lowing com­mand and hit "Enter"— don't close the Pow­er­shell!

dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
powershell

2. En­able WSL2 Vir­tual Ma­chine Fea­ture

After the last com­mand is fin­ished, paste the fol­lowing com­mand in the same Ad­min­is­trator-level shell, and hit "Enter" to en­able the WSL2 VM. Again, keep this shell open.

dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart
powershell

3. Down­load and In­stall the WSL2 Linux Kernel Up­date

Click here to down­load the Mi­crosoft ex­e­cutable for in­stalling the WSL2 Linux Kernel up­date. Once it's fin­ished down­loading, double-click the ex­e­cutable and follow the in­stal­la­tion steps. This part's pretty straight­for­ward

4. Set WSL2 as De­fault Ver­sion

Copy and paste the fol­lowing com­mand in Pow­er­shell to set WSL2 to be the de­fault ver­sion:

wsl --set-default-version 2
powershell

5. In­stall Ubuntu 20.04 from the Mi­crosoft Store

Click the start menu and open the Mi­crosoft Store. Search for "Ubuntu 20.04" and in­stall this Linux distro. If you want to use an­other distro, that's fine; but Ubuntu 20.04 is com­pat­ible with the Re­golith Linux desktop GUI we'll be in­stalling in just a bit.

6. Ubuntu 20.04 Ini­tial Setup

Once Ubuntu is done in­stalling, click "Launch" to ini­tiate first-time in­stal­la­tion setup. You'll be prompted to put in a user­name and pass­word.

7. Make sure You're Using WSL2

At this point, it might be a good idea to double-check that WSL is using ver­sion 2 by de­fault. Open a com­mand prompt (or use Pow­er­shell if it's still open) to paste in the fol­lowing com­mand:

wsl --list --verbose
powershell

8. Down­load and In­stall VcXsrv

VcXsrv is an X Server that we'll use to view the GUI from WSL2.

There are a few other dis­play-for­warding servers avail­able (like Xming ), but I've found VcXsrv works the best. Down­load the ex­e­cutable here and click through the in­stal­la­tion steps.

9. In­stall Re­golith Desktop

I have Re­golith Desktop in­stalled on one of my PCs, and it's fan­tastic. It's pre­con­fig­ured to use the i3 window man­ager which I find in­cred­ibly ef­fi­cient for its tiling and hotkey fea­tures.

A loosed reader, Ro­drigo, asked specif­i­cally for a Re­golith tu­to­rial; but if you want to in­stall a dif­ferent GUI I'm sure its pos­sible! To in­stall Re­golith Desktop, open your fresh Ubuntu in­stall, and paste in the fol­lowing lines:

Directions are for Ubuntu 20.04

These di­rec­tions were tested and working on an Ubuntu 20.04 system. You might still get them to work on 22.x or 24.x, but mileage may vary.

They do use the aptitude package man­ager— which is spe­cific to De­bian-based Linux dis­tri­b­u­tions (such as Ubuntu). So, if you're on a dif­ferent system (like Arch or Alpine) your package man­ager might be dif­ferent and the com­mands or package names might be slightly dif­ferent too.

sudo add-apt-repository ppa:regolith-linux/release
shell
sudo apt install regolith-desktop i3xrocks-net-traffic i3xrocks-cpu-usage i3xrocks-time
shell

It's a lot of pack­ages, so it'll take some time.

10. Change the "Mod" Key

Re­golith, or rather i3-wm, uses the Super (Win­dows) key as the hotkey prefix by de­fault. Since you're run­ning this GUI within Win­dows, you'll run into a lot of overlap be­tween Win­dows' and i3-wm's pre­con­fig­ured short­cuts.

For this reason, I rec­om­mend swap­ping the Super key for the Alt key. To change the Mod key map­ping use the Vim or Nano text ed­i­tors to open the con­fig­u­ra­tion file lo­cated at:

vim /etc/regolith/i3/config
shell

On lines 42 and 43, you should find the Mod key as­sign­ment. Switch "Mod1" and "Mod4" and you'll be good to go! Your edited lines should look like this:

set_from_resource $mod i3-wm.mod Mod1
text
set_from_resource $alt i3-wm.alt Mod4
text

If you're using Vim, hit "Es­cape" and type ":wq", then hit "Enter" to write and quit the file. You can check out the of­fi­cial Re­golith tu­to­rial on making these changes if you get stuck.

11. Ex­port DIS­PLAY pa­ra­meter

An­other crit­ical edit (that I forgot to put in the video— oops 🙃) is to ex­port the DIS­PLAY vari­able. Since WSL2 is a VM, it has it's own IP ad­dress (which can change at each startup). As a re­sult, you'll need to add a couple lines to your bash pro­file for VcXsrv to con­nect to WSL2.

To open your ".bashrc" with Vim:

vim ~/.bashrc
shell

Press and hold Shift then press "G" to jump to the bottom of the file. On two new lines, paste in the fol­lowing code:

export DISPLAY=$(awk '/nameserver / {print $2; exit}' /etc/resolv.conf 2>/dev/null):0 export LIBGL_ALWAYS_INDIRECT=1
bash

12. Open and Con­figure VcXsrv

Click the start menu and type in "Xlaunch" then hit "Enter" to run VcXsrv. Click the "One window without ti­tlebar" op­tion (you can ex­plore the others later, if you want) and click next. Leave it on "Start no client" and click next. Then, in the "Ad­di­tional pa­ra­me­ters" input, add "-ac" and click next. I rec­om­mend clicking "Save con­fig­u­ra­tion" for ease of use.

At this point, you should have a black screen waiting to ac­cept a dis­play input.

13. Run Re­golith Desktop

The last thing to do is run the magic line:

i3-gnome-flashback-session
shell

You should then see a graph­ical Re­golith Desktop ap­pear in the VcXsrv window! Huzzah! Feel free to play around with your new graph­ical WSL2 setup. To see an overview of the avail­able short­cuts, use "Alt+Shift+?" to bring up the help menu. You can find more help in Re­golith's of­fi­cial doc­u­men­ta­tion.

update: 11/21/2020

This tu­to­rial was made using Re­golith Desktop v1.4, but they've since up­dated to v1.5. Reddit user _way­war­done_ was having is­sues with the final i3-gnome-flashback-session com­mand.

If you're get­ting "com­mand not found", try run­ning the new init script pro­vided in v1.5:

regolith-session-init
shell

This new init will start the re­golith desktop and i3-wm just like the old script; but it doesn't seem to start the compton back­ground ser­vice au­to­mat­i­cally any­more. So, until I figure out a better so­lu­tion, run:

compton
shell

...just after Re­golith ap­pears in your VcXsrv window and i3 loads. You can thank _way­war­done_ for helping me catch the change!

Regolith Desktop with Windows 10 Taskbar and i3 tiling running on WSL2.
Regolith desktop environment with Windows 10 taskbar and the i3 tiling window manger running on WSL2.

Wrap­ping Up

Hope­fully you fin­ished this tu­to­rial with a working Re­golith Desktop GUI run­ning off WSL2. If you run into any issue, or have ques­tions, feel free to leave a com­ment. If you'd like to know how to for­ward audio from WSL2 (a fea­ture not yet avail­able by de­fault), let me know and I'll put to­gether an­other tu­to­rial.

Until next time, happy coding!