When you want to do some experimentation or put together a simple code-based presentation Jupyter notebooks are a powerful tool to have at your disposal. But if you use a number of devices over a few locations it can be useful to have a single instance hosted somewhere central (Linode, Digital Ocean, wherever) that you can access from any device wherever you are. There are a handful of ways that you can achieve this:
- log in to your remote machine, set Jupyter up and run jupyter notebook (perhaps in a tmux session) then log out - do this whenever your machine reboots
- as above but using an existing docker image
- spin up an Azure notebook
- ... or we could do something like #1 - but have it setup under a separate user and administered via a systemd service
All four of the above are fine for different reasons and use-cases but here I'll talk about how I put #4 together in a little Linode instance running Fedora 25 - it's relatively simple, you can control over the kernels installed, and it's another excuse to get a bit more in-depth with another Linux subsystem (systemd).
Requirements
All you need is a Linux system which uses systemd (Fedora 15.0 or newer, Debian 8.x or newer, Ubuntu 15.04 or newer, for example) which you have sudoer level access on, and Python 3.x. It's probably pretty straight-forward to set this up on systems using the SysV init but I won't cover them here.
Install and Set Up Jupyter
First thing we need to do is install Jupyter and set up the user context which the Jupyter will be run under - which is a user called "jupyter":
$ sudo python3 -m ensurepip $ sudo pip install jupyter $ sudo useradd jupyter $ sudo passwd jupyter
Next we should switch to the new jupyter user, create the directory our notebooks will live in and generate the Jupyter config we'll mess around with:
$ su - jupyter $ mkdir notebooks $ jupyter notebook --generate-config
The last command will create a new file ~/.jupyter/jupyter_notebook_config.py which we'll do a little messing around with shortly, but before this we'll set up a password
$ python3 Python 3.5.2 (default, Sep 14 2016, 11:28:32) [GCC 6.2.1 20160901 (Red Hat 6.2.1-1)] on linux Type "help", "copyright", "credits" or "license" for more information. >>> from notebook.auth import passwd >>> passwd() # below I enter "password123" Enter password: Verify password: 'sha1:2eff88aac285:385c87867bd18fe852ee1d56b1010d4beed96969'
This will be used to log in to the application when its running. Open up the ~/.jupyter/jupyter_notebook_config.py file in a text editor and add/modify the following lines (using the SHA1 hash returned by the above):
c.NotebookApp.port = 8888 c.NotebookApp.ip = '0.0.0.0' c.NotebookApp.password = 'sha1:2eff88aac285:385c87867bd18fe852ee1d56b1010d4beed96969'
Setting up a Jupyter systemd service
Now we want to create a new systemd service so we can make sure our Jupyter notebook runs on startup, handles logging nicely and has all the other bells-and-whistles afforded to us by systemd. This is surprisingly simple - we want to create a new file jupyter.service in /usr/lib/systemd/system - this will tie together our newly installed Jupyter software and our newly setup jupyter user - using your favourite text editor create it so it looks like the below:
$ sudo cat /usr/lib/systemd/system/jupyter.service [Unit] Description=Jupyter [Service] Type=simple PIDFile=/var/run/jupyter.pid ExecStart=/usr/bin/jupyter notebook --no-browser WorkingDirectory=/home/jupyter/notebooks User=jupyter Group=jupyter [Install] WantedBy=multi-user.target%
Now all that's left to do is cross our fingers, enable our services, kick them off and browse to our remote box and login with our password:
$ sudo systemctl daemon-reload $ sudo systemctl enable jupyter $ sudo systemctl start jupyter
And if you want you can stop here - bookmark your http://www.xxx.yyy.zzz:port address and you're all set!
Conclusion
This was initially just an experiment - an excuse to test out my ability to put together a systemd .service file and do something more with a mostly-idle linux server sitting in a facility somewhere in Amsterdam. However I have found that I really like using this setup. When I was first shown Jupyter (née IPython) I was unimpressed and didn't see the point. However over the last few days I've been working through Project Euler problems again while teaching myself F# (using the IfSharp kernel) and I have found that it lends itself very well to my problem solving workflow on Project Euler.