Thursday, January 5, 2023

WSL2 in minutes

The Windows Subsystem for Linux (WSL to those in the know) is a great thing, allowing emulation of Linux on Windows machines without the need for dual booting or any of that nonsense. I got excited about it years ago, and I am still excited about it now. My years of buying Apple machines so that I could have a command-line enabled machine that could also run desktop software are over. Hurrah!

Anyway, I just installed it again on a new machine running Windows 11, so I thought I'd document how.

1. Run the install command

This is remarkably easy these days (compared to, say, the olden days). I googled for instructions and found this link: https://learn.microsoft.com/en-us/windows/wsl/install 

It boils down to this. Open a PowerShell window as Administrator, and run:

wsl --install

And it will download things and do it all for you. By default it installs the latest Ubuntu LTS version, which is completely fine with me! 

Once installed, you should reboot your machine. But while you're doing that, you might as well...

2. (Optional, but you'll probably need to) Activate the virtualization in BIOS 

My new PC, although built from reasonably up-to-date components (AMD Ryzen 7 processor, 5xxx series, B550 motherboard), did not have virtualization enabled in BIOS by default, and so when I tried to run my new Ubuntu instance, it quit on my immediately, saying:

Please enable the Virtual Machine Platform Windows feature and ensure virtualization is enabled in the BIOS

Since the current version of WSL (WSL2) makes use of virtualization at the hardware level, this was not entirely surprising. Unless you have been mucking about with this sort of thing already, it is quire likely you need to turn on virtualization in the BIOS.

So I rebooted again, went into my BIOS and after a little googling, found instructions for my ASUS motherboard. (I had to enable 'SVM mode' under 'CPU configuration'.) 

After that, the Ubuntu app opened, and asked me to generate a username and password. Success!

3. Set up X-Windows

Assuming you want to do anything serious with your WSL instance, you will probably want to install some kind of X-Windows server. Over the years I have had good luck with VcXsrv, and it is still my go-to. So go ahead and download it, and install it.

When you run VcXsrv (a.k.a. 'XLaunch'), you will get some options in a pop-up window. I make sure to select:

  • 'Multiple windows', on the first page
  • 'Start no client', on the second page
  • Check the box for 'Disable access control' on the third page 
  • Click the button to save your configuration, and save it to your desktop. That way next time you can use the configuration file icon to open VcXsrv with a working configuration from the get-go.

4. Configure your display

I usually have to look this up. I found a useful blog post that had all the information I needed... The key is to configure the DISPLAY environment variable in your terminal properly. This boils down to adding a line to the end of your .bashrc file (you can edit it using nano).

The line you need to add is:
export DISPLAY="`grep nameserver /etc/resolv.conf | sed 's/nameserver //'`:0"

Once you have added it, close your terminal and open it again. 

5. Testing!

You can use the aptitude package installer to add some useful things to your Ubuntu install. One I like to use as a test that X-Windows is working is x11-apps, which is easily installed:

sudo apt install x11-apps

and once installed, run

xeyes

 and you should see a pair of googly eyes on your screen! If so, it's working! 

No comments:

Post a Comment