21

I am trying to open http://localhost in (any) browser from WSL bash terminal.

So far I have tried:

No luck in setting up BROWSER variable for xdg-open, it responds to xdg-open http://localhost with /usr/bin/xdg-open: 851: /usr/bin/xdg-open: /c/"Program: not found.

I have tried escaping with \ and ^. Using %ProgramFiles(x86)% and ofcorse "Program Files (x86)". More or less it is the same answer every time... Any ideas how to set a work flow for opening browser in WSL?

So far I've ended up with:

/c/Program\ Files\ \(x86\)/Google/Chrome/Application/chrome.exe localhost

But I am looking for more elegant solution.

Mirosław
  • 356
  • 1
  • 3
  • 8
  • This doesn't entirely answer your question, but in MacOS you can open via a Bash terminal with the following `open http://localhost:3000` I'm sorry, but that's the only setup I can test at the moment. – holaymolay Oct 07 '18 at 19:11
  • Linux binaries can't expand windows environment variables. – Biswapriyo Oct 08 '18 at 14:10

11 Answers11

17

You can invoke the Windows command line from Bash and use Windows file association to open URL with the default Windows browser.

To do so just type in Bash:

cmd.exe /C start http://localhost

In my case this loads localhost in Chrome, note that the full URL is necessary for Windows to decide what to do.

This is similar to what open does in MacOS, hence you may find useful to directly alias the command and use it also for other type of files:

# add this to .bash_aliases
open='cmd.exe /C start'

Now you can open URL or open file.pdf directly from WSL.


Note: since you are simply redirecting commands to cmd.exe, it needs to have access to the file your working with. As a consequence the above solution will work when you find yourself in the Windows file system, but probably will fail when you are working with files in Linux partition (i.e. in the tmp or in the bin folder). This probably has been fixed in the new version of the WSL but I have not tested it.

gibbone
  • 2,300
  • 20
  • 20
  • This doesn't quite get all the way. Trying this using something like `open file.pdf` from `/tmp` will fail, since CMD doesn't know how to handle UNC paths. Furthermore, if an absolute path is given (`open /tmp/file.pdf`), CMD will interpret the first directory as a switch, and refuse to act since something like `/tmp` isn't a known switch to CMD – Zev Isert Sep 26 '20 at 00:28
  • 1
    The solution I have proposed is just a redirection of WSL commands to cmd.exe ones, hence it will not work wherever cmd.exe is not allowed to access (as in the `tmp` folder). This is a problem of interoperability of the WSL with Windows and I believe cannot be solved in any way. Thanks anyway for the comment, I'm editing my answer to include this detail. – gibbone Sep 27 '20 at 10:06
  • Be careful with this one. I've had configurations saved to the Windows file system that WSL was expecting to get and then could not see. – Michael May 05 '22 at 15:55
12

You are almost there. Just add an alias for the windows chrome executable http://www.linfo.org/alias.html

alias chrome="/c/Program\ Files\ \(x86\)/Google/Chrome/Application/chrome.exe"

Now you can simply run chrome localhost and open chrome in any web location you desire.

