31

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

22

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
  • 6
    Your answer doesn't actually tell me how to open the file after setting the BROWSER env variable... – user305964 Jan 7 '20 at 15:23
  • I had to add \ before the whitespace in the path string for this to work. – macleginn Dec 9 '20 at 10:31
  • This didn't work for me as it's directly using linux path, e.g. file:///tmp/cover688282190/coverage.html – Husni Jan 25 at 4:29
14

Install wslu and use 'BROWSER=wslview'

2
  • This worked for me, thank you! :) – Param Siddharth Oct 7 '20 at 4:41
  • This worked for me, thanks! – Husni Jan 25 at 4:24
12

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.

1
  • 2
    it only opens the Documents folder in Windows explorer for me – Greg Woods Oct 15 '19 at 21:48
8

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

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

5

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

1
  • This answer should get all the upvotes. No need to configure anything. It just works! – Daan van den Bergh Jan 8 at 21:08
4

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
1
  • 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/… – herrklaseen May 22 '20 at 7:59

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.