How to Run Jupyter Notebook on the Cloud: A Step-by-Step Guide

Gabriel Obaldia
KeaML
Published in
4 min readAug 21, 2023

In today’s rapidly evolving technological landscape, cloud computing has become an essential tool for many developers, data scientists, and researchers. Running resource-intensive applications like Jupyter Notebook on the cloud offers numerous benefits, including scalability, flexibility, and cost-efficiency. In this guide, we’ll walk you through the process of setting up Jupyter Notebook on an Ubuntu EC2 instance in Amazon Web Services (AWS), from creating the instance to configuring everything you need.

A computer with a Gigabyte card
Photo by Rafael Pol on Unsplash

If you use KeaML, you won’t need to do all this work. With just a couple of clicks, you will have your environment up and running.

Create an Ubuntu EC2 Instance

  1. Sign in to AWS Console: Log in to your AWS account and navigate to the EC2 Dashboard.
  2. Launch Instance: Click on the “Launch Instance” button to start the instance creation process.
  3. Provide a name: Give your instance a name (e.g. My Jupyter Server)
  4. Choose an Amazon Machine Image (AMI): Select an Ubuntu Server AMI that suits your needs (e.g. Ubuntu Server 22.04 LTS).
  5. Choose an Instance Type: Choose an instance type based on your requirements. For running Jupyter Notebook, a general-purpose instance like c5.large should be sufficient for small-scale projects. If you need a GPU, I recommend G5 instances (e.g. g5.xlarge), which include NVIDIA A10G GPUs.
  6. Assign a Key Pair: Choose an existing key pair or create a new one. This key will be used to access your instance via SSH. If you are creating one, don’t forget to download and store it in a secure place.
  7. Assign a Security Group: Choose an existing security group or create one. Ensure that you allow SSH access (port 22) and Jupyter Notebook access (port 8888).
  8. Add Storage: Configure the amount of storage you need for your instance. Jupyter Notebook projects may require substantial storage if dealing with large datasets.
  9. Review and Launch: Double-check your instance configuration and launch the instance.

Connect to the EC2 Instance

  1. Locate Instance: Once your instance is running, note its public IP address or DNS name.
  2. SSH Access: Open a terminal and use SSH to connect to your instance. Replace your-key.pem with the path to your private key file and your-instance-ip with the actual IP address:
ssh -i "your-key.pem" ubuntu@your-instance-ip

Install Necessary Packages

  1. Update Packages: Update the package repository and upgrade installed packages:
sudo apt update 
sudo apt upgrade -y

2. Install Python: If not already installed, install Python and pip:

sudo apt install python3 python3-pip -y

3. Install Jupyter Notebook: Install Jupyter Notebook using pip:

pip3 install jupyter

Configure Jupyter Notebook

  1. Generate Configuration File: Generate a Jupyter configuration file:
jupyter notebook — generate-config

2. Edit Configuration: Open the configuration file for editing:

nano ~/.jupyter/jupyter_notebook_config.py

3. Configure IP and Port: Find the lines that specify the IP and port Jupyter Notebook should listen on, and modify them as follows:

c.NotebookApp.ip = ‘0.0.0.0’
c.NotebookApp.port = 8888

4. Set Up Password: Generate a password hash to secure your Jupyter Notebook and then copy the generated hash:

from notebook.auth import passwd
passwd()

5. Configure Password: In the configuration file, find the line that starts with # c.NotebookApp.password and replace it with:

c.NotebookApp.password = ‘your-generated-hash’

Install and Configure Nginx (Optional)

  1. Install Nginx: Install the Nginx web server to act as a reverse proxy for Jupyter Notebook.
sudo apt install nginx -y

2. Create Nginx Configuration: Create a new Nginx configuration file:

sudo nano /etc/nginx/sites-available/jupyter

3. Configure Nginx: Add the following configuration, replacing your-domain with your domain or public IP address:

server {
listen 80;
server_name your-domain;

location / {
proxy_pass http://127.0.0.1:8888;
proxy_set_header Host $host;
proxy_set_header X-Real-IP $remote_addr;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
}
}

4. Enable Configuration: Create a symbolic link to enable the configuration and restart Nginx:

sudo ln -s /etc/nginx/sites-available/jupyter /etc/nginx/sites-enabled
sudo systemctl restart nginx

Now, port 8888 will be redirected to port 80. This means that you will be able to access Jupyter Notebook through port 80.

Start Jupyter Notebook

  1. Start Jupyter Notebook: Start Jupyter Notebook on the EC2 instance:
jupyter notebook

2. Access Jupyter Notebook: Open a web browser and navigate to your EC2 instance’s public IP address or domain with the port (http://your-domain:8888 or http://your-domain if you configured Nginx).

3. Login: Enter the password you configured earlier.

Congratulations! You now have Jupyter Notebook up and running on an Ubuntu EC2 instance in the cloud. This setup allows you to work on data science projects, machine learning tasks, and more, with the flexibility and scalability of cloud computing. Remember to manage your instance’s resources and security settings as needed for your project’s requirements.

--

--