9

I'm running a Windows 10 computer and have recently installed Windows Subsystem for Linux. This generally creates a single terminal with no "display" environment. It seems that Tmux can be used to get several terminals, but this doesn't let you use any sort of graphical display.

I'd like to open a Linux browser, such as Firefox, through Windows Subsystem for Linux.

I tried the suggestions here, but I just get the error:

no DISPLAY environment variable specified
NotTheDr01ds
  • 17,574
  • 4
  • 44
  • 81
user1557414
  • 91
  • 1
  • 1
  • 2
  • Please provide which version of WSL your WSL instance is configured to. We also need to know what version of Windows 10 you are running. – Ramhound Aug 25 '20 at 23:36
  • It is 2022 now, and you can update to the windows 10 version 21H2 (which is latest). And then run this command to update wsl: ```wsl --update```. Then you can directly run GUI apps. – Head Feb 09 '22 at 11:08

6 Answers6

7

See Ubuntu wiki Running Graphical Applications.

You need to install on Windows an X-server to be launched from Windows to be used by WSL graphical applications.

You may use any of X410, VcXsrv, MobaXTerm, Cygwin/C, Xming, Cygwin X, vcXsrv.

If graphical applications can't connect to the running X server automatically paste the following commands to the terminal before starting the applications or set them on every login by adding them to ~/.bashrc:

export DISPLAY=:0 # in WSL 1
export DISPLAY=$(awk '/nameserver / {print $2; exit}' /etc/resolv.conf 2>/dev/null):0 # in WSL 2
export LIBGL_ALWAYS_INDIRECT=1

Another useful article is Running Graphical Programs on Windows Subsystem on Linux.

harrymc
  • 455,459
  • 31
  • 526
  • 924
3

Adding an answer with more recent information than was available at the time the question was originally asked, primarily because I'm linking to this question/answer for other duplicates (here and on Stack Overflow, where it's general an off-topic question).

There are (at least) three options:

  • With the release of WSL 1.0.0 (WSL via Microsoft Store application), both Windows 10 and Windows 11 users have full access to WSLg, which allows Linux GUI applications (X or Wayland) to run directly on WSL2 with no additional configuration. Prior to 1.0.0, this feature was limited to Windows 11 users.

    Windows 10 users will need to be on a build ending in .2311 or higher, which currently means installing KB5020030. If the final four digits of your build number are lower than 2311, make sure your Windows is up-to-date, then find KB5020030 in the Optional Updates.

    Once on an up-to-date Windows 10 or Windows 11 system, you can either install WSL from its Microsoft Store page or ...

    wsl --install
    # or, if already installed
    wsl --update
    # confirm 1.0.0 or later via:
    wsl --version
    
  • A legacy alternative is the one that harrymc offers of installing a third-party X server. I won't repeat the information on how to do so since it's already covered in that answer, although I will say that export DISPLAY=$(hostname).local:0 will typically work, and is more concise. This uses mDNS as I go into detail on in this answer.

  • Finally, if you want to run a Linux desktop under WSL, you might want to consider RDP. You can install xrdp and just access a Linux desktop using Windows Remote Desktop connection. See my steps for doing so in this Ask Ubuntu answer. Note that only xfce is really necessary, although the answer includes more detail on running something more complex like Gnome Desktop.

NotTheDr01ds
  • 17,574
  • 4
  • 44
  • 81
2

I struggled with this same issue and couldn't get it working with any of the fixes here. Unsurprisingly, following the Microsoft Documentation on Running GUI apps in the Windows Subsystem for Linux did the trick.

First step is to install a GUI driver according to your GPU:

Then, open up a CommandPrompt (or PowerShell) and run the following commands.

Fresh install

If you have no prior WSL installation, run wsl --install.
You will need to restart your machine to finish installation, after which installation will resume. Once WSL is launched, you will be prompted for a username and password. You will need that password anytime you need to sudo.
If you still haven't installed any linux distro, check out this other guide.

Existing WSL install

If you already have WSL installed, first close any open instances of WSL.
Run wsl --update.
In order to restart WSL, run wsl --shutdown.

Running GUI apps

Once you've installed/updated WSL, open Linux by running wsl from a command prompt. One final step, run: sudo apt update. You're done!

To verify run (or install and run) any app that requires a GUI.

Running Firefox

sudo apt install firefox
firefox

Hope this helps.

NotTheDr01ds
  • 17,574
  • 4
  • 44
  • 81