Damo
  • 5,698
  • 3
  • 37
  • 55
  • 4
    FWIW, my current install requires /mnt/c instead of /c `alias chrome="/mnt/c/Program\ Files\ \(x86\)/Google/Chrome/Application/chrome.exe"` __Note, this is convenient, but doesn't resolve the issue of running a native Linux Chrome binary.__ – Richard Logwood Apr 16 '20 at 12:12
  • In order to run a native Linux GUI application you need a XServer. My "weapon of choice" is VcXsrv, but there are also other good alternatives like Xming or the XServer from the Cygwin. – Lohmar ASHAR Sep 09 '21 at 21:36
  • In my version of WSL, version: 0.70.0.0, chrome is here `alias chrome="/mnt/c/Program\ Files/Google/Chrome/Application/chrome.exe' – ejkitchen Oct 24 '22 at 22:51
  • This has been updated to. `alias chrome="/mnt/c/Program\ Files/Google/Chrome/Application/chrome.exe"` – James O'Toole Jul 17 '23 at 04:11
8

To open localhost in browser from bash terminal, you need to configure wsl so that it
defaults to whatever browser has been set as default in your windows 10 system.

You can do this by using some tools from wslu ("A collection of utilities for WSL").
For this purpose you need.

  • wslview (-u, --unregister "remove wslview as the default WSL web browser.
    -r, --register "register wslview as the default WSL web browser.)

  • wslpath (-a "force result to absolute path format",
    -u "translate from a Windows path to a WSL path (default)")

You need to register your preferred browsers like this...
For Google Chrome:
wslview -r $(wslpath -au 'C:\Program Files (x86)\Google\Chrome\Application\chrome.exe')

For Microsoft Edge:
wslview -r $(wslpath -au 'C:\Windows\SystemApps\Microsoft.MicrosoftEdge_8wekyb3d8bbwe\MicrosoftEdge.exe')

Now you can open localhost as x-www-browser localhost:8080 or www-browser localhost:8080 and x-www-browser or www-browser will default to whatever is your current windows 10 default browser provided it has been registered as described above.
Do not forget to indicate the port; localhost alone did not work for me.

To unregister any browser just change the -r flag to -u.

Have a look at wslview help: info wslview <enter> in the wsl terminal and wslpath <enter> for help with wslpath.

5

Install wslu (A collection of utilities for WSL) https://github.com/wslutilities/wslu#feature and then add these two lines to your shell's RC file, e.g. .bashrc or .zshrc.

export DISPLAY=:0
export BROWSER=/usr/bin/wslview
Marko V
  • 121
  • 2
  • 3
2

You can set the BROWSER variable as you have done . But xdg-open won't work in WSL as the xdg-openscripts are setup to work with unquoted environment variables ( in which case , the path breaks due to spaces in the pathname ).

You can use the wsl-opennpm utility to do the same for WSL .

Once you have npm installed , install wsl-open utility :

sudo npm install -g wsl-open

To open any URL with default Windows Browser :

wsl-open http://google.com

You can also set wsl-open as default program for a file type in WSL :

wsl-open -w // sets wsl-open as the Shell Browser

Then you can use the standard xdg-open for URLs as well with default windows browser :

xdg-open http://google.com 
Dharman
  • 30,962
  • 25
  • 85
  • 135
1

I created a script that basically forwards xdg-open to powershell -c start

Not tested much though.

sudo tee /usr/local/bin/xdg-open <<EOF
#!/bin/sh

powershell.exe -c start "'\$@'"
EOF
sudo chmod +x /usr/local/bin/xdg-open

Cheers Oliver

0

Came across this article that worked for me: https://towardsdatascience.com/running-jupyter-notebook-on-wsl-while-using-firefox-on-windows-5a47ebfae4c1

In short:

Step 1 - Generate config for Jupyter Notebook:

jupyter notebook --generate-config

Step 2 - Edit the config file using "nano" or other editor

The config fileshould be under your home directory under ".jupyter" folder:

~/.jupyter/jupyter_notebook_config.py

Step 3 - Disable launching browser by redirecting file

First comment out the line, then change True to False:

c.NotebookApp.use_redirect_file = False

Step 4 - add a line to your .bashrc file to set the BROWSER path

export BROWSER='/mnt/c/Program Files (x86)/Google/Chrome/Application/chrome.exe'

For me it was Chrome under my Windows Program File. Otherwise any linux installation under WSL doesn't have a native browser to launch, so need to set it to the Windows executable.

Step 5 - restart .bashrc

source .bashrc

That should work!

Angelo
  • 341
  • 1
  • 2
  • 11
0

https://github.com/microsoft/WSL/issues/3632#issuecomment-690061348

export BROWSER='eval "/mnt/c/Program Files (x86)/Google/Chrome/Application/chrome.exe"'

xdg-open https://google.com   # nice work

solved the spaced path problem. it worked for me.

Siuol Lee
  • 35
  • 4
0

Now you can simple use :

sensible-browser http://www.google.com

it already comes with wsl and it opens the default browser in windows

ps: you can also use wslview . to open the file explorer from the bash terminal

0

i'll give you a suggestion, it could be opened via visual studio code in wsl. And install the live server plugin.

Everton P M A
  • 159
  • 1
  • 7
-1

Ok so first of all, I don't use windows anymore so I can't post a full solution that I've personally tested, but back when I did use windows, I use to do this and it worked. (This should probably be a comment, but a while back I deleted some unaccepted answers and lot the associated reputation :/)

Solution:

Don't try to launch your windows programs from inside WSL, instead install the linux version of the program and an X server, such as Xming. Here is an example tutorial for forwarding X apps back to Xming on windows.

Summarized, install Xming (on Windows). Then export the DISPLAY variable:

export DISPLAY=:0

Install google-chrome inside WSL and launch it via the CLI. It should show up on your desktop.

Note: There's also a way to use PuTTY alongside XMing for remote viewing, but you'll need to disable Windows firewalls and install the full openssh-server inside WSL first.

taigrr
  • 63
  • 8