53

I have a ruby script that does contains this line:

system("open '#{html_path}'")

html_path is the path to a local .html file. So the script opens a browser where I can normally inspect and view this file.

In contrast to a MacOS environment, this doesn't work in bash of WSL. Running the script nothing happens, and when I execute the open call in the console directly I get this:

sujan@LenovoX1:/mnt/c/Users/Jan/Documents/foo$ open Preview.html
Couldn't get a file descriptor referring to the console

I already investigated that open does something different in Ubuntu and I should use see or xdg-open to open a file.

Unfortunately these also don't work in WSL:

sujan@LenovoX1:/mnt/c/Users/Jan/Documents/foo$ see ./Preview.html
Unescaped left brace in regex is deprecated, passed through in regex; marked by <-- HERE in m/%{ <-- HERE (.*?)}/ at /us
r/bin/see line 528.
Couldn't find a suitable web browser!
Set the BROWSER environment variable to your desired browser.
Warning: program returned non-zero exit code #1

and

sujan@LenovoX1:/mnt/c/Users/Jan/Documents/foo$ xdg-open ./Preview.html
Unescaped left brace in regex is deprecated, passed through in regex; marked by <-- HERE in m/%{ <-- HERE (.*?)}/ at /us
r/bin/run-mailcap line 528.
Couldn't find a suitable web browser!
Set the BROWSER environment variable to your desired browser.
Warning: program returned non-zero exit code #1
/usr/bin/xdg-open: 771: /usr/bin/xdg-open: www-browser: not found
/usr/bin/xdg-open: 771: /usr/bin/xdg-open: links2: not found
/usr/bin/xdg-open: 771: /usr/bin/xdg-open: elinks: not found
/usr/bin/xdg-open: 771: /usr/bin/xdg-open: links: not found
/usr/bin/xdg-open: 771: /usr/bin/xdg-open: lynx: not found
/usr/bin/xdg-open: 771: /usr/bin/xdg-open: w3m: not found
xdg-open: no method available for opening './Preview.html'

Thinking about it, this makes sense: There is no browser available inside WSL.

Can I somehow set this BROWSER variable so see works inside bash of WSL?


Bonus question: If yes, how can I make the ruby script work without changing that code? It's an external dependency :/

11 Answers 11

37

Install wslu and add export BROWSER=wslview to your $HOME/.bashrc (or equivalent if you use other shell)

7
  • This worked for me, thank you! :) Oct 7, 2020 at 4:41
  • This worked for me, thanks!
    – Husni
    Jan 25, 2021 at 4:24
  • 5
    This should probably be marked as the answer-- this is preinstalled on Ubuntu as a part of ubuntu-wsl and shipped by canonical engineers. You can see a blog post about it here: ubuntu.com/blog/new-installation-options-coming-for-ubuntu-wsl. While setting the $BROWSER envvar does in fact also solve this problem, using wslview allows for predictable bridging between the WSL and Windows environments. I usually also reference wiki.ubuntu.com/WSL first with anything specific to Ubuntu as it's frequently updated.
    – buzzedword
    Mar 24, 2021 at 15:52
  • 1
    You still have to set export BROWSER=wslview in .bashrc or .profile or similar. You can on Ubuntu start the default browser with wslview or ` xdg-open`. I think the answer should make that clear but otherwise nice answer @ParamSiddharth! Aug 2, 2021 at 19:45
  • Unfortunately this setting does not work for xdg-open with a file as an argument like xdg-open index.hml. In this case Windows' Start gets the Linux path in the original form: The system cannot find the file specified. ... + Start "/home/user/tmp/index.html" Oct 12, 2021 at 17:04
34

You can set the BROWSER variable. There is a long article explaining how to set environment variables.

If you want to set the variable for the current terminal session you can use:

export BROWSER='/mnt/c/Program Files/Firefox/firefox.exe'

(assuming you want to use Firefox and have it installed in C:\Program Files\Firefox\firefox.exe)

If you want this to be persistent, you can add the above line to the file ~/.bashrc.

3
  • 9
    Your answer doesn't actually tell me how to open the file after setting the BROWSER env variable...
    – papiro
    Jan 7, 2020 at 15:23
  • 1
    I had to add \ before the whitespace in the path string for this to work.
    – macleginn
    Dec 9, 2020 at 10:31
  • 1
    This didn't work for me as it's directly using linux path, e.g. file:///tmp/cover688282190/coverage.html
    – Husni
    Jan 25, 2021 at 4:29
18

For some reason WSL can see the windows file explorer. If you do:

explorer.exe Preview.html 

in the console it should open in the default Windows web browser.

2
  • 3
    it only opens the Documents folder in Windows explorer for me
    – Greg Woods
    Oct 15, 2019 at 21:48
  • Try to quote the link/path to the webpage like explorer.exe "./Preview.html" Dec 6, 2021 at 16:44
14

I used sensible-browser http://localhost:8001/ on Ubuntu 20.04 w/ WSL2

4
  • 4
    This answer should get all the upvotes. No need to configure anything. It just works! Jan 8, 2021 at 21:08
  • explorer.exe: command not found
    – Sean87
    Dec 22, 2021 at 11:32
  • This answer works.
    – JSVJ
    Feb 8 at 4:30
  • This does not work in Ubuntu 22.04 / WSL2 (without setting anything): Couldn't find a suitable web browser! Set the BROWSER environment variable to your desired browser. May 17 at 21:17
10

Combining the two answers above, the simplest solution is to set BROWSER to windows file explorer in order to use the default Windows web browser

