Set up WSL 2 and X11 in 4 minutes
For some reason, it was difficult for me to find a clear answer on how to set up a X11 server on Windows to use with WSL2 - this is my attempt to clarify the process.
Windows X11 server configuration
First, download and install VcXsrv. Create a new shortcut (or modify the default Start Menu one) and set the target to the following:
"C:\Program Files\VcXsrv\vcxsrv.exe" :0 -ac -terminate -lesspointer -multiwindow -clipboard -wgl -dpi auto
This will launch VcXsrv with the correct parameters for use with WSL2.
Run the shortcut. Nothing will appear, but you can verify it is running by inspecting the system tray. If any Windows Defender Firewall prompts appear, be sure to give VcXsrv network access.
Firewall configuration
Sometimes the Windows Defender Firewall popups incorrectly configure the firewall settings for VcXsrv. Before you continue, you should verify your firewall is configured correctly.
Open your Windows Defender Firewall settings, choose “Inbound Rules” in the the sidebar, then sort the list of entries by name. Scroll until you find VcXsrv. There may be multiple entries. Double click each entry and ensure “Allow the connection” is chosen as the action.
WSL2 configuration
If you haven’t already installed the x11-apps
package, go ahead and install
it. It includes a few graphical apps you can use to confirm your setup is
working. If you are using Ubuntu, you can run the following command:
sudo apt install x11-apps
Next, you will need to correctly set your DISPLAY
environment variable, which
can be done with the following command:
export DISPLAY=$(awk '/nameserver / {print $2; exit}' /etc/resolv.conf 2>/dev/null):0
You can now verify that your setup is working by running a graphical program
such as xeyes
. If everything is working fine, you might want to add the
DISPLAY
variable configuration to your shell profile.