How to Set up an Instance to Run a Jupyter Notebook in GCP?

tudip-logo

Tudip

14 May 2019

The Jupyter Notebook is an open-source web application that allows you to run the live code, embed visualization and explanatory text all in one place. It supports dozens of programming languages like PHP, Ruby on Rails, Spark, Matlab and many more. Installing such applications in the local system can be a real slog. You should have a good system configuration as well as you need to take care of the maintenance. I will guide you in an easy way to get the job done in less effort and you have to pay only when you are using it. To Run Jupyter Notebook on Google Cloud Platform please check the following steps.

We will be using Google Cloud Platform and Anaconda Distribution to perform the task.

Why Anaconda Distribution? Because, it includes Python, the Jupyter Notebook, and other commonly used packages for scientific computing and data science.

So, let’s get started!

Step 1. Create or log in to a GCP account

You must have an account in google cloud platform with which all the billing will be carried with it. You can also create a free tier account here and receive $300 GCP credits to spend on Google Cloud Platform over the next 12 months.

Refer here for more details on GCP free tier account.

Step 2. Create or select a GCP project

In order to setup an instance, you need to select or create a project in GCP. Creating a project is simple, here’s how:

add-gcp-project

Step 3. Setup a VM instance

  • In a new project, you must enable billing to use Compute Engine Service. Go to Compute Engine > VM instance and click “Enable billing”.

setup-vm-instance

  • Click “Create” to create a new VM instance with the configurations below:
    • Instance Name: <Name your instance>
    • Select your nearest region and zone.
      • Region: asia-south1 (Mumbai)
      • Zone: asia-south1-c
    • Machine Type: select “8 vCPUs 30 GB memory, n1-standard-8 (You can increase the memory as per your need)
    • Boot Disk: Ubuntu 16.04 LTS
      • Boot disk type: Standard Persistent disk
      • Size(GB): 10
    • Keep “Identity and API access” as default.
    • Firewall: Click the checkbox to allow both HTTP and HTTPS traffic.
    • Click “Management, security, disks, networking, sole tenancy” to expand and go to Disks section.
      • Uncheck “Delete boot disk when the instance is deleted” deletion rule.
    • Click on the “Create” button.
    • Now your instance is ready!

Note: When you are not using your instance, you can stop your instance to save money. You can stop your instance by clicking Stop present under three dots menu.three-dots

Step 4. Make External IP as Static

  • In order to access the Jupyter notebook, we need to make External Ip as static.
  • Go to Networking > VPC Network > External IP addresses.
  • You will see an external IP address of your instance, change the type from Ephemeral to Static.
  • You will see a popup window where you need to provide a name for the new static IP address and click Reserve.
    Note: Make a note of your external IP address, you will require it later.

Step 5. Create Firewall Rules

  • On the same path, click Firewall rules (Networking > VPC Network > Firewall rules) and Click “CREATE FIREWALL RULE” with below configuration:
    • Name: <Enter a firewall name>
    • Targets: All instances in the network
    • Source IP ranges: 0.0.0.0/0
    • Protocols and ports: Select “Specified protocols and ports” option.
      • tcp: 8888 <You can change any other port number>
    • Keep other configuration as default.
    • Click on the Create button.

firewall-rules

Step 6. Install Anaconda in your VM instance

  • Go to your VM instance (Compute > Compute Engine > VM instance) and click on SSH button of your instance.

image2

  • In your SSH terminal, enter the commands below one by one:
wget https://repo.continuum.io/archive/Anaconda3-4.2.0-Linux-x86_64.sh
bash Anaconda3-4.2.0-Linux-x86_64.sh
  • Now follow the on-screen instructions and choose yes for question – Do you wish the installer to prepend the Anaconda3 install location to PATH in your /home/haroldsoh/.bashrc ?[yes|no][no]
  • Enter the commands below and install the other software:
source ~/.bashrc

pip install tensorflow

pip install keras

Step 7. Setup the VM server

  • Enter the commands below to create and configure the Jupyter configurations files:
jupyter notebook --generate-config

vi ~/.jupyter/jupyter_notebook_config.py
  • Press “i” to insert and add the following lines in the configuration file:
c = get_config()
c.NotebookApp.ip = '*'
c.NotebookApp.open_browser = False
c.NotebookApp.port = 8888

Note: You can change the port number. I have chosen the 8888 port here.

  • Once you are done with making changes, press Esc and type “:wq” to save and exit from the file.

Step 8. Launch Jupyter Notebook

  • Run the command below:
    jupyter notebook
  • Now, you can launch the notebook in your browser
    http://<external-ip-address>:enter< your port number without angular brackets>

image6

Congratulations! You just set up a VM instance to run Jupyter notebook using GCP.

Also, know more about Affordable Virtual Machine Instances in GCP

Request a quote