90

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,856

13 Answers13

102

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

52

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,372
30

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
  • 30,396
  • 15
  • 136
  • 260
29

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

15

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'
12

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
  • 221
8

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

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

5

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 upgrade
sudo apt install wslu

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
  • 51
2

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
  • 990
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
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
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