Running Linux on Windows with WSL 2 and a Native Kernel

by
Tags: , ,
Category:

This blog post is a preview of life with the Windows 10 2019 Fall Update. For thrill seekers now, or in the future, you’ll be able to launch your favorite Linux distro, even with a custom built Linux Kernel, right from Windows, in a matter of a few seconds using the Windows Subsystem for Linux, Version 2.0.

I love Linux/FreeBSD. My love for Unix-like OSes got me down the Mac train for many years (I’m still a devoted fan of the OS, much less of their terrible design flaws in current laptops where keys fail to function and nobody at Infinite Loop apparently ever used an ESCAPE KEY).

I also love the hardware of my Surface Book 2 bought in a huff after using and giving back a (thank you, Chariot I love you) 13” Macbook 2018 edition with touch bar. I hate, hate, hate the touch bar. I need a good keyboard and gosh darn it, an escape key. YUCK. The Surface Book 2 has a stylus, touch, and a great keyboard. But it runs Windows.

Native Ubuntu on a Surface Book 2?

Cue me wasting my free time trying to get Ubuntu working natively on Windows. On that score, yes, you can do it, but “it hurts us, baggins…” I have a 100GB partition on my Surface with Ubuntu 19.x to prove it, and if you go that way, please use the amazing Kernel provided by Jake Day, and donate a few dollars his way to support his tireless work on this kernel. It’s really fantastic, and the two things that mostly stink about it are the difficulty with suspend/resume and the camera support. But things like native NVIDIA driver support and raw speed just make it an incredible deal to work with.

Most days I’m onsite at a client doing actual development work, I run the native platform.

It’s great, but then sometimes I need an app like Photoshop. Or iTunes. Or OneNote and the stylus. (Don’t get me started on how horrible Linux is with tablets, and yes I’ve tried the Xournal app and it is not very good, certainly at organizing or searching for notes, which it basically doesn’t provide.).

WSL to the Rescue, Sort Of

For light development and code review of apps while still staying in Windows, I had been running Ubuntu on a feature of Windows 10 called the Windows Subsystem for Linux (WSL), version 1.x, until recently. It’s serviceable, even runs X11 desktops if you hack it just right, but it’s very slow. It’s best used with the files living on the Windows file system so you can modify them from inside and outside of the app. Plus you should never store your files in the Linux side on WSL 1.x and edit them there from Windows, since they are corrupted easily by external access.

The WSL 1.x apps ran in a virualized Linux kernel, and the file system was something like 20x slower than regular file access. So most of the time I ran in native Ubuntu for my day job and flipped over to Windows to read something on my Kindle, take notes, etc. A bit of a mess. Others would say, “Ken, just use a Mac!”

WSL 2 via the Windows Insider Fast Ring FTW

WSL 2.0 was just released to the Windows Insider Program’s Fast Ring (not for the feint of heart) back at the end of June 2019. It provides a more native Linux experience for windows developers than WSL 1, with the following major upgrades:

  • A fully native Linux Kernel running at full speed on your Windows machine
  • The ability to launch software like Docker for Ubuntu as if you were natively developing on Ubuntu
  • Faster apps in X11 for tools like X410, if you want to use a Linux desktop like Lubuntu
  • Storing and managing your files in the Linux instance rather than hosting them on the Windows side (for performance)

Some things still aren’t ready yet, like:

  • Access to USB and other devices from the Linux side
  • Support for audio (though I’m trying to get it working with Pulse Audio for Windows, which did work back in WSL 1)

In fairness, these aren’t really urgent problems, they are nice-to-haves. The ability to natively edit files under Linux, launch Docker for Ubuntu, even run Linux development tools is so much better than running Docker for Windows and getting bogged down in the Windows development universe. The speed overall is just about what it is in native Linux for me for the most part.

Also, for some odd reason, all of the Linux instances share some sort of Virtual Machine, maybe for network access, I’m unsure why and will post a link once I find out.

The Basics

Warning: Assume you will have to use an “experiments” machine while running WSL 2 in the Fast Ring. I’ve been burned before by upgrades that trash your operating system. For example, that time when I had a talk to give at a conference and somehow the Windows build started crashing on a graphics card related issue. On a Surface Book 2. Let that sink in: in late 2018, Microsoft blew up the Graphics Card driver for GTX-1060 on its own laptop line while building the next generation of Windows. So your mileage may vary.

Do you…want it to hurt me, Kevin?

