Open browser in host system from Windows Subsystem for Linux?
Solution 1
Install wslu and add export BROWSER=wslview
to your $HOME/.bashrc
(or equivalent if you use other shell)
Solution 2
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
.
Solution 3
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.
Solution 4
I used
sensible-browser http://localhost:8001/
on Ubuntu 20.04 w/ WSL2
Solution 5
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'
Related videos on Youtube
janpio
Updated on September 18, 2022Comments
-
janpio over 1 year
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 theopen
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 usesee
orxdg-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 sosee
works insidebash
of WSL?
Bonus question: If yes, how can I make the ruby script work without changing that code? It's an external dependency :/
-
Holger over 5 yearsI 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 executeexplorer.exe https://www.google.com
. Any idea on how to get only one tab via xdg-open? -
Greg Woods over 4 yearsit only opens the Documents folder in Windows explorer for me
-
papiro over 4 yearsYour answer doesn't actually tell me how to open the file after setting the
BROWSER
env variable... -
Brett over 4 yearsI have the same problem as @Holger any updates on a fix?
-
Brett over 4 yearsusing
update-alternatives --install "bin/host_chrome" "chrome" "/mnt/c/Program Files (x86)/Google/Chrome/Application/chrome.exe" 1
followed byexport BROWSER=host_chrome
did the trick for me. -
herrklaseen almost 4 yearsThis 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/… -
Param Siddharth over 3 yearsThis worked for me, thank you! :)
-
macleginn over 3 yearsI had to add \ before the whitespace in the path string for this to work.
-
Daan van den Bergh over 3 yearsThis answer should get all the upvotes. No need to configure anything. It just works!
-
Husni over 3 yearsThis worked for me, thanks!
-
Husni over 3 yearsThis didn't work for me as it's directly using linux path, e.g. file:///tmp/cover688282190/coverage.html
-
buzzedword about 3 yearsThis 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, usingwslview
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. -
pmdci about 3 yearsdid 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. -
dotnetCarpenter almost 3 yearsYou still have to set
export BROWSER=wslview
in .bashrc or .profile or similar. You can on Ubuntu start the default browser withwslview
or ` xdg-open`. I think the answer should make that clear but otherwise nice answer @ParamSiddharth! -
pabouk - Ukraine stay strong over 2 yearsUnfortunately this setting does not work for
xdg-open
with a file as an argument likexdg-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"
-
dzz over 2 years
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. -
David DIVERRES over 2 yearsTry to quote the link/path to the webpage like
explorer.exe "./Preview.html"
-
Sean87 over 2 yearsexplorer.exe: command not found
-
JSVJ over 2 yearsThis answer works.
-
Admin about 2 yearsThis 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.
-
Admin about 2 years@buzzedword Unfortunately since Ubuntu 22.04
wslu
is not installed by default. github.com/wslutilities/wslu/issues/233