Running GUI apps with WSL

Tags: WSL

Running Linux on Windows

WSL is a pretty neat piece of technology; you can run a full Linux OS inside of your current Windows session. It’s one of my favorite things that Microsoft has made in recent history. WSL definitely beats out trying to run a VM inside your current session, but there’s one major downside. It’s not really possible to run GUI applications from WSL, unless you set up a bunch of other stuff.

I’m writing this post from Emacs from a GUI app running from WSL, so I figure it’s a good time to show how I got it working. Of course, if you’re not running Windows, this might be more of a novelty - honestly if you’re already working in a *Nix system, then you’re in a pretty good spot.

Getting Set Up

I’ll assume you’re running Windows 10 with a stable release that came out around the time of this article. I hope that we can look back on this article and remember when we had to do stuff like this with some nostalgia.

Microsoft’s own documentation on getting WSL is solid, so check it out here. I’ve had to go both ways for getting WSL installed, both the Windows Store and getting a package. Both work just as well, but many corporate firewalls block you from getting to the Windows Store.

Next, pick your Linux distro of choice. I tend to go with Ubuntu because I’m not as hardcore of a Linux guy. If you love Debian, Arch, etc., you know my email.

Anyway, if you’ve got Linux running in a terminal of your choosing (I use ConEmu so I can get real cool screenshots), then you’re ready to go. I wanted to try out some specific GUI programs (emacs!) so I had to update most of my packages, but you shouldn’t have to with a fresh install.

The last thing you need is an X Server for WSL to connect to on your Windows machine. This is an app that acts as an interface for running graphical programs via the Windows system. WSL 2 is supposed to be getting something that does this natively in Windows soon (I think fall/winter?) but to try it out now you’d need to be a part of the Insider Program and run bleeding edge Windows, whatever that means. Instead, you can just install VcXsrv.

This Handy freeware program is a no-hassle X Server. I just install using the defaults and it’ll work for me.

Setting Up WSL to Connect

You have WSL, you have an XServer now you can just run GUI programs right? Well, probably not. WSL needs to know where to connect to the XServer, and on WSL 2 that means a networked interface.

To WSL and Windows the OS is running in a networked location. To connect to the XServer you’re running in Windows you need to know the IP for the host computer. This isn’t too bad, because WSL has access to run applications from Windows that can tell it where it is.

In Powershell you can run route.exe print to see all the netowrk info for your computer. WSL can also run this program (wild right?) and use the magic of bash to get the correct network location. If you’re running a desktop distro of Linux, chances are it’s already set up by default on install, but this is basically how it works in regular Linux installs too.

I didn’t figure this out on my own: I used the code snippets from this GitHubissue. Thanks a bunch!

For completeness' sake I’ll show the .bashrc update needed to get this to work:

# .bashrc
export DISPLAY=$(route.exe print | grep | head -1 | awk '{print $4}'):0.0

I tacked this onto my .bashrc file, then exited out of WSL, shutdown the instance to be sure that the changes take place.

wsl --list --running
wsl --terminate Ubuntu

Relaunch, and I’m good to go.

As long as your XServer is up on Windows, you should be able to run this eye testing program: xeyes. Now you can run just about any Linux GUI application from WSL in Windows!

Side-note upgrading Ubuntu on WSL

I had to upgrade my Ubuntu which was it’s own issue. I had Ubuntu 16 on my WSL, which was 2 Stable releases behind! Upgrading is usually painless, but for some reason the snap package manager was basically stopping me from upgrading.

On my desktop (which didn’t have any important stuff on it) I gave up, uninstalled Ubuntu 18 and installed the newer LTS version. Easy, just delete everything. On my laptop, I didn’t really want to aggressively greenfield everything, so I had to actually fix it.

Fist do all the things you usually do: check if you have a release in the first place:

lsb_release -a

You should see a the OS version you’re running, and if it’s not the current LTS then you can try the upgrade process. If it’s on the current LTS, and you want to upgrade to a non-LTS version by messing with the /etc/update-manager/release-upgrades file. It’s got some pretty helpful comments.

What should work to upgrade the OS version is the following:

sudo apt update
sudo apt list --upgradable
sudo apt upgrade
sudo apt --purge autoremove

sudo do-release-upgrade

But unfortunately I was not so lucky. I spent a good day or two on it, and forced myself to look at the logs. Essentially the issue was a few problems with the snap package manager. I don’t need snap to live in Linux, so I just Thanos snapped it.

sudo apt-remove snap

And the reslease upgrade worked like a charm!

Wow, Linux on Windows

I’m trying out Emacs from this set up, and it’s pretty neat. I’m thrilled that you can actually do this.

There’s a few other Linux apps I’m looking to try, but the big one was Emacs. You might know I’m a Vim fanboy, but I decided it was time to try out the other guys. Derek Taylor from DistroTube was the same way and his walkthroughs on Doom Emacs convinced me to take the plunge. This playlist has all the reference material from his channel, and it’s been really helpful. I’m not sure I’ll come out an Emacs evangelist but it did get me to try this project out.

Short of just running a Linux distro as my daily driver I think this is the best solution to getting the best and most powerful (and open) software on my machines.