Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why is bash (WSL) using w3m as it's default browser?

Bash on Ubuntu on Windows 10 is using w3m to open URLs inside the terminal instead of opening Chrome or Edge. How can I get it to launch the Windows default web browser?

For example, the following code

webbrowser.open_new("http://www.youtube.com/watch?v=dQw4w9WgXcQ")

looks like this:

webpage displayed as text in terminal

This is exactly the same as opening the page with

w3m http://www.youtube.com/watch?v=dQw4w9WgXcQ

This is on a fresh Win 10 installation and after I've enabled Bash (via the Windows Subsystem for Linux) and done the following:

$ sudo apt update && sudo apt upgrade && sudo apt dist-upgrade
$ sudo apt install build-essential
$ sudo apt install python-pip python-dev
$ sudo pip install --upgrade pip

Yet

print webbrowser._tryorder

gives

['www-browser', 'w3m']

www-browser is just another alias for w3m, why is windows-default not listed?

Previously, The same Python 2.7 code functioned as expected on a 32-bit Windows 10 machine running Git-Bash, so I don't think there are any issues with the python side of things.

like image 893
theNathanielWatkins Avatar asked Dec 24 '22 22:12

theNathanielWatkins


2 Answers

Easy Fix:

This simple work-around works if you are running the latest WSL with Ubuntu 16.04 that comes with the current Windows Insider Preview Slow channel build (#14986). You don't have to run an xserver in the background, and you can launch your Windows browser of choice!

All you need to do is add the following lines to your ~/.bashrc file (Chrome's default install location listed below as an example. Feel free to put the address to your desired browser):

export DISPLAY=:0
export BROWSER=/mnt/c/Program\ Files\ \(x86\)/Google/Chrome/Application/chrome.exe

NOTE: when dealing with complex characters in addresses like spaces or (), you have to escape them with a "\".

Harder (Hackier) Fix:

Basically, Windows Subsystem for Linux (WSL) doesn't officially support GUI based programs. However, there is a workaround to run some GUI based Linux programs from WSL using xserver, see here: How to Run Graphical Linux Desktop Applications from Windows 10’s Bash Shell

If this does not work for you, you may need to also set the BROWSER environment variable, which is what I was trying to do when I found the GUI hack, see here: Google Earth and $BROWSER environment variable

Note:

Even though this "works" it will throw a few errors (example below) as it is starting, just wait and soon it will launch in an xserver window. For one of my python projects, it halted execution. To get around this, I could probably write in some error handling. I'd still be interested to hear if someone has a more elegant solution to the problem on older Windows 10 systems.

Nathaniel@DESKTOP-NAGL0DJ:~$ libkmod: ERROR ../libkmod/libkmod-module.c:1619 kmod_module_new_from_loaded: could not open /proc/modules: No such file or directory
Error: could not get list of modules: No such file or directory
[144:144:0103/123322:FATAL:render_sandbox_host_linux.cc(40)] Check failed: 0 == shutdown(renderer_socket_, SHUT_RD). shutdown: Invalid argument
#0 0x7f41e1522a2e base::debug::StackTrace::StackTrace()
#1 0x7f41e153cf87 logging::LogMessage::~LogMessage()
#2 0x7f41e153d1a9 logging::ErrnoLogMessage::~ErrnoLogMessage()
#3 0x7f41df5ace45 content::RenderSandboxHostLinux::Init()
#4 0x7f41df36c65a content::BrowserMainLoop::EarlyInitialization()
#5 0x7f41df36f7ff <unknown>
#6 0x7f41df368e1d content::BrowserMain()
#7 0x7f41df2d73d4 <unknown>
#8 0x7f41df2d6611 content::ContentMain()
#9 0x7f41e1cf98e8 <unknown>
#10 0x7f41d4681f45 __libc_start_main
#11 0x7f41e1cf97ba <unknown>

Aborted (core dumped)
Sandbox: unexpected multithreading found; this prevents using namespace sandboxing.

(firefox:30): GConf-WARNING **: Client failed to connect to the D-BUS daemon:
Did not receive a reply. Possible causes include: the remote application did not send a reply, the message bus security policy blocked the reply, the reply timeout expired, or the network connection was broken.
like image 94
theNathanielWatkins Avatar answered Jan 11 '23 07:01

theNathanielWatkins


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

Run the two commands from your shell to make it work right away or log out and back in again.

like image 37
Marko V Avatar answered Jan 11 '23 07:01

Marko V