Install and configure JupyterHub in an Amazon EC2 instance

DRAFT: so far jupyterhub works only through http. I’ll add steps on how to use https, and some other tweaks to the configuration.

First of all let’s create an Amazon Instance.

For this tutorial I set up a t2.micro instance based on Ubuntu 18.04 64 bit.

I would like to access jupyterhub from remote locations using https. During the creation of the instance I have modified the Security Group to accept traffic through port 443 (the default for https) from all remote locations. To make tests without encryption I also opened port 8080. I advice you open it before the https access is fully setup.

During the creation of the instance I created a new key pair (that I called Jupyterhub) and I saved the private key (jupyterhub.pem file) on my laptop in my ~/.ssh folder.

Once this is done you have to change the permissions of the key file: chmod 400 ~/.ssh/jupyterhub.pem

Now go to the EC2 dashboard and find the instance you just created and annotate the public ip (in this guide I’ll denote it with .

Access the EC2 machine using

ssh -i ~/.ssh/jupyterhub.pem ubuntu@

This is not strictly necessary, but every time I create a new instance I am used to update all the packages with the following list of commands

sudo apt-get update
sudo apt-get upgrade
sudo apt-get dist-upgrade
sudo apt-get autoremove
sudo apt-get autoclean

In most cases it will be necessary to reboot the machine with:

sudo reboot now

Note that when the server shuts down the connection is lost, so you have to wait a few seconds and re-connect through ssh.

In these notes I’ll install Jupyterhub using the ubuntu user. Note however that in a production environment this could have some safety concerns because the ubuntu user is a “sudoer” and doesn’t require password by default. If you plan to have the Jupyterhub server open to the world it’s better if you secure your server first.

I’ll set up a conda environment where I’ll install Jupyter Notebooks and Jupyterhub.

At the moment of writing you can get the anaconda installer with:

curl -O https://repo.anaconda.com/archive/Anaconda3-5.2.0-Linux-x86_64.sh

Once the download has finished you can install conda with

sh ./Anaconda3-5.2.0-Linux-x86_64.sh

I don’t like to add the whole anaconda3/bin folder to my PATH because in the past I had conflicts with executables that come from the OS.

To overcome this I usually do:

cd ~
mkdir ~/bin
ln -s /home/ubuntu/anaconda3/bin/conda ~/bin/
ln -s /home/ubuntu/anaconda3/bin/activate ~/bin/
ln -s /home/ubuntu/anaconda3/bin/deactivate ~/bin/

This way I can activate and deactivate the environments, and use the conda bin only when actually in an environment.

Next create a conda environment where we’ll install Jupyter and JupyterHub:

conda create –name jupyter python=3.6

source activate jupyter

conda install -c conda-forge jupyter notebook

Next let’s create the default configuration file:

jupyter notebook –generate-config

OK, now that we have done this let’s configure the default port and let’s allow to listen for requests from all ips.

Let’s edit the configuration file for jupyter

vim ~/.jupyter/jupyter_notebook_config.py

Make sure to set the NotebookApp.ip to ‘*’:

c.NotebookApp.ip = ‘*’

and to change the listening port to 8080 (or any other port you want, but make sure to add a rule in the security group).

## The port the notebook server will listen on.
c.NotebookApp.port = 8080

Now you can start the notebook and verify that everything works fine:

jupyter notebook –no-browser

Notice that you will get displayed a link like:

http://127.0.0.1:8080/?token=b28edd5356210e6be70fa75da1d9ce41a75e7d010f04733c

In order to access your notebook you have to substitute 127.0.0.1 with the public ip of your EC2 instance

Leave a Reply