WSL2

Updated: In Code

The May 2020 Windows 10 update—that eventually made its way out late into June—brought with it an upgraded Windows Subsystem for Linux (WSL).

WSL 2 changes the underlying architecture of the system, introducing a full Linux kernel built by Microsoft. This brought a wide range of improvements including a better file system, faster boot time, and tighter integration between Linux and the host Windows environment.

With the upgraded version, WSL2 is now an adequate environment for most development. The previous version was quite slow for large JavaScript projects, especially for an npm install and other file intensive actions.

See Microsoft’s installation guide for how to install and update to WSL2.

Integration

One of the cooler integration is the ability to run Windows binaries from the Linux command-line. This includes launching apps, or even piping to a Windows program, for example running this in WSL2 will send the output to the clipboard.

echo "Hello" | clip.exe

The integration I use daily is WSL2 and Visual Studio Code.

From my project directory in Linux, I can run code . and it will open VSCode on Windows. Combined with the Visual Studio Code Remote extension allows for using the entire Linux toolchain — build, lint, git — in Windows.

You can also use VSCode’s built-in terminal and it will open a Linux terminal in the WSL environment. You barely know you’re using Windows.

Docker

There are several improvements to using Docker and WSL2. The new kernel allows Docker to run Linux containers natively without emulation, speeding up all aspects of your containers while using less resources.

See Docker setup guide on how to use WSL2 backend.

Networking

The one downside for the WSL2 is the networking. I’m no expert in virtualization on Windows, but from what I’ve read the way WSL2 is setup requires recreating the stack occasionally.

Practically what this means is the IP address for the WSL2 environment changes when it recreates a new stack, so isn’t every launch but could be if you haven’t launched in awhile and it cleans itself up. I usually leave it running, so it doesn’t occur too often.

Depending on what you are doing, and services you run in WSL2 this could be a minor annoyance. I run several different WordPress installs in WSL2 and use named virtual hosts to reference them, for example wptest.local, or blog.local.

So when they IP address changes, I need to edit the Windows hosts file and update with the new address.

Localhost

You can use localhost and different ports and refer to the WSL2 environment, there is some magic mapping happening that maps localhost to both Windows and Linux environments, however I’ve found this to be real problematic.

When using localhost and loading a site from Linux is slow and many of the requests, for example the site might load but the stylesheets get missed.

So I stick with named hosts and occasionally have to update the IP address. I wish there was a way to run as a administrator in WSL2 and edit the hosts file from there. It’d be nice to script up.

Setting up WordPress

Setting up WordPress on WSL2 is the same as before, see my guide for installing WordPress on WSL

Installing and setting up is really no different than installing on any Linux environment.

The minor difference is WSL2 does not use systemd, so starting Apache and MySQL use service commands like so:

sudo service apache2 start
sudo service mysql start

Setup Node on WSL2

Most of my development is in JavaScript, and WSL2 makes it quite easy to work with JavaScript projects that assume a Linux environment.

Installing and setting up Node.js is no different than any other regular Linux environment.

I prefer to use nvm to install and manage Node, it tends to be easier and allows a bit more control. The quick setup is:

Install nvm:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.3/install.sh | bash

Install Node.js LTS:

nvm install --lts

See Microsoft’s guide to set up Node.js on WSL 2 for more detailed instructions.

Newsboat

I use the Newsboat RSS reader, it is a command-line tool for reading RSS. It runs fine in WSL2 but you need to configure it to open a browser in Windows. Here’s how:

Create a local script, I put mine in $HOME/bin, that calls the Windows browser. Here’s the script I use for Chrome, I name it chrome.exe so I know that it is pointing to a Windows executable:

#!/bin/sh

# use Windows browser
/mnt/c/Program\ Files/Google/Chrome/Application/chrome.exe "$@" &

After creating mark it executable, and in your ~/.newsboat/config add the following browser configuration:

browser "~/bin/chrome.exe"

That’s it, now when you open a link in Newsboat on WSL2, it will open in the Windows browser.

Summary

In summary, WSL2 is a pretty good environment, making it possible to do normal Linux development from within Windows. The integrations work well, the performance is fine, if it wasn’t the networking annoyance it’d be almost perfect.

I still prefer a full GNOME environment and a true native Linux for development, but as I mentioned in my Embracing Windows 10 article, unfortunately not all software is available on Linux.

One Comment

Elijah Fadodun August 23, 2020 Reply

It’s really awesome

Leave a Reply