on
Linux GUI application on windows: WSL2 and VcXsrv configuration
It is now quite easy to use X application from WSL2 linux distribution. Let’s review how.
Prerequisites
- You are using chocolatey
- You already have configured your WSL2 image and have a GUI program available like
xeyes
Step by step
On WSL2 Linux side:
- Add this to
/etc/profile.d/set-display.sh
1 - then source it:
export HOST_IP=$(cat /etc/resolv.conf | grep nameserver | awk '{print $2}')
export DISPLAY=$HOST_IP:0
The WSL2 virtual machine has its own network stack, it can retrieve its host IP through the
/etc/resolv.conf
file.
On windows side:
- Install VcXsrv
choco install vcxsrv -y
- Auto start VcXsrv by
- win + r then type
shell:startup
- Right click >
new
>shortcut
- Enter shortcut location to
"C:\Program Files\VcXsrv\vcxsrv.exe" -ac -multiwindow
- win + r then type
-ac
option means the X server will accept any client connection. It should be okay for a home desktop. Be careful of these kind of options on a laptop.
We will have to configure firewall rules, otherwise WSL2 linux would not be able to communicate with VcXsrv
- Double click on the shortcut you just created.
- When for firewall configuration about VcXsrv, check second option ensuring it can communicate on public networks.
Conclusion
You should now be able to launch a graphical application from WSL2 and see its graphical window appear. Your configuration should also be persisted across reboots
To test it, in linux terminal:
xeyes
should open a GUI application with two eyes following your mouse cursor.
Troubleshooting
If you have trouble at this point, check no blocking rule exist for VcXsrv windows xserver
in your firewall configuration:
- win + r. then type
wf.msc
- Click on inbound rule
- Delete each blocking rule for
VcXsrv windows xserver
Resources
-
Adding it as a profile script enable loading it during loging. You can also add the snippet in your
.bashrc
which will enable DISPLAY on interactive shell only. ↩︎
Knowing so, if you think you found an error or inexact content, you are more than welcome to notify it through comment below ⏬.
Also, if you found the content useful and it helped you, consider leaving a comment too or, better, give me fuel buying me a coffee with the link on the top of the website. 🙏