WSL 2: Run Graphical Linux Desktop Applications from Windows 10 Bash Shell "Error E233: cannot open display"

ASE picture ASE · May 18, 2020 · Viewed 45.3k times · Source

How to run graphical Linux desktop applications from Windows 10’s Bash Shell?

First, I installed Windows Subsystem for Linux (WSL) following steps as shown in here as follows:

(1) Installed Windows 10 Pro Insider Preview Build 19619.

(2) Installed Ubuntu Linux distribution.

(3) Changed the distribution version from WSL 1 to WSL 2.

Second, to enable graphical Linux desktop applications from Windows 10’s Bash Shell, I followed the following steps as shown here as follows:

(4) I installed an X Server that is Xming

(5) Installed graphical GTK-based vim editor as test using:

sudo apt-get install vim-gtk

(6) Set my display environment variable

export DISPLAY=:0

(7) Launch an Application

gvim

However, this did not lunch the application and I got the following error:

E233: cannot open display
Press ENTER or type command to continue
E852: The child process failed to start the GUI
Press ENTER or type command to continue

Any idea why this error is occurring?

Answer

Jaime picture Jaime · Feb 27, 2021

The networking subsystem in WSL2 is different than the used in WSL1. You must consider the differences to access networking apps running on Windows and on Linux:

  • In WSL1, Linux uses the same IP addresses than the Windows host, then, you can access the applications using localhost or 127.0.0.1
  • In WSL2, Linux runs on a lightweight virtual machine and has a different IP address. To access networking apps running on the Windows Host you must use the Windows IP address.

Checking the IP address of the Windows host

There are many ways to determine the IP addresses in the Windows host. You may run the following commands in your WSL Linux:

  • cat /etc/resolv.conf shows the IP address of the eth0 interface in Windows
  • ipconfig.exe shows the all the IP configuration in the Windows host
  • route.exe print shows the network routing configuration in the Windows host

Setting the DISPLAY variable for WSL2

Based on the Microsoft documentation, you may set the DISPLAY variable checking the nameserver in the /etc/resolv.conf file. (@fqquiner and @VPraharsha already mentioned this)

export DISPLAY=$(grep nameserver /etc/resolv.conf | awk '{print $2}'):0.0

However, I had problems using this solution, probably because I use my notebook with a WiFi connection and multiple virtual networks. Instead of the previous solution, I determine the Windows IP address using route.exe and checking the interface used in the default gateway.

export DISPLAY=$(route.exe print | grep 0.0.0.0 | head -1 | awk '{print $4}'):0.0

Setting the DISPLAY variable in the .profile

You may set the DISPLAY variable in your ~/.profile file. I used the following code:

# set DISPLAY to use X terminal in WSL
# in WSL2 the localhost and network interfaces are not the same than windows
if grep -q WSL2 /proc/version; then
    # execute route.exe in the windows to determine its IP address
    DISPLAY=$(route.exe print | grep 0.0.0.0 | head -1 | awk '{print $4}'):0.0

else
    # In WSL1 the DISPLAY can be the localhost address
    if grep -q icrosoft /proc/version; then
        DISPLAY=127.0.0.1:0.0
    fi

fi