2

I have WSL + Ubuntu installed on my windows machine, with a few utilities installed.

The following commands open up a GUI when executed from within my Ubuntu terminal:

  • code . (opens up a vscode instance in current working directory)
  • jupyter notebook (opens up a chrome instance on the local host)

The following commands fail to open up a GUI when executed from within my Ubuntu terminal.

  • gvim test.txt
  • matplotlib.pyplot.show() (when run from within a python interpreter)

I am aware of the workarounds for exporting the DISPLAY environment variable to allow opening up GUIs from the command line. Why do some commands (code, jupyter notebook) in WSL+Ubuntu successfully open up a GUI in the Windows host, when (gvim, matplotlib) others require exporting of the DISPLAY variable and use of an XServer ?

5
  • 1
    Presumably, you’re not calling Linux applications when it works.
    – Daniel B
    Jun 4, 2021 at 14:16
  • 1
    I am aware of the workarounds for exporting the DISPLAY environment variable .. in addition to what @DanielB says.. this is not a "workaround" this is how x11 WORKS. You are NOT AWARE or you wouldn't have referred to it as a "workaround". You need an xserver. You need an IP address to host the xserver. By default on a linux machine, this is the loopback and by default, Linux has an xserver to host the calls (when running x11). Jun 4, 2021 at 14:27
  • 1
    There is nothing magic or special about WSL. I had x11 up and working during the betas more than two years ago. Also.. WSL 1 or 2 .. there IS a difference.. but that doesn't matter for this discussion. Jun 4, 2021 at 14:30
  • 1
    @SeñorCMasMas so then WHY doescode and jupyter notebook successfully open a graphical user interface on my monitor WITHOUT THE USE OF XSERVER?
    – user32882
    Jun 4, 2021 at 16:10
  • @user32882 - Just to clear some confusion, are we talking about version 1 or version 2 WSL instance? Only Insider Preview builds, with WSL2, can run Linux GUI applications today. What version (the exact build) of Windows 10 are you using? As been pointed out using a Windows application to remotely display x11 has worked for awhile (but was not explicitly supported).
    – Ramhound
    Jun 4, 2021 at 17:06

1 Answer 1

2

To understand what's going on, you have to make the distinction between "Windows GUI Apps" and "Linux (X) GUI Apps". Everything that you mention that opens is a Windows application.

It's important to note that this answer is only for the current released version of WSL. Later this year, WSLg is scheduled to be released with the 21H2 update of Windows 10. This will add the ability to run Linux GUI applications. As you mention a "workaround" in your question, you are already aware that WSL does not currently include this capability, unless you are running a Windows Insider/Preview version, or if you have manually installed a separate X Server and configured it (as mentioned in the "workaround").

While you would normally expect that you would have to start a Windows app in WSL using the .exe version (and you'd be correct), there are a few exceptions.

For the code . example that you mention, Microsoft specifically includes a shell script in the VSCode download and places it in the Windows PATH. Because you are calling code instead of code.exe, this shell script is being run. You can take a look at it with view "$(which code)". This script, among other things, ends up calling the Windows .exe version.

If you are curious why Windows executables (.exe's) work in WSL, please see this very good answer on the topic.

As for Jupyter notebooks, it is specifically launching a URL. Python includes a webbrowser library that allows it to launch a browser with a URL. You can try it out by launching python3 and then entering the following commands:

>> import webbrowser
>> webbrowser.open("https://superuser.com/q/1654716/1210833")
true

Note that this only works out of the box on the Ubuntu distribution on WSL, and perhaps some others that have provided the proper hooks. For example, in OpenSuse, it does not work by default:

>> import webbrowser
>> webbrowser.open("https://superuser.com/q/1654716/1210833")
false

However, on other distributions, you can simply export the BROWSER environment variable. For example, I'm using the Vivaldi browser on Windows:

export BROWSER="/mnt/c/Program Files/Vivaldi/Application/vivaldi.exe"

After that, OpenSuse works the same as Ubuntu with the webbrowser.open call.

Ubuntu provides the wslu package by default on WSL. This includes wslview, to open the default app for a file (e.g. wslview payroll.pdf) and also the www-browser hook which is probably what Python is using when you launch your Jupyer notebook.

1
  • @SeñorCMasMas Not one bit -- Just pedantic, a trait I share ;-). (Previous comment marked for deletion, since I've seen it. Feel free to do the same with this one when you do.) Jun 4, 2021 at 20:49

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .