🥦

WSL2 に Jupyter Lab を入れて Windows のブラウザで動かす

2023/10/07に公開

Jupyter のインストール

Ubuntuを想定。WSL上で以下を実行。Python3はデフォルトで入っていたのでそのまま使用

$ python3 -V
Python 3.10.12

$ sudo apt install -y python3-pip

$ pip install jupyterlab

パスを通す

$ ls ~/.local/bin/ | grep jupyter
jupyter
jupyter-dejavu
jupyter-events
jupyter-execute
jupyter-kernel
jupyter-kernelspec
jupyter-lab
jupyter-labextension
jupyter-labhub
jupyter-migrate
jupyter-nbconvert
jupyter-run
jupyter-server
jupyter-troubleshoot
jupyter-trust

.bashrc に以下の行を追加

~/.bashrc
export PATH="$HOME/.local/bin:$PATH"

これで jupyter コマンドが使えます。

Windows側のブラウザを自動起動させる

上記までを行なうと

$ jupyter lab

を実行できるようになりますが、WSL上で実行している関係でWindows側のブラウザが自動で立ち上がりません。

なので、jupyter lab 立ち上げ時の設定を変更することにします。
まずは設定ファイルを生成します。

$ jupyter lab --generate-config

これで ~/.jupyter/jupyter_lab_config.py ファイルが生成されます。
あとは、以下を変更します。

$ vi ~/.jupyter/jupyter_lab_config.py
~/.jupyter/jupyter_lab_config.py
# c.ServerApp.use_redirect_file = True
c.ServerApp.use_redirect_file = False

# c.ServerApp.browser = ''
c.ServerApp.browser = '/mnt/c/Program\ Files\ \(x86\)/Microsoft/Edge/Application/msedge.exe %s'

これで jupyter lab コマンドでブラウザが自動で立ち上がるようになりました。

参考
https://github.com/jupyter/notebook/issues/4594

Discussion