Using WSL 2 to run Linux GUI applications in Windows 10 with a shortcut
Install WSL 2.
What is a Linux Desktop Environment?
If you use Linux without a Desktop Environment, you just have a terminal to communicate with Linux kernel which is good when you work with servers but when you are developing software you almost always need IDE, web browser, and several other tools and utilities, because of these GUI applications you need to install a Desktop Environment like Gnome, KDE and … which provide a window manager (and some other important components like Windowing system, Display server and …) to show the GUI and manage interaction with users.
What is WSL 2?
By WSL 2 (Windows Subsystem for Linux), You have a real Linux kernel inside a lightweight utility virtual machine (VM) that completely integrated with windows and you can install your favorite Linux distribution on top of it and run Linux binary on Windows.
Running Linux GUI applications using WSL 2
But WSL 2 does not support running the GUI application now, because the GUI application in Linux at least needs a Windowing system and a Display server to show the GUI and manage interaction with users.
For now, we have two choices:
Installing a desktop environment (e.g. Gnome or KDE) on WSL 2 distribution (e.g Ubuntu-20.04) and connect to it via RDP
Installing an X Server ( a Display server like VcXsrv, xming or cygwin’s xwin) on Windows 10 and introduce it as a Display server to WSL 2:
Running WSL GUI Apps on Windows 10
Creating a shortcut for Linux GUI applications (Firefox, Gnome System Monitor, Intellij Idea, gedit, and …) and run them from the Start menu
Until now you can run Linux GUI applications from WSL 2 terminal by installing a Display server (VcXsrv) on windows 10 and introduce it to WSL 2 distribution (like Ubuntu-20.04).
What do we have to do if we don’t want to run Linux GUI applications from the command line?
You can pass Linux commands directly to the wsl.exe
command (from Powershell or CMD) without going to Linux terminal:
wsl ls -al
We can use this feature to run a Linux GUI program with a shortcut from the Start menu. We just need to set the DISPLAY
environment variable with the IP address of the host machine (which runs X Server) and then runs the GUI application like firefox or IntelliJ Idea:
- create a script file and call it
wsl-app-runner.bat
(I usually create all these script files in the current user home directoryC:\Users\YOUR_USERNAME\programs\linux-apps
)
@echo off
for /f "tokens=3 delims=: " %%I in ('netsh interface IPv4 show addresses "vEthernet (WSL)" ^| findstr /C:"IP Address"') do set ip==%%I
set ipAddress=%ip:~1%
Powershell.exe wsl "DISPLAY='%ipAddress%':0" %1
This script finds the WSL host IP address (which runs X server on it) and then set it in the DISPLAY
environment variable for the wsl
command and also get the name of the Linux GUI application as a first argument. We are going to reuse this script to create several shortcuts.
- create a Visual Basic script file (.vbs) for your Linux GUI application with this content (
firefox.vbs
). This file has to create beside thewsl-app-runner.bat
file and also you had to install the application (e.g. firefox) on the WSL 2 distribution (in my case Ubuntu-20.04).
Set oShell = CreateObject ("Wscript.Shell")
Dim strArgs
strArgs = "cmd /c wsl-runner-app.bat firefox"
oShell.Run strArgs, 0, false
This Visual Basic script file is executable and also allows us to hide the command line. In this script, we use wsl-app-runner.bat
to run our favorite Linux GUI application.
3- Right-click on the Visual Basic script file in the windows file explorer and select the create shortcut
menu then rename this shortcut to everything you want (for example Firefox Ubuntu-20.04
). If you want you can select an icon for this shortcut in the file properties.
4- Finally copy this shortcut into the start menu shortcuts directory (C:\Users\YOUR_USERNAME\AppData\Roaming\Microsoft\Windows\Start Menu\Programs
). I usually create a new folder there (for example Linux Apps
) and paste WSL 2 shortcuts into it.
Now you can search this application in the Start menu and open it by just one click.
If you just repeat steps 2 to 4 for any new Linux GUI application that you have installed on your WSL 2 distribution, you can run it easily. As you can see in the following photo I run gnome-system-monitor
application and you can see firefox memory usage in it.
Running gnome-system-monitor using WSL 2
Conclusion
If you a going to use Linux GUI applications like Firefox or IntelliJ Idea (which you have installed on your WSL 2 distribution) every day this approach allows you to integrate those applications with Windows 10 Desktop Environment and you can easily run and use them.