MOMO
  • 21
  • 2
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Oct 12 '22 at 20:51
  • While this link may answer the question, it is better to include the essential parts of the answer here and provide the link for reference. Link-only answers can become invalid if the linked page changes. - [From Review](/review/late-answers/1150917) – Toto Oct 12 '22 at 21:18
  • Awesome, thanks. Will do. – MOMO Oct 12 '22 at 23:13
  • A couple of additional notes on this -- First, I mention this option in my answer, but you do go into more detail, which I think is good. However, the main problem with your answer is that the question above is specifically about Windows 10. Your answer just won't work on Windows 10 -- As I mention in my answer, Win 10 users must upgrade to Windows 11 in order to be able to use WSLg (which is what you are recommending). – NotTheDr01ds Oct 13 '22 at 10:12
  • 3
    When I run `firefox`, I get `Command '/usr/bin/firefox' requires the firefox snap to be installed. Please install it with: snap install firefox`. If I run `snap install firefox` it fails with `error: cannot communicate with server: Post "http://localhost/v2/snaps/firefox": dial unix /run/snapd.socket: connect: no such file or directory`. I'm on Windows 11 and `xeyes` is working for me (to prove GUI works). – tolache Dec 06 '22 at 18:46
  • @tolache have you installed firefox in your **WSL**? I mention you need to ´sudo apt install firefox´, I imagine ´snap install firefox´ is equivalent. – MOMO Dec 08 '22 at 15:55
  • @MOMO Thanks for reaching out. Yes, before I did the above, I installed Firefox with `sudo apt install firefox`. – tolache Dec 09 '22 at 10:27
  • @tolache, any luck? I'm having the same issue – Esteban Jan 08 '23 at 20:12
  • @Esteban unfortunately, not. Sorry. If someone figures it out, I would still be interested in the answer. – tolache Jan 09 '23 at 07:37
  • 1
    @tolache, I actually had some luck following this [thread](https://github.com/ubuntu/WSL/issues/299#issuecomment-1324102239), particularly what made `sudo snap install firefox` work for me was to include that `/etc/wsl.conf` file to activate systemd, and `sudo snap refresh` then after a wsl reboot – Esteban Jan 09 '23 at 14:46
2

Remove all export DISPLAY settings in your startup files!

It's not needed anymore.

With recent WSL (Windows 10 Build 19044+ or Win 11), the system takes care of that.

I repeat.

You do no longer need to export the DISPLAY variable!

Prior to reading the official documentation, linked in MOMO's answer, I had tried all the fancy ways of getting the DISPLAY value automatically set posted by others, and nothing worked.

Backstory

I, and many with me, use non-default name servers, so I can't use that trick to refer to my local system. Instead I have a static value for IPv6 localhost in /etc/hosts.

# This is to always have a named host address for servers on WSL, like sshd
# Usually I use 127.0.0.1 instead.
::1     wsl

And the same in Windows c:\Windows\System32\drivers\etc\hosts

Then I had set the DISPLAY variable in ~/.bashrc like this.

export DISPLAY=wsl:0.0

When testing both thunderbird and mousepad start fine.

But if I tried to start firefox/firefox-esr I got failed access to DISPLAY.

$ firefox-esr
Error: cannot open display: wsl:0.0

Basically, thunderbird never cared what DISPLAY was set to. Perhaps that is why it worked for me. It started even back when the variable was set to the wrong IP.

But, now, with no DISPLAY variable manually set, firefox works great!

Information about my Windows 10 system.

$ uname -a
Linux pappa 5.15.79.1-microsoft-standard-WSL2 #1 SMP Wed Nov 23 01:01:46 \
UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 22.04.1 LTS
Release:        22.04
Codename:       jammy


> wsl --version
WSL-version: 1.0.3.0
Kernelversion: 5.15.79.1
WSLg-version: 1.0.47
MSRDC-version: 1.2.3575
Direct3D-version: 1.606.4
DXCore-version: 10.0.25131.1002-220531-1700.rs-onecore-base2-hyp
Windows: 10.0.19045.2364

> wsl -l -v
  NAME      STATE           VERSION
* Ubuntu    Running         2
  Debian    Stopped         2

NVIDIA Geforce GTX 1070, driver 527.56

Apps that I tested.

$ thunderbird --version
 Thunderbird 102.6.1

$ mousepad --version
Mousepad 0.5.8

$ firefox --version
Mozilla Firefox 108.0.1

$ firefox-esr --version
Mozilla Firefox 102.6.0esr

Even something heavy like GIMP runs fine.

$ gimp --version
GNU Image Manipulation Program version 2.10.30

And finally...

$ set | grep DISPLAY
DISPLAY=:0
WAYLAND_DISPLAY=wayland-0
  • FYI, only the `DISPLAY=:0` option worked with `xeyes` for me (wsl2 on up-to-date Win10). – Sz. Jul 11 '23 at 12:06
1

sudo nano /etc/wsl.conf

and put in it:

[boot]
systemd=true

if one omit this detail we get the error !! :

error: cannot communicate with server: Post "http://localhost/v2/snaps/firefox": dial unix /run/snapd.socket: connect: no such file or directory

when

sudo snap install firefox

install-ubuntu-on-wsl2-on-windows-11

-2

We recommended called gwsl better than old wsl did.

  • Welcome to Super User! Please quote the essential parts of the answer from the reference link(s), as the answer can become invalid if the linked page(s) change. – DavidPostill Dec 11 '21 at 22:51