Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

WSL (Ubuntu): how to open localhost in browser from bash terminal

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

So far I have tried:

  • How can I open Google Chrome from the terminal with the URL "localhost:3000"?
  • "Couldn't find a file descriptor referring to the console" on Ubuntu bash on Windows
  • How to mention C:\Program Files in batchfile

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.

like image 405
Mirosław Avatar asked Oct 07 '18 18:10

Mirosław


People also ask

How do I access localhost WSL?

WSL translates the Linux system calls into windows ones so the Ubuntu network data flows through the exact same TCP/IP stack as the windows data. In short this means to access the Linux localhost you just access the windows one, they are the same. localhost:4567 or 127.0. 0.1:4567 will do what you want.

Can I open a browser in WSL?

This can be achieved using the wslview utility and setting the $BROWSER environment variable to 'BROWSER=wslview'. wslview is a WSL browser emulator that can help you open links in the default Windows browser or open files on Windows from WSL.


2 Answers

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

like image 148
Oliver Sorg Avatar answered Sep 29 '22 00:09

Oliver Sorg


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.

like image 38
gibbone Avatar answered Sep 29 '22 01:09

gibbone