It is now quite easy to use X application from WSL2 linux distribution.

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.sh1 - 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 startup VcXsrv by
    • Win+R】then type shell:startup
    • Right click > new > shortcut
    • Enter shortcut location to "C:\Program Files\VcXsrv\vcxsrv.exe" -ac -multiwindow

-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


  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.