How To Set Up a JupyterLab Environment on Ubuntu 22.04

Last Updated: Mon, Oct 31, 2022
Machine Learning Server Apps Vultr Cloud GPU

JupyterLab provides a web-based development environment for Data Science and AI projects. It is a flexible interface that allows developers to configure and manage machine learning and scientific computing workflows. JupyterLab is the next generation of the Jupyter Notebook. It's designed to fix many usability issues of the Notebook, and it greatly expands its scope with support for more than 40 programming languages, including R, Python, Scala, and Julia.

This guide shows you how to set up a JupyterLab environment and secure it with Let's Encrypt SSL on Ubuntu 22.04.

Prerequisites

You'll need:

  • A fresh Vultr Ubuntu 22.04 LTS x64 server instance. For best results, we recommend a High-Frequency Compute instance with at least 2 GB of memory.

  • A non-root sudo user. Use Vultr's best practice guide to create a sudo user on Ubuntu.

  • A Fully Qualified Domain Name (FQDN) assigned to your server's IP address.

For security and performance, follow Vultr's best practices guide to update Ubuntu.

1. Set the Hostname and FQDN

You must set the hostname and FQDN before you deploy the Let's Encrypt HTTPS certificate for security. Use the Vultr's Best Practice Guide to configure your server hostname and FQDN. This guide uses the example names jupyterhost, and jupyterhost.example.com.

2. Configure Firewall Rules for JupyterLab

JupyterLab requires OpenSSH, HTTP, and HTTPS traffic, along with inbound TCP traffic on port 8888.

$ sudo ufw allow OpenSSH

$ sudo ufw allow http

$ sudo ufw allow https

$ sudo ufw allow in 8888/tcp

$ sudo ufw enable

Output:

Command may disrupt existing ssh connections. Proceed with operation (y|n)? y

When prompted to proceed, type Y and press ENTER.

3. Install Python and PIP

JupyterLab requires the Python and PIP packages. You can install both packages using the following command.

$ sudo apt install python3 python3-pip -y

After the successful installation, verify the Python version:

$ python3 --version

Output:

Python 3.10.6

Next, verify the PIP version:

$ pip --version

Output:

pip 22.0.2 from /usr/lib/python3/dist-packages/pip (python 3.10)

4. Install JupyterLab

Install the JupyterLab package using the PIP command-line utility:

$ pip3 install jupyterlab

This command will install the JupyterLab and store it inside the ~/.local/bin/.

Next, edit the ~/.bashrc file and add the JupyterLab path:

$ nano ~/.bashrc

Add the following line at the end of the file:

export PATH=$PATH:~/.local/bin/

Save the file, then activate the path variable using the following command:

$ source ~/.bashrc

5. Verify JupyterLab Installation

Start the JupyterLab instance using the following command:

$ jupyter lab --allow-root --ip=0.0.0.0 --no-browser

Output:

[I 2022-09-30 08:12:00.071 ServerApp] jupyterlab | extension was successfully linked.

[I 2022-09-30 08:12:00.084 ServerApp] nbclassic | extension was successfully linked.

[I 2022-09-30 08:12:00.086 ServerApp] Writing Jupyter server cookie secret to /home/jupyter/.local/share/jupyter/runtime/jupyter_cookie_secret

[I 2022-09-30 08:12:00.328 ServerApp] notebook_shim | extension was successfully linked.

[I 2022-09-30 08:12:00.349 ServerApp] notebook_shim | extension was successfully loaded.

[I 2022-09-30 08:12:00.351 LabApp] JupyterLab extension loaded from /home/jupyter/.local/lib/python3.10/site-packages/jupyterlab

[I 2022-09-30 08:12:00.351 LabApp] JupyterLab application directory is /home/jupyter/.local/share/jupyter/lab

[I 2022-09-30 08:12:00.355 ServerApp] jupyterlab | extension was successfully loaded.

[I 2022-09-30 08:12:00.360 ServerApp] nbclassic | extension was successfully loaded.

[I 2022-09-30 08:12:00.360 ServerApp] Serving notebooks from local directory: /home/jupyter

[I 2022-09-30 08:12:00.361 ServerApp] Jupyter Server 1.19.1 is running at:

[I 2022-09-30 08:12:00.361 ServerApp] http://jupyterhost:8888/lab?token=8dca3f363780c2dd3a400e152958a17536854f2ffb5a7e44

[I 2022-09-30 08:12:00.361 ServerApp]  or http://127.0.0.1:8888/lab?token=8dca3f363780c2dd3a400e152958a17536854f2ffb5a7e44

[I 2022-09-30 08:12:00.361 ServerApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).

[C 2022-09-30 08:12:00.364 ServerApp] 



    To access the server, open this file in a browser:

        file:///home/jupyter/.local/share/jupyter/runtime/jpserver-4428-open.html

    Or copy and paste one of these URLs:

        http://jupyterhost:8888/lab?token=8dca3f363780c2dd3a400e152958a17536854f2ffb5a7e44

     or http://127.0.0.1:8888/lab?token=8dca3f363780c2dd3a400e152958a17536854f2ffb5a7e44

