Jupyter notebook as a service on Ubuntu 18.04 with Python 3

How amazing would it be to start your computer, visit localhost:8888, and  know that your Jupyter Notebook is waiting for you, up and running ? Well here is how to make this dream a reality on Ubuntu 18.04 with Python 3.


Step 1: Install Jupyter Notebook


Install pip3 and the python header files

sudo apt update 
sudo apt install python3-pip python3-dev

I also want the packages I install in my notebook not to be in conflict with the other python packages installed on the system, so I’m creating a virtual environment for Jupyter only. If the 2 lines of code below don’t work for you, check out this post for the full instructions.

virtualenv -p python3 notebook-env
. notebook-env/bin/activate

Your command line prompt should now be preceded by (notebook-env) to signal that you are working in the virtual environment you just created. As you will see by typing

(notebook-env) naysan@hp:~$ which pip3

From now on (until you deactivate the environment), the pip3 executable you will call is the one from your notebook environment. Use this pip3 binary to install jupyter

(notebook-env) naysan@hp:~$ pip3 install jupyter

Launch jupyter 

(notebook-env) naysan@hp:~$ jupyter-notebook


This command should open your browser at  localhost:8888. Now I don’t know about you, but since I launched the notebook from my home directory, there are a bunch of directories listed in the notebook interface that have nothing to do with where I’d like to store my python notebooks. Here is a way to dedicate a clean, separate directory for your Jupyter experiments.

Shut down your kernel with Ctrl+C, then

# Back to the HOME directory
(notebook-env) naysan@hp:~$ cd

# Create a clean new directory dedicated to your notebooks
(notebook-env) naysan@hp:~$ mkdir my-notebooks

# Use this directory as your --notebook-dir argment as you re-launch jupyter-notebook
(notebook-env) naysan@hp:~$ jupyter-notebook --notebook-dir=/home/naysan/my-notebooks

Much cleaner, right?


Step 2: Setup Jupyter-notebook as a service


This step is made easier if we setup a password for the notebook beforehand, otherwise Jupyter will ask you to use a very long token to access it. Shut down your kernel with Ctrl+C, then

(notebook-env) naysan@hp:~$ jupyter notebook password
Enter password: **********
Verify password: **********
[NotebookPasswordApp] Wrote hashed password to /home/naysan/.jupyter/jupyter_notebook_config.json


Locate your jupyter-notebook binary

(notebook-env) naysan@hp:~$ which jupyter-notebook 

Create a file named jupyter.service that contains the following content. Don’t forget to replace my personal info – like my user name, Naysan – with what works  for you

Description=Jupyter Notebook

ExecStart=/bin/bash -c ". /home/naysan/notebook-env/bin/activate;jupyter-notebook --notebook-dir=/home/naysan/my-notebooks"



It is now time to install the service

(notebook-env) naysan@hp:~$ sudo cp jupyter.service /etc/systemd/system/

# Use the enable command to ensure that the service starts whenever the system boots
(notebook-env) naysan@hp:~$ sudo systemctl enable jupyter.service

(notebook-env) naysan@hp:~$ sudo systemctl daemon-reload
(notebook-env) naysan@hp:~$ sudo systemctl start jupyter.service
(notebook-env) naysan@hp:~$ systemctl status jupyter.service 
● jupyter.service - Jupyter Notebook
   Loaded: loaded (/etc/systemd/system/jupyter.service; enabled; vendor preset: enabled)
   Active: active (running) since Sat 2019-09-07 12:18:08 EDT; 53min ago
 Main PID: 32316 (bash)
    Tasks: 2 (limit: 4915)
   CGroup: /system.slice/jupyter.service
           ├─32316 /bin/bash -c . /home/naysan/notebook-env/bin/activate;jupyter-notebook --notebook-dir=/home/naysan/my-notebooks
           └─32319 /home/naysan/notebook-env/bin/python3 /home/naysan/notebook-env/bin/jupyter-notebook --notebook-dir=/home/naysan/my-notebooks

Sep 07 12:18:08 hp bash[32316]: [I 12:18:08.473 NotebookApp] http://localhost:8888/

Step 3: Witness the magic

Visit http://localhost:8888/

Enter your password, and voilà, you are all set !