WSL

Install JupyterLab or Jupyter Notebook in Windows Subsystem for Linux (WSL2)

· 6 min read
Install JupyterLab or Jupyter Notebook in Windows Subsystem for Linux (WSL2)

Web-based interactive development environment JupyterLab (the next generation of Jupyter Notebook ) is very popular in the field of data science and machine learning. Windows Subsystem for Linux (WSL2) is well known for super-fast boot time, less resource consumption and seamless integration of Linux guest on the Windows Host platform without any traditional Virtual Machine (e.g. VMware, Oracle virtual box). In this post, we'll see how to install JupyterLab or Jupyter Notebook in WSL and access the web-based Notebook interface with any browser from a Windows machine.

Pre-requisites:

In this article, we'll use pip (package installer for Python) to install JupyterLab or Jupyter Notebook. Linux distribution that you are running may or may not come with Python or pip (package installer for Python) installed by default. In my case, Python3 came preinstalled for Ubuntu 22 distro.  

abhijit@AwsJunkie:~$ uname -r
5.10.102.1-microsoft-standard-WSL2

abhijit@AwsJunkie:~$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 22.04.1 LTS
Release:        22.04
Codename:       jammy

abhijit@AwsJunkie:~$ python3 --version
Python 3.10.6

Up-to-date current packages.

sudo apt update -y && sudo apt upgrade -y

Install python3 and pip.

abhijit@AwsJunkie:~$ sudo apt install python3 python3-pip
Reading package lists... Done
Building dependency tree... Done
Reading state information... Done
python3 is already the newest version (3.10.6-1~22.04).
python3 set to manually installed.
The following NEW packages will be installed:
  python3-pip
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.

Note: If python3 was already there preinstalled then you will see "python3 set to manually installed.". But if you are concerned, you can mark it as automatically installed again by executing below command.

abhijit@AwsJunkie:~$ sudo apt-mark auto python3
python3 set to automatically installed.

Install JupyterLab / Jupyter Notebook

To install JupyterLab run pip install jupyterlab and for Jupyter Notebook, run pip install notebook.  I'll recommend installing JupyterLab as it is next generation Notebook interface with more flexibility and functionalities.