To set up WSL 2, first you have to join the Fast Ring of Windows updates, which delivers a version of Windows compatible with this feature. Figure you’re out 2 hours to get that upgrade established. (Still realize you can’t easily go back without wiping your machine? Ok, go for it). Once that is installed, you search for “Turn Windows Features On or Off” on your Windows search, and install WSL 2. This will require you to install the Hyper-V Platform and tooling and reboot first.

Once Hyper-V is installed, you’ll want to tell your OS to use version 2.0 of WSL by default. Use Start -> Run -> CMD and then right-click instead of launching it, and run it with Administrative privileges. In that CMD dialog, type the following:

wsl --set-default-version 2

This will switch WSL to the version 2 file system and kernel features by default.

Next, install a Linux instance from the Windows Store (yes, you read that right, from the Windows Store). My go-to is Ubuntu (the current version at the time of this post was 18.04 LTS). Install it and then choose it from the Windows Start Menu. It will boot up a shell and ask you for your new user name and password.

Integrating with X11

Now I have a full Linux desktop launchable by clicking an icon in my Windows Start menu or Launch Bar:

I don’t have a great set of instructions for getting an X11 desktop up, since it was trial and error, but here are the basics:

Install an X11 Client (i.e. window server) on Windows

For options, try X410.exe or Xserv. You will probably have to pay a few bucks but it will be worth it to access your files from an IDE like IntelliJ or tools like Sublime and the Thunar finder rather than only the Terminal.

Configure your Display

You’ll have to set your Linux DISPLAY environment variable to point to your desktop instance. Luckily there is a quick way to do that in WSL 2. I use this in my favorite shell (Oh My ZSH) and it should work in .bashrc…

export DISPLAY=`echo $(cat /etc/resolv.conf | grep 'nameserver' | awk '{print $2}')`:0

This may change in an upcoming WSL 2.0 update. Microsoft recently enabled hitting localhost from Windows to browse Linux-run servers, but not the other way around, at least so far. For Linux to see Windows clients (as the X11 Client is a client hitting Linux) you need to access the TCP/IP NameServer (the machine that runs the Ubuntu / linux instance) – which happens to be an IP Address shared by the server and visible to the X11 client.

Install X11 Stuff

Here is where the fun begins. You can install Lubuntu, KDE, some say even Gnome 3 desktops. Use your Linux distribution’s package manager (`apt install lubuntu` on Ubuntu) as Root (so, `sudo bash`) But keep it simple. LXDE or Lubuntu are your best bets. If you install Lubuntu you should be able to launch your X11 server, (you may have to make it publicly accessible for the WSL Linux instance to see it), then in the linux shell try typing xeyes and see if the eyes show up on the X11 environment.

My Startup Script

This startup script is triggered by a VBS launch. It kills my existing X410 server and re-launches it. Then it runs an Ubuntu session that starts up the Lubuntu LXDE environment:

@echo off
start /B x410.exe /desktop
rem run ubuntu and xfce
ubuntu.exe run "xfce4-session lubuntu lubuntu"

I’m working now with trying to get Pulse Audio to fire up again, though I can just watch videos on the Windows side.

Here is the VBS script to launch it:

WScript.CreateObject("Shell.Application").ShellExecute "c:\Users\kenri\run-ubuntu.cmd", 
  "", "", "open", 0

I dragged that to the toolbar, then had to fix up the settings on the item to:

  • Target: C:\Windows\System32\wscript.exe c:\Users\myusername\run-ubuntu.vbs
  • Start in: c:\Users\myusername
  • Run: minimized

Oh, and please disable the screensaver in Ubuntu, especially when using Lubuntu. Otherwise your screen will go black on resume. It’s a strange bug, but you don’t really need X11 to screensave…

Tools I’m Using

I installed my favorite development tools, all running happily in either native WSL Ubuntu, including:

Speeding up your Linux Instance

Don’t try to run Unity or any 3D compositor. That’s what Native Linux is best at. Accelerated graphics are not something you’ll want to run here.

Games? Forget it.

One more thing: you should disable AntiVirus checking of the filesystem of the Linux instance (it’s not going to find the problems anyway and slows down writes). For mine, it’s Ubuntu so it lives in a subdirectory of my home directory AppData\Local\Packages\something CanonicalGroupLtd something Ubuntu something (see how specific I can be?). Just add an exclusion to that folder so it doesn’t check files it doesn’t understand.

Resources (an ever-expanding list will be placed here