Using GUI applications with WSL2

WSL2 has been a game-changer for Windows developers. If you want to learn more about the architectural changes in WSL2, I recommend this great presentation from MS Build 2019.

In this post, I’ll explain how you can use graphical applications inside WSL2. This was already possible on WSL1, but due to some architectural changes, the configuration is a little bit different on WSL2.

My main developing environments are the excellent Jetbrain’s IntelliJ and Rider IDEs. But unfortunately, as of this date, they still don’t support WSL remote development as Visual Studio Code does.

To circumvent this limitation, I’m using IntelliJ inside WSL2 through X. Initially I thought this wouldn’t give me a great developing experience, but I was mistaken. I’ve been using this setup without major hiccups for the last weeks.

If you need to do the same, follow these simple steps:

Install an X server on your Windows machine.

There are great free alternatives available, like VcXsrv. If you want better support for HiDPI screens, I recommend the commercial X410.

After the installation, you’ll probably need to configure your WSL instance’s IP on your X Server access control list. If you are using VcXsrc, this can be done using the xhost tool or editing the X0.hosts file in the VcXsrc installation’s directory.

Point your instance to the Windows X Server

Inside your WSL instance, add the following code snippet to your $HOME/.bashrc:

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

This will set the variable DISPLAY to point to your Windows host address. Unlike WSL1, WSL2 instances run inside a HyperV VM with a different IP from the host. The host’s address inside the VM network will be retrieved from the /etc/resolv.conf file.

LIBGL_ALWAYS_INDIRECT will force the client to send any OpenGL commands to the X server, making sure the rendering will occur on the Windows side.

Test your setup

If you are using an Ubuntu instance, install the x11-apps package:

# sudo apt install x11-apps

After the installation ends, you can check your configuration executing the xcalc or xeyes tools.

Back to top ↑