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

Step by step

On WSL2 Linux side:

export HOST_IP=$(cat /etc/resolv.conf | grep nameserver | awk '{print $2}')
export DISPLAY=$HOST_IP:0
Code Snippet 1: Command to retrieve host IP and set X display.

The WSL2 virtual machine has its own network stack, it can retrieve its host IP through the /etc/resolv.conf file.

On windows side:

choco install vcxsrv -y
Code Snippet 2: Chocolatey command to install VcXsrv

-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

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
Code Snippet 3: An X application for your test.

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:

Resources


  1. 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. ↩︎



The content of this post reflects my opinion, current state of knowledge and error can slip through its content.
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. 🙏