How to run graphical applications in Windows 10 subsystem for Linux (WSL2)

How to run graphical applications in Windows 10 subsystem for Linux (WSL2)

Preamble

In the web, there are plenty of guides and tutorials about how to install and run the subsystem for Linux in Windows 10 (WLS2), and also about running applications with a graphical interface (GUI). I am no expert in the field and, therefore, I have nothing to add to what can already be found via any common search engine.

On the other hand, reading and following some of the guides I found, I managed (with some issues) to do almost everything I needed, except running programs with graphical interface: I could not find a way to make things work.

Having the Linux bash available in Windows was anyway useful to me, I could do some things, for example quick operations on csv (comma-separated values) files containing experimental data. But, I still wanted the chance to run certain programs with graphical interface.

In particular, years ago I frequently used Gnumeric, which, at that time, was more convenient to me compared to Excel (at least until I managed to handle the fundamentals of VBA, and anyway before the 2013 version). Unfortunately, the release of versions compiled for Windows has been abandoned, so I was stuck with lots of files that I could not easily handle anymore.

Eventually, I did manage to run graphical applications on WSL2, so I wrote down step-by-step the sequence of moves that allowed me reaching this goal, essentially as a reminder to summarize everything I read around and the numerous trials I made.

1) Installing WSL2

It is not really difficult, there are many available guides. For example, at this link you can find one in the official Microsoft website. Steps are rather clear, firstly you have to enable two additional features, namely Virtual Machine Platform (not Hyper-V) and Windows Subsystem for Linux (Fig. 1). To do this, you could probable type “features” in the search bar, then “Turn Windows features on or off” (this is translated from Italian, I cannot double check in an English OS), or type directly “optionalfeatures” and run the command that will appear.

WSL2 Funzionalità aggiuntive per il sottosistema Windows per Linux | Additional features for the Windows subsystem for Linux
Figure 1. Enabling the additional features for the Windows subsystem for Linux

Once it is done, you have to reboot PC (even more times) according to directions, then you have to go to Microsoft Store and look for a Linux distribution (e.g. Ubuntu, Fig. 2), then install it. Once completed, you can run it from Windows menu, thus opening a Linux terminal in a new window.

WSL2 - Ricerca di Ubuntu in Windows Store | Search Ubuntu in Windows Store
Figure 2. Searching Ubuntu in Windows Store

2) Installing, configuring and running an X server in Windows

I personally tried Xming and VcXsrv, keeping the latter, but the procedure is practically the same. Once the installation file has been downloaded and installed, you have to run XLaunch (found among the apps, or by typing it in the search bar, see Fig. 3). Then, I left the default setting in the first window (“Select display setting“), i.e. “Multiple windows” with display number –1, and in the second window (“Select how to start clients“), i.e. “Start no client“. At the third windows (“Extra settings“) I put a tick on all three options, i.e. “Clipboard“, “Native openGL” e “Disable access control“. The deactivation of the access control can be alternatively imposed by typing “-ac” inside the cell of additional parameters. In the next and last windows you can save the configuration to a file, if desired, or just click “Finish” to run the X server. The sequence is shown in Figures 4a–4d.

WSL2 - Configurazione dell'Xserver via XLaunch 1 | X server configuration via XLaunch 1
Figure 3. Running XLaunch
WSL2 - Configurazione dell'Xserver via XLaunch 2 | X server configuration via XLaunch 2
Figure 4a. First configuration window of the X server (default settings)
WSL2 - Configurazione dell'Xserver via XLaunch 3 | X server configuration via XLaunch 3
Figure 4b. Second configuration window of the X server (default settings)
WSL2 - Configurazione dell'Xserver via XLaunch 4 | X server configuration via XLaunch 4
Figure 4c. Third configuration window of the X server: tick all the options
WSL2 - Configurazione dell'Xserver via XLaunch 5 | X server configuration via XLaunch 5
Figure 4d. Last configuration window of the X server: save to file, if desired, or click directly “Finish” to turn on the server

3) Checking that firewall is not blocking the app

To get things working, you have to pay attention that, at the first run of the X server, Windows Firewall asks whether to block or allow the application. However, I think that the permission is applied by default only to the profile related to private networks, being thus the application blocked in public network profiles. Conversely, it is necessary to revise settings and be sure that, among the rules for incoming connections, VcXsrv be allowed also for the public profile, especially the TCP protocol (Fig. 5).

WSL2 - Controllo dei permessi dei Firewall | Check of Firewall permissions
Figure 5. Check Firewall permissions: allow VcXsrv also for the public profile

4) Retrieving the IP address assigned to WSL

This can be achieved in different manners, for example using PowerShell or, as in Figure 6, via Windows command prompt, where typing “ipconfig” and taking note of the IPv4 address related to the adapter that contains “WSL” in its description.

WSL2 - Recupero dell'indirizzo IP relativo a WSL | Retrieval of the IP address related to WSL
Figure 6. Retrieving the IP address related to WSL

5) Running Linux and exporting correctly the parameters

Now, from the Linux terminal (Ubuntu in my case) you have to export the parameter LIBGL_ALWAYS_INDIRECT (only if the option “Native opengl” was checked when configuring the X server, see Fig. 4, otherwise skip it) and the parameter DISPLAY, which has to correspond to the IP address identified at the previous step. To do this, you just need to type the following instructions, pressing “enter” at the end of each one. Obviously, “NNN.NNN.NNN.NNN” has to be replaced with the correct IP found for your machine (see the example in Fig. 7). Pay attention to upper and lower case letters, because the Linux terminal is case-sensitive, differently from the Windows one.

export LIBGL_ALWAYS_INDIRECT=1 [ENTER]
export DISPLAY=NNN.NNN.NNN.NNN:0 [ENTER]
WSL2 - esportazione dei parametri per il server X | export of parameters for the X server
Figure 7. Exporting parameters for the X server

6) Checking the settings

If no issue arose, you can test if everything is working simply running any application with graphical interface that you previously installed. For instance, I tried to run the file manager formerly known as Nautilus (Fig. 8).

WSL2 - Lancio di una applicazione grafica (Nautilus) | Launch of a graphical application (Nautilus)
Figure 8. Launching a graphical application (Nautilus)

7) Running multiple windows

When you run a program with graphical interface, this will open a window but will keep the terminal occupied. To get different programs on multiple windows, a possible solution consists in running programs in background, just adding a space and an ampersand (&) at the end of the instruction line used to launch them.

8) References

Below I collected some links to guides and web pages that I read, among others, to understand how to deal with all this.

First release: 11 December 2020 | Last update: 3 April 2021

Leave a Reply

Your email address will not be published. Required fields are marked *