How to Run GUI Application in Windows Subsystem for Linux (WSL)
Ping Zhou, 2019-08-10
Install WSL
Basically you need to turn on WSL in Program Features. Refer to this document for the steps: https://docs.microsoft.com/en-us/windows/wsl/install-win10
Install Linux distribution in WSL
Refer to same document for steps. I installed Ubuntu but there are other distributions available. https://docs.microsoft.com/en-us/windows/wsl/install-win10
Install Dependencies
GUI applications may have additional dependencies (libraries, etc). An easy way to install proper libraries is by installing some GUI programs, which will automatically resolve and install needed libraries. For example:
sudo apt install x11-apps
Configure X-Window Server
There are several choices available. I used the X-Win server from Cygwin because I already have one installed.
However, the default X-Win configuration doesn’t work for WSL. After some researching I found the reason is that it doesn’t listen on TCP port. So the solution is to add “–listen tcp” option to the X-Win shortcut:
C:\cygwin64\bin\run.exe --quote /usr/bin/bash.exe -l -c "cd; exec /usr/bin/startxwin -- -listen tcp"
Restart your X-Win server, and set DISPLAY environment in WSL:
export DISPLAY=:0
Try running some GUI application from WSL. For example, “xeye”, or launching Emacs with GUI.
If you see authorization error like this, it means need to configure xauth for WSL. See following section for steps.
Authorization required, but no authorization protocol specified Error: Can't open display: :0
Configure xauth
There are two options to solve this problem. First one is to copy the “.Xauthority” file from Cygwin to your WSL home folder.
Source file:
C:\cygwin64\home\<CYGWIN_USER>
Destination file:
C:\Users\<WINDOWS_USER>\AppData\Local\Packages\CanonicalGroupLimited.UbuntuonWindows_79rhkp1fndgsc\LocalState\rootfs\home\<WSL_USER>
Not finished yet! There is an extra step needed:
You need to set correct owner/permssion on the “.Xauthority” file that was copied to your WSL home folder.
sudo chown <WSL_USER> .Xauthority chmod +rwx .Xauthority
The other (maybe easier) option is to disable xauth in your X-Win server startup script. Edit “startxwin” file in your Cygwin/bin folder, find the line with “enablexauth” and turn it off.
Done! Now you should be able to run GUI applications in WSL. For example, Emacs running from WSL/Ubuntu: