WSL(windows subsytem linux)下添加网络浏览器支持

windows下在subsystem linux中安装了Anaconda,在使用jupyter notebook时,发现命令行并不能自动打开浏览器,原来是子系统linux中没有可用网页浏览器,本文就讲述一下如何在WSL中添加网络浏览器支持。

问题现象

WSL命令行中,在相应目录下使用jupyter notebook,会出现如下提示:

➜  Algorithm_Research git:(master) jupyter notebook
[I 10:44:16.783 NotebookApp] Serving notebooks from local directory: /mnt/c/Users/smslit/Desktop/topscomm/Algorithm_Research
[I 10:44:16.784 NotebookApp] 0 active kernels 
[I 10:44:16.784 NotebookApp] The Jupyter Notebook is running at: http://localhost:8888/?token=000982665d732a5d7052fe3bbadb4346fe0b3fdbc8b87bb4
[I 10:44:16.784 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[W 10:44:16.789 NotebookApp] No web browser found: could not locate runnable browser.
[C 10:44:16.789 NotebookApp] 
    
    Copy/paste this URL into your browser when you connect for the first time,
    to login with a token:
        http://localhost:8888/?token=000982665d732a5d7052fe3bbadb4346fe0b3fdbc8b87bb4

其中可以看到No web browser found: could not locate runnable browser,这是因为win下启用了ubuntu bash后只是命令行的接口,但没有相应网络浏览器安装。

意外发现

WSL可以执行exe

惊奇的发现wsl的命令行中可以调用exe,比如执行以下命令可以打开chrome浏览器,也就是说可以将windows中安装的浏览器注册给wsl。

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

WSL中可以用命令行配置默认浏览器

如果我们直接用如下命令行配置网页浏览器,会提示没有安装浏览器,没有选项。

sudo update-alternatives --config x-www-browser

怎么办?这个时候想到了一种饶路子的方式,ubuntu下有配置浏览器选项优先级的命令,这个命令需要指明浏览器,此时指定win 下chrome的路径就可以了,试一下:

➜  Downloads cd /mnt/c/Program\ Files\ \(x86\)/Google/Chrome/Application
➜  Application ll
总用量 1.4M
dr-xr-xr-x 0 root root  512 8月  18 08:31 60.0.3112.101
-r-xr-xr-x 1 root root 1.3M 8月  11 15:40 chrome.exe
-r-xr-xr-x 1 root root  410 8月  18 08:31 chrome.VisualElementsManifest.xml
-r-xr-xr-x 1 root root 121K 8月  14 08:41 master_preferences
drwxrwxrwx 0 root root  512 8月  25 18:14 SetupMetrics
➜  Application sudo update-alternatives --install /usr/bin/x-www-browser x-www-browser /mnt/c/Program\ Files\ \(x86\)/Google/Chrome/Application/chrome.exe 100
update-alternatives: 使用 /mnt/c/Program Files (x86)/Google/Chrome/Application/chrome.exe 来在自动模式中提供 /usr/bin/x-www-browser (x-www-browser)
➜  Application sudo update-alternatives --config x-www-browser 
链接组 x-www-browser (提供 /usr/bin/x-www-browser)中只有一个候选项:/mnt/c/Program Files (x86)/Google/Chrome/Application/chrome.exe无需配置。

jupyter notebook

此时再次执行jupyter notebook,惊喜,确实可以自动打开chrome呈现仿真目录:

➜  Algorithm_Research git:(master) jupyter notebook                               
[I 10:59:17.800 NotebookApp] Serving notebooks from local directory: /mnt/c/Users/smslit/Desktop/topscomm/Algorithm_Research
[I 10:59:17.800 NotebookApp] 0 active kernels 
[I 10:59:17.800 NotebookApp] The Jupyter Notebook is running at: http://localhost:8888/?token=2d7f588a4341b73bc65fcc6c85aac0289677123ef1b8e24c
[I 10:59:17.800 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[C 10:59:17.807 NotebookApp] 
    
    Copy/paste this URL into your browser when you connect for the first time,
    to login with a token:
        http://localhost:8888/?token=2d7f588a4341b73bc65fcc6c85aac0289677123ef1b8e24c
[I 10:59:18.359 NotebookApp] Accepting one-time-token-authenticated connection from 127.0.0.1
[W 11:02:15.429 NotebookApp] 404 GET /static/components/preact/preact.min.js.map (127.0.0.1) 32.15ms referer=None
[W 11:02:15.433 NotebookApp] 404 GET /static/components/proptypes/index.js.map (127.0.0.1) 2.30ms referer=None
[W 11:02:15.436 NotebookApp] 404 GET /static/components/preact-compat/preact-compat.min.js.map (127.0.0.1) 2.09ms referer=None