Jupyter (JupyterLab) Unattended Service

Create a New Python Environment

python3 -m venv /opt/jupyterlab

to install venv, execute apt install python3-venv.

Install JupyterLab

source /opt/jupyterlab/bin/activate
pip3 install jupyterlab

Create a Start-up Script

My start.sh which I’ve put into /opt/jupyterlab:

#!/usr/bin/env bash

source bin/activate

export SPARK_HOME=/opt/spark
export PATH=$PATH:$SPARK_HOME/bin:$SPARK_HOME/sbin
export PYSPARK_PYTHON=/opt/jupyterlab/bin/python3
export PYSPARK_DRIVER_PYTHON='jupyter'
export PYSPARK_DRIVER_PYTHON_OPTS='lab --config config.py --notebook-dir=~/notebooks/'

pyspark

This assumes Apache Spark integration - pyspark command actually starts pyspark with Jupyter notebooks, hence those environment variables above. I’m assuming Spark is installed in /opt/spark and is running already.

config.py contains a few config values:

# disable identity tokens, as we are running on a server unattended
c.IdentityProvider.token = ''

Hint: to make Spark reference certain packages on start, create spark-default.conf in /opt/spark/conf. Mine looks like this:

spark.jars.packages=org.apache.hadoop:hadoop-azure:3.2.1,io.delta:delta-core_2.12:2.1.1

SystemD Unit

My working unit file:

[Unit]
Description=Personal JupyterLab
Wants=network-online.target
After=network-online.target

[Service]
User=alg
Group=alg
Type=simple
WorkingDirectory=/opt/jupyterlab
ExecStart=/opt/jupyterlab/start.sh
Restart=always
RestartSec=10

[Install]
WantedBy=multi-user.target

Thanks! You can always email me or use contact form for more questions/comments etc.