Open your web browser and access the JupyterLab instance using the URL http://your-server-ip:8888/lab?token=8dca3f363780c2dd3a400e152958a17536854f2ffb5a7e44.

JupyterLab Instance

Press the CTRL+C to stop the JupyterLab instance.

6. Set a JupyterLab Configuration File and Password

By default, JupyterLab has no configuration file, and the password isn't set.

  1. First, generate a configuration file for JupyterLab.

    $ jupyter-lab --generate-config
    

    Output:

    Writing default config to: /home/jupyter/.jupyter/jupyter_lab_config.py
    
  2. Next, generate a password to secure the JupyterLab instance:

    $ jupyter-lab password
    

    This will prompt you to provide a password of your choice:

    Enter password: 
    
    Verify password: 
    
    [JupyterPasswordApp] Wrote hashed password to /home/jupyter/.jupyter/jupyter_server_config.json
    
  3. Run the cat command to view the hashed password:

    $ cat /home/jupyter/.jupyter/jupyter_server_config.json
    

    Output:

    {
    
      "ServerApp": {
    
        "password": "argon2:$argon2id$v=19$m=10240,t=10,p=8$fNZsdmvkPtZ6bPfroL8zVQ$iDEc3uY+YqLxl4tQ9MlSZQ9KxGcnJomKGtmdBAeUfQE"
    
      }
    
    }
    

Copy the generated password from the above output. You will require this hashed password in the configuration step.

7. Install Let's Encrypt SSL for JupyterLab

Let's Encrypt provides a free SSL certificate to secure your data when you access it via a web browser.

  1. Install the Certbot client package.

    $ sudo apt install certbot -y
    
  2. Generate SSL certificates to secure your JupyterLab instance.

    $ sudo certbot certonly --standalone
    

    This command will ask you to provide your email and accept the term of service:

    Saving debug log to /var/log/letsencrypt/letsencrypt.log
    
    Enter email address (used for urgent renewal and security notices)
    
     (Enter 'c' to cancel): your-email-address
    
    
    
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    
    Please read the Terms of Service at
    
    https://letsencrypt.org/documents/LE-SA-v1.3-September-21-2022.pdf. You must
    
    agree in order to register with the ACME server. Do you agree?
    
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    
    (Y)es/(N)o: Y
    
    
    
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    
    Would you be willing, once your first certificate is successfully issued, to
    
    share your email address with the Electronic Frontier Foundation, a founding
    
    partner of the Let's Encrypt project and the non-profit organization that
    
    develops Certbot? We'd like to send you email about our work encrypting the web,
    
    EFF news, campaigns, and ways to support digital freedom.
    
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    
    (Y)es/(N)o: Y
    
  3. Provide your email, type Y then press the ENTER key. This will ask you to enter your domain name:

    Account registered.
    
    Please enter the domain name(s) you would like on your certificate (comma and/or
    
    space separated) (Enter 'c' to cancel): jupyterhost.example.com
    
    Requesting a certificate for jupyterhost.example.com
    
  4. Type your FQDN domain name and press the ENTER. You should see the following output:

    Successfully received certificate.
    
    Certificate is saved at: /etc/letsencrypt/live/jupyterhost.example.com/fullchain.pem
    
    Key is saved at:         /etc/letsencrypt/live/jupyterhost.example.com/privkey.pem
    
    This certificate expires on 2022-12-29.
    
    These files will be updated when the certificate renews.
    
    Certbot has set up a scheduled task to automatically renew this certificate in the background.
    
    
    
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    
    If you like Certbot, please consider supporting our work by:
    
     * Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
    
     * Donating to EFF:                    https://eff.org/donate-le
    
    - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
    

8. Configure JupyterLab

  1. Open the JupyterLab configuration file in an editor:

    $ nano /home/jupyter/.jupyter/jupyter_lab_config.py
    
  2. Scroll down the file, find the variable c.ServerApp.certfile and c.ServerApp.keyfile, and change both values as follows:

    c.ServerApp.certfile = '/etc/letsencrypt/live/jupyterhost.example.com/fullchain.pem'
    
    c.ServerApp.keyfile = '/etc/letsencrypt/live/jupyterhost.example.com/privkey.pem'
    
  3. Find the variable c.ServerApp.ip and change it as follows:

    c.ServerApp.ip = '*'
    
  4. Find the variable c.ServerApp.open_browser and change it as follows:

    c.ServerApp.open_browser = False
    
  5. Find the variable c.ServerApp.password and change it with your hash password as follows:

    c.ServerApp.password = 'argon2:$argon2id$v=19$m=10240,t=10,p=8$fNZsdmvkPtZ6bPfroL8zVQ$iDEc3uY+YqLxl4tQ9MlSZQ9KxGcnJomKGtmdBAeUfQE'
    
  6. Find the variable c.ServerApp.port and change it as follows:

    c.ServerApp.port = 8888
    
  7. Save and close the file when you are finished.

  8. Change the permission of the Let's Encrypt directory.

    $ sudo chmod 750 -R /etc/letsencrypt
    
  9. Change the ownership of the Let's Encrypt directory.

    $ sudo chown jupyter:jupyter -R /etc/letsencrypt
    