export BROWSER='/mnt/c/Windows/explorer.exe'
3
  • 1
    I like that idea, but when I call xdg-open https://www.google.com my Firefox for Windows open two tabs with google, while it is only one tab when I execute explorer.exe https://www.google.com. Any idea on how to get only one tab via xdg-open?
    – Holger
    Feb 1, 2019 at 9:40
  • I have the same problem as @Holger any updates on a fix?
    – Brett
    Jan 9, 2020 at 14:47
  • 2
    using update-alternatives --install "bin/host_chrome" "chrome" "/mnt/c/Program Files (x86)/Google/Chrome/Application/chrome.exe" 1 followed by export BROWSER=host_chrome did the trick for me.
    – Brett
    Jan 9, 2020 at 15:01
7

Building on the prior answers

export BROWSER=/some/path/to/chrome.exe is likely the solve you are looking for, however chrome's default path is /mnt/c/Program Files (x86)/Google/Chrome/Application/chrome.exe (a path with spaces and parens for the shell to escape). This works fine if the application uses the a quoted version of the variable "${BROWSER}" will successfully open chrome. However if you use the variable unquoted $BROWSER, bash will consider it as individual variables. This breaks in some tools, such as xdg-open.

working around this is simple, you can symlink it to a path with out spaces.

ln -s "/mnt/c/Program Files (x86)/Google/Chrome/Application/chrome.exe" ~/.local/bin/chrome
export BROWSER=~/.local/bin/chrome

Alternatively, you can use update-alternatives as Brett pointed out. In the end, this is also a symlink, but has slightly more visibility for when/if you need to update where the link points.

update-alternatives --install "bin/host_chrome" "chrome" "/mnt/c/Program Files (x86)/Google/Chrome/Application/chrome.exe" 1
export BROWSER=host_chrome
2
  • 1
    This helped me, but I needed to set the absolute path to the link target /home/username/.local/bin/chrome to be able to launch the browser. Now I'm able to launch Chrome from Hyper and from the terminal in VS Code. Also see this post regarding chrome tmp files: might be useful if you are running karma tests: stackoverflow.com/questions/54090298/… May 22, 2020 at 7:59
  • did not work for me. It complains that "alternative link is not absolute as it should be". So it is missing a forward-slash for "bin/host_chrome". Should be "/bin/host_chrome". However the browser opens, but it does not open the URL in question. Just the blank start page.
    – pmdci
    May 17, 2021 at 20:11
6

Here is a "poor man's" open from macOS for WSL:

alias open="powershell.exe /c start"

Add that to your ~/.profile or ~/.bashrc as appropriate, or run it in your WSL shell, and then the following things will work:

open . # opens current folder in Explorer as e.g. \\wsl$\Ubuntu\home\john\myapp
open foobar.txt # opens in notepad.exe
open README.md # opens in VSCode in Windows, for example
open http://example.com # opens in your default web browser in Windows
open Preview.html # opens in your default web browser in Windows, as file://wsl%24/Ubuntu/home/john/Preview.html

Downside: this only seems to work with relative paths, not absolute paths

It would be nice if someone made a shell script to emulate the macOS version, like -a for application, -e for editor, -R to reveal in explorer.exe, etc... and make it work with absolute paths

1
  • 1
    to convert between Windows and WSL path simply use wslpath
    – phuclv
    Jul 25 at 3:11
1

Under WSL2, you only need to install xdg-utils:

sudo apt-get update -y
sudo apt-get install -y xdg-utils

Then open any page you want with:

xdg-open https://superuser.com/
1

x-www-browser

Set the default browser of x-www-browser like this:

$ sudo update-alternatives --config x-www-browser
There are 3 choices for the alternative x-www-browser (providing /usr/bin/x-www-browser).

  Selection    Path                           Priority   Status
------------------------------------------------------------
  0            /usr/bin/google-chrome-stable   200       auto mode
  1            /usr/bin/firefox                40        manual mode
  2            /usr/bin/google-chrome-stable   200       manual mode
* 3            /usr/bin/wslview                30        manual mode

Press <enter> to keep the current choice[*], or type selection number: 

Source: https://askubuntu.com/a/16626

Then you can update the browser priorities like this:

sudo update-alternatives --install /usr/bin/x-www-browser x-www-browser <browser-path> <priority_as_integer>

Source: https://askubuntu.com/a/620983/1507914

xdg-open

Set the default browser of xdg-open by creating a file /usr/share/applications/wslview.desktop with this content:

[Desktop Entry]
Version=1.0
Name=WSLview
Exec=wslview %u
Terminal=false
X-MultipleArgs=false
Type=Application
Categories=GNOME;GTK;Network;WebBrowser;
MimeType=text/html;text/xml;application/xhtml+xml;application/xml;application/rss+xml;application/rdf+xml;image/gif;image/jpeg;image/png;x-scheme-handler/http;x-scheme-handler/https;x-scheme-handler/ftp;x-scheme-handler/chrome;video/webm;application/x-xpinstall;

and then running this:

xdg-settings set default-web-browser wslview.desktop

Source: https://github.com/wslutilities/wslu/issues/214#issue-1069341444

0

There is a way to do this without installing anyhting.

First open your browser and open file://wsl.localhost/ and lets just say you want to open Ubuntu-20.04 then you can change that URL to file://wsl.localhost/Ubuntu-20.04/

you can take a list of what you can access with file://wsl.localhost/ and its name on powershell or cmd with command wslconfig /l.

0

The following procedure helps you in WSL2:

Open ~./bashrc

nano ~./bashrc

Add the following command to the end of the file.

  • If you use Firefox:
export BROWSER='/mnt/c/Program Files/Mozilla Firefox/firefox.exe'
  • If you use Google Chrome:
 export BROWSER='/mnt/c/Program Files/Google/Chrome/Application/chrome.exe'

Now, save the file and close it. Then run the following command:

source ~./bashrc

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.