abhijit@AwsJunkie:~$ pip install jupyterlab
Defaulting to user installation because normal site-packages is not writeable
Collecting jupyterlab
  Downloading jupyterlab-3.5.3-py3-none-any.whl (8.8 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 8.8/8.8 MB 4.1 MB/s eta 0:00:00
:
:
Successfully installed MarkupSafe-2.1.2 anyio-3.6.2 argon2-cffi-21.3.0 argon2-cffi-bindings-21.2.0 arrow-1.2.3 asttokens-2.2.1 attrs-22.2.0 babel-2.11.0 backcall-0.2.0 beautifulsoup4-4.11.2 bleach-6.0.0 certifi-2022.12.7 cffi-1.15.1 charset-normalizer-3.0.1 comm-0.1.2 debugpy-1.6.6 decorator-5.1.1 defusedxml-0.7.1 executing-1.2.0 fastjsonschema-2.16.2 fqdn-1.5.1 idna-3.4 ipykernel-6.20.2 ipython-8.9.0 ipython-genutils-0.2.0 isoduration-20.11.0 jedi-0.18.2 jinja2-3.1.2 json5-0.9.11 jsonpointer-2.3 jsonschema-4.17.3 jupyter-client-8.0.2 jupyter-core-5.2.0 jupyter-events-0.6.3 jupyter-server-2.2.0 jupyter-server-terminals-0.4.4 jupyterlab-3.5.3 jupyterlab-pygments-0.2.2 jupyterlab-server-2.19.0 matplotlib-inline-0.1.6 mistune-2.0.4 nbclassic-0.5.1 nbclient-0.7.2 nbconvert-7.2.9 nbformat-5.7.3 nest-asyncio-1.5.6 notebook-6.5.2 notebook-shim-0.2.2 packaging-23.0 pandocfilters-1.5.0 parso-0.8.3 pexpect-4.8.0 pickleshare-0.7.5 platformdirs-2.6.2 prometheus-client-0.16.0 prompt-toolkit-3.0.36 psutil-5.9.4 ptyprocess-0.7.0 pure-eval-0.2.2 pycparser-2.21 pygments-2.14.0 pyrsistent-0.19.3 python-dateutil-2.8.2 python-json-logger-2.0.4 pytz-2022.7.1 pyzmq-25.0.0 requests-2.28.2 rfc3339-validator-0.1.4 rfc3986-validator-0.1.1 send2trash-1.8.0 sniffio-1.3.0 soupsieve-2.3.2.post1 stack-data-0.6.2 terminado-0.17.1 tinycss2-1.2.1 tomli-2.0.1 tornado-6.2 traitlets-5.9.0 uri-template-1.2.0 urllib3-1.26.14 wcwidth-0.2.6 webcolors-1.12 webencodings-0.5.1 websocket-client-1.5.0

Launch JupyterLab / Jupyter Notebook

We'll access the Notebook interface remotely with a browser running in Windows Host. So, launch the Notebook server running on WSL with --no-browser flag.  If you get jupyter-lab: command not found then use the full path ~/.local/bin/jupyter-lab

abhijit@AwsJunkie:~$ jupyter-lab --no-browser
jupyter-lab: command not found

abhijit@AwsJunkie:~$ ~/.local/bin/jupyter-lab --no-browser
[I 2023-02-01 22:25:06.900 ServerApp] Package jupyterlab took 0.0000s to import
[I 2023-02-01 22:25:06.904 ServerApp] Package jupyter_server_terminals took 0.0032s to import
[I 2023-02-01 22:25:06.904 ServerApp] Package nbclassic took 0.0000s to import
[W 2023-02-01 22:25:06.905 ServerApp] A `_jupyter_server_extension_points` function was not found in nbclassic. Instead, a `_jupyter_server_extension_paths` function was found and will be used for now. This function name will be deprecated in future releases of Jupyter Server.
[I 2023-02-01 22:25:06.906 ServerApp] Package notebook_shim took 0.0000s to import
[W 2023-02-01 22:25:06.906 ServerApp] A `_jupyter_server_extension_points` function was not found in notebook_shim. Instead, a `_jupyter_server_extension_paths` function was found and will be used for now. This function name will be deprecated in future releases of Jupyter Server.
[I 2023-02-01 22:25:06.909 ServerApp] jupyter_server_terminals | extension was successfully linked.
[I 2023-02-01 22:25:06.914 ServerApp] jupyterlab | extension was successfully linked.
[I 2023-02-01 22:25:06.917 ServerApp] nbclassic | extension was successfully linked.
[I 2023-02-01 22:25:07.068 ServerApp] notebook_shim | extension was successfully linked.
[I 2023-02-01 22:25:07.109 ServerApp] notebook_shim | extension was successfully loaded.
[I 2023-02-01 22:25:07.109 ServerApp] jupyter_server_terminals | extension was successfully loaded.
[I 2023-02-01 22:25:07.110 LabApp] JupyterLab extension loaded from /home/abhijit/.local/lib/python3.10/site-packages/jupyterlab
[I 2023-02-01 22:25:07.110 LabApp] JupyterLab application directory is /home/abhijit/.local/share/jupyter/lab
[I 2023-02-01 22:25:07.112 ServerApp] jupyterlab | extension was successfully loaded.
[I 2023-02-01 22:25:07.114 ServerApp] nbclassic | extension was successfully loaded.
[I 2023-02-01 22:25:07.115 ServerApp] Serving notebooks from local directory: /home/abhijit
[I 2023-02-01 22:25:07.115 ServerApp] Jupyter Server 2.2.0 is running at:
[I 2023-02-01 22:25:07.115 ServerApp] http://localhost:8888/lab?token=04c6177214d7aecf5eba9056a947177f5d829cc573f632e6
[I 2023-02-01 22:25:07.115 ServerApp]  or http://127.0.0.1:8888/lab?token=04c6177214d7aecf5eba9056a947177f5d829cc573f632e6
[I 2023-02-01 22:25:07.115 ServerApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[C 2023-02-01 22:25:07.118 ServerApp]

    To access the server, open this file in a browser:
        file:///home/abhijit/.local/share/jupyter/runtime/jpserver-9768-open.html
    Or copy and paste one of these URLs:
        http://localhost:8888/lab?token=04c6177214d7aecf5eba9056a947177f5d829cc573f632e6
     or http://127.0.0.1:8888/lab?token=04c6177214d7aecf5eba9056a947177f5d829cc573f632e6

Verify

By default, the Notebook server will be launched at http://localhost:8888/. Just copy and paste one of the URLs printed in the console. For example, http://localhost:8888/lab?token=04c6177214d7aecf5eba9056a947177f5d829cc573f632e6

Create one Notebook (File --> New --> Notebook)

Install JupyterLab or Jupyter Notebook in Windows Subsystem for Linux (WSL2)

Type print('Hello World!') in the first cell and run (Shift + Enter). It will print "Hello World!".

Optional

We can cerate an alias for  ~/.local/bin/jupyter-lab --no-browser and append it in .bashrc to avoid typing long command each time we launch JupyterLab / Jupyter Notebook.

abhijit@AwsJunkie:~$ sudo nano ~/.bashrc

Add following alias at the end of .bashrc file.

#Jupyter-lab  with no-browser flag
alias jupyter-lab="~/.local/bin/jupyter-lab --no-browser"

Reload .bashrc

source ~/.bashrc

Verify

abhijit@AwsJunkie:~$ jupyter-lab
[I 2023-02-01 23:04:20.037 ServerApp] Package jupyterlab took 0.0000s to import
[I 2023-02-01 23:04:20.041 ServerApp] Package jupyter_server_terminals took 0.0035s to import
[I 2023-02-01 23:04:20.041 ServerApp] Package nbclassic took 0.0000s to import

References