Open browser in host system from Windows Subsystem for Linux?

63,720

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'
Share:
63,720

Related videos on Youtube

janpio
Author by

janpio

Updated on September 18, 2022

Comments

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

  • Holger
    Holger over 5 years
    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?
  • Greg Woods
    Greg Woods over 4 years
    it only opens the Documents folder in Windows explorer for me
  • papiro
    papiro over 4 years
    Your answer doesn't actually tell me how to open the file after setting the BROWSER env variable...
  • Brett
    Brett over 4 years
    I have the same problem as @Holger any updates on a fix?
  • Brett
    Brett over 4 years
    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.
  • herrklaseen
    herrklaseen almost 4 years
    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/…
  • Param Siddharth
    Param Siddharth over 3 years
    This worked for me, thank you! :)
  • macleginn
    macleginn over 3 years
    I had to add \ before the whitespace in the path string for this to work.
  • Daan van den Bergh
    Daan van den Bergh over 3 years
    This answer should get all the upvotes. No need to configure anything. It just works!
  • Husni
    Husni over 3 years
    This worked for me, thanks!
  • Husni
    Husni over 3 years
    This didn't work for me as it's directly using linux path, e.g. file:///tmp/cover688282190/coverage.html
  • buzzedword
    buzzedword about 3 years
    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-w‌​sl. 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.
  • pmdci
    pmdci about 3 years
    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.
  • dotnetCarpenter
    dotnetCarpenter almost 3 years
    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!
  • pabouk - Ukraine stay strong
    pabouk - Ukraine stay strong over 2 years
    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"
  • dzz
    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
    David DIVERRES over 2 years
    Try to quote the link/path to the webpage like explorer.exe "./Preview.html"
  • Sean87
    Sean87 over 2 years
    explorer.exe: command not found
  • JSVJ
    JSVJ over 2 years
    This answer works.
  • Admin
    Admin about 2 years
    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.
  • Admin
    Admin about 2 years
    @buzzedword Unfortunately since Ubuntu 22.04 wslu is not installed by default. github.com/wslutilities/wslu/issues/233