Setup Jupyter Notebook Server to Start Up on Boot and Use Different Conda Environments.

Artem Trunov
2 min readJun 7, 2019

--

When you rent an instance in a cloud and want to run a Jupyter notebook, you’ll typically need to login to the instance and start the Jupyter server. Let’s skip this step and setup an instance with autostart of Jupyter notebook server at boot time.

The reality is, most modern Linux distros (Ubuntu 18.04 in my case) use systemd. In systemd realm, you describe your daemon in a service file.

Here is a corresponding .service file for a Jupyter server:

[Unit]
Description=IPython notebook
[Service]
Type=simple
PIDFile=/var/run/ipython-notebook.pid
Environment="PATH=/home/artem/miniconda3/envs/notebook_env/bin:/home/artem/miniconda3/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
ExecStart=/home/artem/miniconda3/envs/notebook_env/bin/jupyter-notebook --no-browser --notebook-dir=/home/artem --NotebookApp.token='my_pasword' --ip=0.0.0.0
User=artem
Group=artem
WorkingDirectory=/home/artem
[Install]
WantedBy=multi-user.target

You’ll notice that I call jupyter-notebook from a notebook_env environment. I have a special setup that allows to run a notebook instance with any conda environments I create. I do this with a help of nb_conda_kernels extension of Jupyter. I install it in a dedicated env, in order not to mix it with “real” working environments:

$ conda create -n notebook_env python=3.7 notebook nb_conda_kernels

Then, you can create your Data Science environment with just a jupyter client package:

$ conda create -n dsenv python=3.7 pandas matplotlib ipykernel

The latter package is needed so that python enterpreter from dsenv can server as jupyter kernel for the notebook. Full jupyter-notebook set of packages is not required in this env!

Okay, so once you have created /lib/systemd/system/ipython-notebook.service with above content, issue the following commands:

$ sudo systemctl daemon-reload

to register config changes, and

$ sudo systemctl start ipython-notebook

You should be able to check your service with:

$ sudo systemctl status ipython-notebook

and just by checking the process:

$ ps -ef | grep jupyter

Now, once you aim your browser to the server home page and go to start a new notebook, you can pick up any conda environment you have created earlier.

In order to have your Jupyter server started up automatically on boot, do:

$ sudo systemctl enable ipython-notebook

Security note

I used here extra potentially dangerous options to the notebook server:

--NotebookApp.token='my_pasword' --ip=0.0.0.0

The first one sets an access token, the one that is a part of the server URL or the one you enter to login the the server manually, to the string of your liking. This is because the default behavior is to generate a token and print it to the stdout when the Jupyter server is launched. But when systemd starts the server for you, you don’t have the output easily, so hence the shortcut.

The second option allows connection to the notebook from everywhere, overriding default localhost-only permission. This is used if you want to access your server remotely, as in our case.

To learn how to secure your Jupyter server instance, see my other post: Jupyter Notebook security in cloud.

--

--