JupyterLab is a web-based interactive development environment used for work in data science, numerical computing, machine learning, and similar fields. “Jupyter” is a portmanteau of Julia, Python, and R.

1. Installation

There are two approaches: installing it directly on the machine, or via Docker.

1.1 Direct installation

Just use a Python package manager — either pip or conda works.

python3 -m pip install --user --upgrade jupyterlab

1.2 Using Docker

First install Docker — see the other relevant sections of this site. Then pull the corresponding Docker image; see here for details too.

docker pull jupyter/datascience-notebook

2. Configuring JupyterLab

2.1 Managing a locally installed Jupyter with systemctl

Create a jupyter.service file at /etc/systemd/system/jupyter.service:

[Unit]
Description=Jupyter Lab
After=syslog.target network.target

[Service]
User=your_username
Group=your_groupname
Environment="PATH=/usr/local/bin:/usr/bin:/bin"
ExecStart=/usr/bin/jupyter lab --ip=0.0.0.0 --port=8888 --no-browser
WorkingDirectory=/home/your_username/
Restart=always
RestartSec=10
KillMode=mixed

[Install]
WantedBy=multi-user.target

Note that you need to replace the User and Group fields with your own username and group name. The WorkingDirectory field should point to the directory you want Jupyter Lab to run from. Manage the service with:

sudo systemctl daemon-reload
sudo systemctl start jupyter
sudo systemctl enable jupyter

Configure your firewall rules. If a firewall is enabled on your server, make sure the port Jupyter Lab uses (8888 by default) is open.

2.2 Configuring it with Docker

Create a folder to hold JupyterLab’s configuration files, for example:

mkdir jupyterlab_config

In a terminal, pull the JupyterLab Docker image:

docker pull jupyter/scipy-notebook

This pulls the scipy-notebook build of JupyterLab from Docker Hub. You can choose a different build instead, such as jupyter/datascience-notebook or jupyter/minimal-notebook — see Docker Hub’s documentation for details. Start the JupyterLab container with:

docker run -p 8888:8888 -v /path/to/jupyterlab_config:/home/jovyan/.jupyter jupyter/scipy-notebook

This starts the JupyterLab container, maps local port 8888 to the container’s port 8888, and stores JupyterLab’s configuration files in the local /path/to/jupyterlab_config folder.

Access JupyterLab in a browser by going to http://localhost:8888 and pressing Enter. You’ll see JupyterLab’s login page. For authentication, use the token, which you can find in the container’s logs, or retrieve with:

docker logs <container_name_or_id>

In the container logs you’ll find a token that looks something like:

http://127.0.0.1:8888/?token=xxxxxxxxxxxx

Copy the token and paste it into the login page.

Once that’s done, you can use your own Python code and libraries in JupyterLab.

3. Security Features

Mainly, setting a password and enabling 2FA.

3.1 Setting a password

To set a password for JupyterLab, run:

jupyter notebook password

Then follow the prompt to enter your desired password. The password will be stored, hashed, in your JupyterLab configuration file.

3.2 Configuring 2FA

To enable 2FA for JupyterLab, use the jupyter-server-mfa extension. Here are the basic steps:

python3 -m pip install jupyter-server-mfa

Generate and configure a shared secret. The following command generates a 64-byte random key:

jupyter mfa-keygen

Store the generated key somewhere safe, and add it to your JupyterLab configuration file:

c.ServerMFA.shared_secret = b'your_secret_key'

Configure JupyterLab. To enable 2FA, add the following lines to your JupyterLab configuration file:

c.ServerApp.authenticator_class = 'jupyter_server_mfa.MFAAuthenticator'
c.ServerApp.disable_check_xsrf = True

Restart JupyterLab, then log in with your username and password. After logging in, you’ll be prompted for a TOTP (time-based one-time password) code, which you can generate with Google Authenticator or a similar app.

Hopefully these steps help you set a password or enable 2FA in JupyterLab. Keep in mind that security measures like strong passwords and 2FA help protect your system and data.