73

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

1

13 Answers 13

88

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

8
  • This worked for me, thanks!
    – Husni
    Commented Jan 25, 2021 at 4: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: 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
    Commented 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! Commented Aug 2, 2021 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" Commented Oct 12, 2021 at 17:04
  • 3
    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
    Commented Nov 18, 2021 at 13:38
46

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.

4
  • 10
    Your answer doesn't actually tell me how to open the file after setting the BROWSER env variable...
    – papiro
    Commented Jan 7, 2020 at 15:23
  • 5
    I had to add \ before the whitespace in the path string for this to work.
    – macleginn
    Commented Dec 9, 2020 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
    Commented Jan 25, 2021 at 4:29
  • Works great for me. No need to install anything else Commented Sep 5 at 19:29
28

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.

3
  • 5
    it only opens the Documents folder in Windows explorer for me
    – Greg Woods
    Commented Oct 15, 2019 at 21:48
  • Try to quote the link/path to the webpage like explorer.exe "./Preview.html" Commented Dec 6, 2021 at 16:44
  • At least at time of writing the path translation between WSL and explorer.exe is very finicky. In my experience it hardly works at all on WSL volumes and will arbitrarily not work on Windows volumes. Commented Sep 2, 2022 at 17:19
25

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

6
  • 5
    This answer should get all the upvotes. No need to configure anything. It just works! Commented Jan 8, 2021 at 21:08
  • explorer.exe: command not found
    – Dumbo
    Commented Dec 22, 2021 at 11:32
  • This answer works.
    – JSVJ
    Commented Feb 8, 2022 at 4:30
  • 3
    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. Commented May 17, 2022 at 21:17
  • 1
    Worked for me today on WSL2 Ubuntu 20.04 installed via PowerShell wsl --install -d Ubuntu command
    – Austin D
    Commented Nov 21, 2022 at 9:14
13

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
    Commented Feb 1, 2019 at 9:40
  • I have the same problem as @Holger any updates on a fix?
    – Brett
    Commented Jan 9, 2020 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
    Commented Jan 9, 2020 at 15:01
10

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/… Commented 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
    Commented May 17, 2021 at 20:11
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

2
  • 2
    to convert between Windows and WSL path simply use wslpath
    – phuclv
    Commented Jul 25, 2022 at 3:11
  • alias fe='pwsh.exe -nop -c start .' # File Explorer here
    – joharr
    Commented Jul 27 at 23:49
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

3

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

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

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

You must log in to answer this question.

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