Setting up an X11 Server on WSL2
Published:
The following is a set of instructions for setting up an X11 server for the Windows Subsystem for Linux (WSL2).
Download and install VcXsrv on your Windows machine.
Create a file called config.xlaunch
in notepad containing the following lines
<?xml version="1.0" encoding="UTF-8"?>
<XLaunch WindowMode="MultiWindow" ClientMode="NoClient" LocalClient="False" Display="0" LocalProgram="xcalc" RemoteProgram="xterm" RemotePassword="" PrivateKey="" RemoteHost="" RemoteUser="" XDMCPHost="" XDMCPBroadcast="False" XDMCPIndirect="False" Clipboard="True" ClipboardPrimary="False" ExtraParams="" Wgl="True" DisableAC="True" XDMCPTerminate="False"/>
and then save this file to
%AppData%\Microsoft\Windows\Start Menu\Programs\Startup
Now open your Linux terminal and edit the file ~/.bashrc
with either nano
or vim
nano ~/.bashrc
and add the following lines
export DISPLAY=`grep -oP "(?<=nameserver ).+" /etc/resolv.conf`:0.0
export XDG_RUNTIME_DIR=/tmp/my-x-server
export RUNLEVEL=3
then save and exit (for nano
CTRL+O then CTRL+X).
Run these commands
mkdir /tmp/my-x-server
sudo chmod 700 /tmp/my-x-server
and finally run
source ~/.bashrc
Now your X11 server will start every time Windows boots, and every Linux shell you open will be automatically connected to the X11 server. There is no need to ever do any other configuration steps at any point.