9. Create a Systemd Service for JupyterLab

Systemd service allows JupyterLab to keep running in the background. Even you don't need to start it after the system reboot.

  1. Use the nano command to create a systemd service file.

    $ sudo nano /etc/systemd/system/jupyter-lab.service
    

    Add the following configurations:

    [Service]
    
    Type=simple
    
    PIDFile=/run/jupyter.pid
    
    WorkingDirectory=/home/jupyter/
    
    ExecStart=/home/jupyter/.local/bin/jupyter-lab --config=/home/jupyter/.jupyter/jupyter_lab_config.py
    
    User=jupyter
    
    Group=jupyter
    
    Restart=always
    
    RestartSec=10
    
    [Install]
    
    WantedBy=multi-user.target
    

    Save and close the file when you are done.

  2. Reload the systemd daemon to apply the configuration.

    $ sudo systemctl daemon-reload
    
  3. Start the JupyterLab service.

    $ sudo systemctl start jupyter-lab.service
    
  4. Enable the JupyterLab service to start at system reboot.

    $ sudo systemctl enable jupyter-lab.service
    
  5. Verify the JupyterLab service status.

    $ sudo systemctl status jupyter-lab.service
    

    Output:

    ● jupyter-lab.service
    
         Loaded: loaded (/etc/systemd/system/jupyter-lab.service; disabled; vendor preset: enabled)
    
         Active: active (running) since Fri 2022-09-30 08:30:16 UTC; 12s ago
    
       Main PID: 5460 (jupyter-lab)
    
          Tasks: 1 (limit: 2242)
    
         Memory: 51.5M
    
            CPU: 932ms
    
         CGroup: /system.slice/jupyter-lab.service
    
                 └─5460 /usr/bin/python3 /home/jupyter/.local/bin/jupyter-lab --config=/home/jupyter/.jupyter/jupyter_lab_config.py
    
    
    
    Sep 30 08:30:17 jupyterhost jupyter-lab[5460]:     [I 2022-09-30 08:30:17.163 ServerApp] notebook_shim | extension was successfully  loaded.
    
    Sep 30 08:30:17 jupyterhost jupyter-lab[5460]:     [I 2022-09-30 08:30:17.165 LabApp] JupyterLab extension loaded from /home/ jupyter/.local/lib/python3.1>
    
    Sep 30 08:30:17 jupyterhost jupyter-lab[5460]:     [I 2022-09-30 08:30:17.165 LabApp] JupyterLab application directory is /home/ jupyter/.local/share/jupy>
    
    Sep 30 08:30:17 jupyterhost jupyter-lab[5460]:     [I 2022-09-30 08:30:17.169 ServerApp] jupyterlab | extension was successfully loaded.
    
    Sep 30 08:30:17 jupyterhost jupyter-lab[5460]:     [I 2022-09-30 08:30:17.174 ServerApp] nbclassic | extension was successfully loaded.
    
    Sep 30 08:30:17 jupyterhost jupyter-lab[5460]:     [I 2022-09-30 08:30:17.175 ServerApp] Serving notebooks from local directory: /home/jupyter
    
    Sep 30 08:30:17 jupyterhost jupyter-lab[5460]:     [I 2022-09-30 08:30:17.175 ServerApp] Jupyter Server 1.19.1 is running at:
    
    Sep 30 08:30:17 jupyterhost jupyter-lab[5460]:     [I 2022-09-30 08:30:17.175 ServerApp] https://localhost:8888/lab
    
    Sep 30 08:30:17 jupyterhost jupyter-lab[5460]:     [I 2022-09-30 08:30:17.176 ServerApp]  or https://127.0.0.1:8888/lab
    
    Sep 30 08:30:17 jupyterhost jupyter-lab[5460]:     [I 2022-09-30 08:30:17.176 ServerApp] Use Control-C to stop this server and shut down all kernels (twi>
    
    lines 1-20/20 (END)
    
  6. Verify the JupyterLab listening port:

    $ ss -antpl | grep jupyter
    

    Output:

    LISTEN 0      128          0.0.0.0:8888      0.0.0.0:*    users:(("jupyter-lab",pid=5460,fd=7))
    
    LISTEN 0      128             [::]:8888         [::]:*    users:(("jupyter-lab",pid=5460,fd=8))
    

10. Access the JupyterLab Instance

Open your favorite web browser and access the JupyterLab instance securely using the URL https://jupyterhost.example.com:8888/. This will ask you to provide your JupyterLab password.

Jupyter Lab Login Screen

Enter your password and click on the Log In. This will redirect you to the JupyterLab dashboard screen:

Jupyter Lab Dashboard

More Information

See the JupyterLab documentation for more information.

Want to contribute?

You could earn up to $600 by adding new articles.