How to set up DISPLAY variable for WSL2 of ubuntu 20?

7,745

Just use

export DISPLAY=:0.0

You can test it will xeyes. If you don't have xeyes

sudo apt install x11-apps
Share:
7,745

Related videos on Youtube

youssef
Author by

youssef

Updated on September 18, 2022

Comments

  • youssef
    youssef over 1 year

    I have a WSL2 with ubuntu 20. In most posts, setting up DISPLAY environmental variable to access windows X11 server is made through WSL localhost address in ~/.bashrc

    $ export DISPLAY=$(awk '/nameserver / {print $2; exit}' /etc/resolve.conf 2>/dev/null):0
    

    or

    $ export DISPLAY=$(cat /etc/resolve.conf |grep nameserver| sed 's/nameserver //'):0
    

    However, these commands will find the WSL2 machine localhost IP that differs from host machine localhost.

    For Me, to make xlaunch X11 server works properly, I have to set DISPLAY every time the host localhost IP is changed using cmd ipconfig command.

    Is there a way to set DISPLAY to host machine localhost before the start of WSL2. For instance, through windows %USERPROFILE%\.wslconfig file.

    • dotnetCarpenter
      dotnetCarpenter almost 3 years
      Perhaps Running WSL GUI Apps on Windows 10 is useful? export DISPLAY=$(ip route|awk '/^default/{print $3}'):0.0 or one of the other two examples in the article.
    • MaxiReglisse
      MaxiReglisse over 2 years
      As explained in techcommunity.microsoft.com/t5/windows-dev-appconsult/…, do not forget to allow VcXsrv in the Windows firewall settings: Windows Security -> Firewall & network protection -> Allow an app through firewall -> make sure VcXsrv has both public and private checked.
  • Roy Truelove
    Roy Truelove about 3 years
    This didn't work for me, though the OP's method did work. Connection Refused.
  • cup
    cup about 3 years
    @RoyTruelove Do you know what the DISPLAY variable evaluated to?
  • tansy
    tansy over 2 years
    Worked for me. It somehow hanged plasma-desktop and trying to come back to my opened windows I tried all kinds of things but seting DISPLAY to ":0.0" saved my day.
  • Admin
    Admin almost 2 years
    Good method - I hadn't seen that one before. There's a simpler version, but it requires the built-in WSL resolver, which some people need to override (I believe the OP did). I'm fairly sure yours will work even when not using the built-in WSL resolver.
  • Admin
    Admin almost 2 years
    Do you mean using the "$(hostname).local". I saw that but couldnt get it to work with the Xserver which is running on the host. ie. when I do a $ telnet "$(hostname).local" 6000 ... which is the XServer port, I cant reach it. Whereas it would when I used the above. I think it's a firewall problem which I cannot play around with cos it's a work computer and everything is kind of locked down.
  • Admin
    Admin almost 2 years
    Right - Do you override /etc/resolv.conf? If so, then the $(hostname).local (mDNS lookup) won't work. I wrote up a separate answer on it, but mentioned that your technique should be used if the mDNS name can't be used.