How to setup Jupyter Notebook on Raspberry Pi 3 with Raspbian Stretch Lite, Supervisor and Virtualenv to run Python 3 codes

By taking Andrew Ng's Deep Learning Coursera course, you can get a better understanding of deep learning. In addition, you will also get many Jupyter Notebook documents that you can reference to build models that can solve interesting problems.

In order to readily access these documents, you may want to setup Jupyter Notebook on a single board computer like the Raspberry Pi 3. With this in mind, this post shows how you can setup Jupyter Notebook on Raspberry Pi 3 with Raspbian Stretch Lite, Supervisor and Virtualenv to run Python 3 codes.

Recommended hardware

So what can you buy for this Raspberry Pi 3 project?

For the purpose of setting up Jupyter Notebook to readily access your documents, your can consider getting the following items:

Setting up Raspbian Stretch Lite on Raspberry Pi 3 to run Python 3 applications

Once you got the hardware, proceed to setup Raspbian Stretch Lite on Raspberry Pi 3 for running Python 3 applications.

After that, you will get your Raspberry Pi 3 running the Raspbian Stretch Lite operating system with the following software pieces:

  • SSH server that enables you to configure Raspbian Stretch Lite from another computer in your home network.
  • Python 3 for running Jupyter Notebook.
  • Python 3 package manager (pip3) for installing Jupyter Notebook.
  • Virtualenv for you to create an isolated environment for running Jupyter Notebook.

Keep your remote session to your Raspbian Stretch Lite to continue with the rest of the setup.

Installing Supervisor

Supervisor is a convenient tool for running an application as a server daemon. Therefore, we can use it to keep an instance of Jupyter Notebook running as a server daemon. With this in mind, let's install Supervisor on your Raspbian Stretch Lite with the following command:

sudo apt-get install supervisor -y

After that, run the following command to verify your installation:

sudo systemctl status supervisor

Once you had run the command, you should see output similar to the following:

● supervisor.service - Supervisor process control system for UNIX
   Loaded: loaded (/lib/systemd/system/supervisor.service; enabled; vendor preset: enabled)
   Active: active (running) since Sun 2018-05-06 14:41:12 +08; 3s ago
     Docs: http://supervisord.org
 Main PID: 18293 (supervisord)
   CGroup: /system.slice/supervisor.service
           └─18293 /usr/bin/python /usr/bin/supervisord -n -c /etc/supervisor/supervisord.conf
 
May 06 14:41:12 raspberrypi systemd[1]: Started Supervisor process control system for UNIX.
May 06 14:41:14 raspberrypi supervisord[18293]: 2018-03-03 13:52:14,741 CRIT Supervisor running as root (no user in config file)
May 06 14:41:14 raspberrypi supervisord[18293]: 2018-03-03 13:52:14,743 WARN No file matches via include "/etc/supervisor/conf.d/*.conf"
May 06 14:41:14 raspberrypi supervisord[18293]: 2018-03-03 13:52:14,794 INFO RPC interface 'supervisor' initialized
May 06 14:41:14 raspberrypi supervisord[18293]: 2018-03-03 13:52:14,796 CRIT Server 'unix_http_server' running without any HTTP authentication checking
May 06 14:41:14 raspberrypi supervisord[18293]: 2018-03-03 13:52:14,797 INFO supervisord started with pid 18293

Creating a directory to keep your Jupyter Notebook documents

Once you had installed Supervisor, you can proceed with creating a directory to keep your Jupyter Notebook documents. For example, you can run the following command to create the jupyter-notebook directory inside the home directory:

mkdir ~/jupyter-notebook

Creating a virtual environment and install Jupyter Notebook

Once you had created the directory to keep your Jupyter Notebook, proceed to create the virtual environment for Jupyter Notebook:

virtualenv -p python3 ~/jupyter-env

After that, run the following commands to install Jupyter Notebook into the virtual environment:

source ~/jupyter-env/bin/activate
pip install jupyter
deactivate

After all these commands had completed, you will be able to run Jupyter Notebook from the virtual environment.

Creating a shell script to run Jupyter Notebook within the virtual environment

Once you had created the virtual environment with Jupyter Notebook installed, proceed to create a shell script to run Jupyter Notebook. To do so, run the following command to create a shell script at ~/run-jupyter-notebook.sh:

sudo nano ~/jupyter-notebook/run-jupyter-notebook.sh

And inside the editor, create the following content:

#!/bin/bash
source /home/pi/jupyter-env/bin/activate
jupyter notebook --ip 0.0.0.0 --port 9999 --no-browser
deactivate

Once you had saved the file, make the file executable by running the following command:

sudo chmod +x ~/jupyter-notebook/run-jupyter-notebook.sh

Creating the Supervisor configuration file to run Jupyter Notebook

In order to get Supervisor to run Jupyter Notebook, you need to create a Supervisor configuration file.

Run the following command to create a configuration file at /etc/supervisor/conf.d/jupyter-notebook.conf:

sudo nano /etc/supervisor/conf.d/jupyter-notebook.conf

And inside the editor, create the following content:

[program:jupyter-notebook]
directory=/home/pi/jupyter-notebook
command=/bin/bash -E -c ./run-jupyter-notebook.sh
autostart=true
autorestart=true
stopsignal=INT
stopasgroup=true
killasgroup=true
user=pi

Once you had saved /etc/supervisor/conf.d/jupyter-notebook.conf, run the following command to restart Supervisor:

sudo systemctl restart supervisor.service

Accessing Jupyter Notebook for the first time

When you access Jupyter Notebook for the first time, you need to provide a token that Jupyter Notebook had generated.

In order to get the token, get into supervisorctl with the following command:

sudo supervisorctl

And inside supervisorctl, print the log generated by Jupyter Notebook by running the following command:

tail jupyter-notebook stdout

This will print output similar to the following:

[I 21:15:48.344 NotebookApp] Serving notebooks from local directory: /home/pi/jupyter-notebook
[I 21:15:48.344 NotebookApp] 0 active kernels
[I 21:15:48.345 NotebookApp] The Jupyter Notebook is running at:
[I 21:15:48.345 NotebookApp] http://0.0.0.0:9999/?token=8f819c94ddbb9286271735fc9ab78838c6c81748d206ce37
[I 21:15:48.346 NotebookApp] Use Control-C to stop this server and shut down all kernels (twice to skip confirmation).
[C 21:15:48.348 NotebookApp] 

The fourth line contains the token that you can use to access Jupyter Notebook for the first time.

Enter http://[your-raspberry-pi-ip]:9999/?token=[token] in your browser. For example, if your Raspberry Pi 3 IP address is 192.168.1.109, you will enter http://192.168.1.109:9999/?token=8f819c94ddbb9286271735fc9ab78838c6c81748d206ce37 based on the above output.

Once you had enter the URL into your browser, you will see a similar screen as the following:

Jupyter Notebook page with coursera deep learning folder

With that, you will be able to readily access your Jupyter Notebook documents whenever you need to.

Buying the Raspberry Pi 3 hardware to host your own Jupyter Notebook

If you do not have the Raspberry Pi 3 hardware mentioned in this post yet, you may want to purchase them from Amazon. Simply click on the button below to add the Raspberry Pi 3 hardware to your cart. You may remove anything that you already have or replace some of the items with other items.


About Clivant

Clivant a.k.a Chai Heng enjoys composing software and building systems to serve people. He owns techcoil.com and hopes that whatever he had written and built so far had benefited people. All views expressed belongs to him and are not representative of the company that he works/worked for.