Setting up remote JupyterLab in Raspberry Pi
Jupyter is an interactive development environment developed and born out of the IPython project. Since 2014, the Ipython project has grown to become one of the most popular tools in the data science.
What the Jupyter project adds is the integration of notebooks, documents and activities in a powerful interface that allows for an iterative process while writing code. It is a god send for the hobbyist developper and a perfect way to build prototypes.
This is a miny guide to set up a jupyter environment in a remote server, which can be helpful when seting up remote development environments (think monitoring projects, synchronisation services, relays, etc). Me, I am just using a Raspberry Pi as an example.
Connecting to your pi
You will first connect to your raspbery pi via ssh from your local machine. Open a terminal and type :
$ ssh username@piaddress
Replace username with the name of the user (pi by default). Replace piaddress with your rpi IP address. Once connected we will go on to set some essentials. We will use the $ to denote a bash command.
1. Set up the basics
Considering this is a brand new system, let’s start by getting our packages up to date and upgrading to the latest Raspbian distribution. Run this three commands one at the time.
$ sudo apt-get update
$ sudo apt-get upgrade-dist
$ sudo apt-get clean
Install jupyterlab
All done, now we can get to install the necessary packages. Let’s checkup with versions of Python your system has. Raspbian comes with both python2 and python3 pre-installed.
# tells you which version of python3 is installed.
$ python3 --version
$ pip install jupyterlab # install jupyter
As of the time of writing, we need to add all pip executables to our path variable to ensure our systems sees our jupyter installation.
$ export PATH=$PATH:~/.local/bin
Jupyterlab needs nodejs and npm to track web status, install it by running :
$ sudo apt-get install nodejs npm
Extras : install the zsh shell
Zsh is an extended Bourne shell with many improvements, including some features of Bash, ksh, and tcsh. A more powerful alternative to the standard shell.
# Install and enable zsh shell,
# you will need to relogin to see this one take effect.
$ sudo apt-get install zsh
$ chsh -s $(which zsh)
Run the lab server
In order to acces a remote session we will need to set up a password. This will be stored in our jupyter_notebook_config.py file which you can later find in ~/.jupyter.
$ jupyter notebook password
>> Enter password : # follow prompt and enter a new password
Finally, you start the server in port 9999
$ jupyter lab --port=9999 --no-browser
That's all good, you now have a remote server of Jupyter running and listening at port 9999 of your raspberry pi. You can stop it with ctrl-c for now.
2. Configure jupyter lab to run on start-up
Now for the cool stuff, we can configure our remote machine to automatically run jupyter lab on start-up. To do so, we will create a simple sh script file that runs the last command above. We will then create a service to run this file on system start-up. This "service" is just jargon for a program running in the background. All this is managed using the systemd suite.
Systemd
Systemd is designed to manage the complex demands of modern Linux systems by dynamically managing all processes from system boot to shutdown. It is the modern replacement for the legacy Linux initialization systems and scheduled execution (cron).
# create folder called startup-scripts in user folder
$ mkdir ~/startup-scripts
# create new file named jupyterlab.sh
$ touch ~/startup-scripts/jupyterlab.sh
The content of your jupyterlab.sh file should look like this
#!/bin/bash
export PATH=$PATH:~/.local/bin
cd ~
jupyter lab --port=9999 --no-browser
# notice the shebang (first line with "!") telling that
# the file must be runned by a bash shell.
# Change it accordingly if you are using bash or zsh as a default.
Now we need to create our service file. Under the directory /usr/lib/systemd/system/, create a new file named jupyterlab.service :
$ cd /usr/lib/systemd/system/
$ touch jupyterlab.service
This new file jupyterlab.service should look like this:
# /usr/lib/systemd/system/jupyterlab.service
[Unit]
description="Jupyter Lab start-up service"
[Service]
Type=simple
ExecStart=/home/pi/startup-scripts/jupyterlab.sh
User=pi # important so that we don't run as root !
WorkingDirectory="~"
Restart=always
RestartSec=10
[Install]
WantedBy=multi-user.target
Finally, activate and start the new service. This will basically run the jupyterlab.sh command in the background for you.
$ sudo systemctl enable jupyterlab.service # will create links
$ sudo systemctl start jupyterlab.service # will start the service
Optional : You can check the status of your service using the systemctl status command anytime
$ systemctl status jupyterlab.service
There you go, you explored how to set up a jupyter service in a remote service and experience the workings of the new systemd managing system.