66

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 :/

janpio
  • 2,396
  • 8
  • 25
  • 34

13 Answers13

63

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

Pawel Zubrycki
  • 731
  • 5
  • 4
  • This worked for me, thank you! :) – Param Siddharth Oct 07 '20 at 04:41
  • This worked for me, thanks! – Husni Jan 25 '21 at 04:24
  • 8
    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: https://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 https://wiki.ubuntu.com/WSL first with anything specific to Ubuntu as it's frequently updated. – buzzedword Mar 24 '21 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! – dotnetCarpenter Aug 02 '21 at 19:45
  • 2
    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"` – pabouk - Ukraine stay strong Oct 12 '21 at 17:04
  • `wslview` needs to run powershell, which is extremely slow - urls open with a delay of seconds on my computer. `explorer.exe` on the other hand is instantaneous and opens the url using your default browser as per your Windows settings. – dzz Nov 18 '21 at 13:38
  • @buzzedword Unfortunately since Ubuntu 22.04 `wslu` is not installed by default. https://github.com/wslutilities/wslu/issues/233 – pabouk - Ukraine stay strong May 17 '22 at 23:20
  • .. also attempting to install wslu on 22.04 seriously fubar'd my system. – Brian Horakh Nov 27 '22 at 22:48
40

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.

Sean Allred
  • 1,182
  • 2
  • 11
  • 32
Florian_1990
  • 509
  • 3
  • 2
  • 10
    Your answer doesn't actually tell me how to open the file after setting the `BROWSER` env variable... – papiro Jan 07 '20 at 15:23
  • 3
    I had to add \ before the whitespace in the path string for this to work. – macleginn Dec 09 '20 at 10:31
  • 2
    This didn't work for me as it's directly using linux path, e.g. file:///tmp/cover688282190/coverage.html – Husni Jan 25 '21 at 04:29
23

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.

phuclv
  • 26,555
  • 15
  • 113
  • 235
Elizabeth Hou
  • 331
  • 2
  • 2
19

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

Michael Cole
  • 717
  • 6
  • 8
12

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'
  • 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 01 '19 at 09:40
  • I have the same problem as @Holger any updates on a fix? – Brett Jan 09 '20 at 14:47
  • 3
    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 09 '20 at 15:01
9

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
Xarses
  • 191
  • 1
  • 4
  • 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: https://stackoverflow.com/questions/54090298/karma-use-windows-chrome-from-wsl – herrklaseen May 22 '20 at 07: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 '21 at 20:11
7

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

John Douthat
  • 671
  • 1
  • 10
  • 22
2

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

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/
ixe013
  • 930
  • 9
  • 19
1

Run following commands in WSL

sudo yum install yum-utils
sudo yum install wslu
export BROWSER=wslview

If your default browser is not firefox then you will have to create a symbolic link to make it work.

ln -s "/mnt/c/Program Files/Mozilla Firefox/firefox.exe" ~/.local/bin/firefox
export BROWSER=~/.local/bin/firefox

Persist this setting, add the following line to ~/.bashrc ~/.bash_profile at the end.

Use vim, vi or nano editor of your choice.

export BROWSER=~/.local/bin/firefox

Sumith
  • 111
  • 2
1

After reading different solutions here and other forums, having the same issue with error

xdg-open: no method available for opening [url]

to my amazement, what worked for me under my Ubuntu 22.04.1 LTS install on WSL2 is

sudo apt install wslu
sudo apt upgrade

then tried the "open" (xdg-open) command one more time and it just worked. In other words, it opened the default browser on my windows 10

plpepin
  • 11
  • 2
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.

Aditya
  • 125
  • 7
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