Setting Up a Remote Jupyter Notebook in 8 Steps

Before starting you will need to set up an AWS instance. These steps were carried out using a Ubuntu 18 instance so some commands may be different depending on which instance is selected. 

1. Access Instance

First open up a terminal window and navigate to whatever directory you placed you key pair. Next ensure your key pair has the proper permissions with:

chmod 400 your_key_name.pem

By default Ubuntu won’t let you log in as a root user so you will have to login as ubuntu user.

ssh -i your_key_name.pem ubuntu@ec2_public_ip

2: Install Necessary Packages

Before you install jupyter you will need to make sure some packages are up to date and some packages are installed. This can be done with the following commands:

sudo apt update

sudo apt -y upgrade

sudo apt install -y python3-pip

sudo apt install build-essential libssl-dev libffi-dev python-dev

sudo apt install -y python3-venv

These packages give you all the tools to set up a virtual environment to run you jupyter notebook from.

3. Setup Environment

Once the necessary packages are installed you can set up your virtual environment. Good practice is to keep all of these environments in their own folder which you can do with:

mkdir environments

Next cd into the environments folder and create your environment.

cd environments

python3 -m venv my_env

4. Activate Environment and Install Jupyter

Once the environment is created, activate it and install jupyter.

source my_env/bin/activate

python3 -m pip install jupyter

5. Create Jupyter Config File and Add Password

In order to access the notebook remotely more easily its suggested that you create a password for it. You do this by first creating a jupyter config file:

jupyter notebook --generate-config

Then writing a password into it with the command:

jupyter notebook password

Create whatever password you want and verify it.

6. Launch Jupyter from a Port of your Choice

Now that your notebooks can be accessed with a password.  Launch a jupyter notebook from an open port and specify no browser (as the ec2 instance does not have a visual interface)

jupyter notebook --no-browser --port=8080

With the notebook running, open a new terminal for the next step.

7. SSH to Notebook Port from a New Terminal

In order to access the jupyter notebook ssh tunnel into the ec2 instance with the command:

ssh -i your_key_name -N -L 8080:localhost:8080 ubuntu@ec2_public_ip

the -N specifies that you will not be running any commands so don’t be worried if nothing happens after you enter it and its just a blank line. This means it worked! You can now access your notebook in browser.

8. Access your Notebook from a Modern Web Browser at the Specified Port

Enter this link in your browser (assuming you used the same port):  http://localhost:8080/

And that’s it! Now you can interface with your ec2 remotely in a jupyter notebook from your local computer. If you ever want to access this notebook again you will just need to relaunch it and ssh tunnel back into it.

Re-launching your notebooks

Now that everything is set up you can re-access an ec2 notebook with 4 commands. First ssh to your ec2 instance:

ssh -i your_key_name.pem ubuntu@ec2_public_ip

Next activate the virtual environment you want and launch a notebook

source environments/my_env/bin/activate

jupyter notebook --no-browser --port=8080

Then open a new terminal window and ssh tunnel into the port you specified

ssh -i your_key_name.pem -N -L 8080:localhost:8080 ubuntu@your_public_ip

Finally access the notebook from your browser

http://localhost:8080/

Summary of Commands:

chmod 400 your_key_name.pem

ssh -i your_key_name.pem ubuntu@ec2_public_ip

sudo apt update

sudo apt -y upgrade

sudo apt install -y python3-pip

sudo apt install build-essential libssl-dev libffi-dev python-dev

sudo apt install -y python3-venv

mkdir environments

cd environments

python3 -m venv my_env

source my_env/bin/activate

python3 -m pip install jupyter

jupyter notebook --generate-config

jupyter notebook password

source environments/my_env/bin/activate

jupyter notebook --no-browser --port=8080

From different Terminal:

ssh -i your_key_name.pem -N -L 8080:localhost:8080 ubuntu@your_public_ip

 

Leave a comment