Running graphical programs on WSL

This is a quick walkthrough on getting graphical programs working on a Linux distro running via Windows Subsystem for Linux. We’ll use Ubuntu for this, as it’s one of the most common distros and can be easily installed via the Microsoft Store.

Once you’re up and running with Ubuntu on WSL, it’s time to install the X server, which will handle displaying programs. X uses a server-client model, though usually the server and the client are the same machine. In this case, the X server will actually be installed and run on Windows itself, while Ubuntu on WSL takes the role of client. There are many options to choose from, but I prefer Cygwin/X as it’s free and easy to install. When installing via Cygwin’s setup program, make sure to select both the xinit and xlaunch packages. All the other necessary dependencies will be selected automatically.

After installation is finished, find and launch the Xlaunch shortcut in your Start menu (under the Cygwin-X folder). Click next twice to accept the defaults. On the third page, titled Extra Settings, enter the following into the additional parameters text box: -listen tcp. Normally, the Cygwin/X server only listens on Unix sockets, but that won’t work for WSL, so we have to tell the server to also listen for TCP connections. Click next again, and then finish. Optionally, save the configuration for later use. This option will create a config.xlaunch file, which can be double-clicked to immediately start the X server with the saved settings.

Back in WSL, run the following commands:

export DISPLAY=:0
export LIBGL_ALWAYS_INDIRECT=1

The first sets the X display to connect to, and the second enables the use of the native Windows OpenGL library. These variables can also be set in your shell initialization file (.bashrc, .zshrc, etc.) so they’ll be available any time you start Ubuntu on WSL.

Now the moment we’ve been waiting for: actually running a graphical program. There are obviously many to choose from, but for this quick test, we’ll go with something with few dependencies. In WSL, run sudo apt install xsol to install a simple X solitaire program. Then type xsol to run it. If all goes well, you’ll see the game window pop up.

Success!

Once you’re done experimenting, you can shut down the X server by right-clicking the icon in the Windows system tray and clicking Exit. Now you’re all set to run graphical Linux applications that can even take advantage of native Windows OpenGL acceleration.

Troubleshooting tip: if programs on WSL complain about not being able to connect to the display, make sure the X server is actually running and that the DISPLAY environment variable is set and matches the X display number, which is :0 by default. Also ensure that you started X with the -listen tcp option. Further information about Cygwin/X can be found in its user manual.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.