Installing Jupyter Notebook on Google Cloud

Google Cloud is the upcoming Cloud computing platform that is taking over AWS. Coming from AWS, google cloud does look confusing at-least I felt so. So heres a blog to help everyone install Jupyter Notebook in Google Cloud.

In google cloud, one can create a project and all the instances for that particular project. This separate layer can ensure that any change in the settings like firewall ,etc can be applied over to the entire project’s components. The image below should be able to provide more clarity

Hierarchical View of Projects, Instances and Resources

To use Jupyter Notebook on the instance, we need to create an instance first.

Log into the Google Cloud Console. Create a new project using the “+” sign as shown in the image.

Dashboard Page

Once the project is created, select the project and click on the Products and icons and navigate to the Compute Engine label and click on VM Instances.

Click Create an instance

  1. Name your instance, choose a zone that you want. Click on Customize and look for the appropriate memory, CPUs ,etc as per your need.

Change Boot Disk accordingly and the SSD size.

You will need to check off the HTTP traffic it will needed when we access the instance for Jupyter Notebook. You may un-check “Delete boot disk when instance is deleted” to ensure the data is available even after the instance dies.

And click on Create. You are done!! The instance will be up and running in a few minutes.


The next step is to Setup the instance to be accessible.

To access the Jupyter Network, we need to make the The External IP address static.

Navigate to Products And Services -> VPC Network -> External IP Addresses.

Click on the External IP Address for the newly created instance.

By default the type of IP Address should be Ephemeral .Make it Static by changing it to Static.

Under the same VPC Network, you will find Firewall rules. Navigate to Firewall rules and create a new Firewall Rule.

Provide a Firewall name. Mention the Targets and the Source IP ranges as per your requirement.

Provide the Specified Protocols and Ports as tcp:8888, assuming that is the port you might be using for Jupyter Notebook and Save it.

The instance is all set to be accessed for Jupyter Notebook.


Next step is to install Anaconda in the instance.

Let us move in to the shell of the instance we just created. To do that, it is very simple in google cloud. They have a shell which can be accessed over the browser. This saves us from the hassle of accessing the SSH keys.

Let us go back to the instance page and Select the instance and against the SSH value, there is a drop down list with 4 options. We will use the first one.

As soon you login to the shell, run pwd and make a note of ur user directory. That will be needed for the rest of the steps.

Download the latest version of Anaconda using the below command

wget https://repo.continuum.io/archive/Anaconda3-4.2.0-Linux-x86_64.shwget https://repo.continuum.io/archive/Anaconda3-4.2.0-Linux-x86_64.sh

Run the Shell file to install Anaconda

bash Anaconda3–4.2.0-Linux-x86_64.sh

If you come across the question Do you wish the installer to prepend the Anaconda3 install location to PATH in your /home/ubuntu/.bashrc ? [yes|no] [no] answer yes.

The above step can be done manually as well by running the command

export PATH=/home/<user_directory>/anaconda3/bin:$PATH

Create a password for Jupyter Notebook using the following commands:

ipython

from IPython.lib import passwd

passwd()

Enter password: [Create password and press enter] Verify password: [Press enter]

'sha1:98ff0e580111:12798c72623a6eecd54b51c006b1050f0ac1a62d'

<Take a note of the above generated password. You will be needing it shortly.>

exit

Create Config profile

jupyter notebook --generate-config

Create certificates for https

mkdir certs

cd certs

sudo openssl req -x509 -nodes -days 365 -newkey rsa:1024 -keyout mycert.pem -out mycert.pem

Configure Jupyter

cd ~/.jupyter/

vi jupyter_notebook_config.py

Insert this at the beginning of the document:

c = get_config()
# Kernel config
c.IPKernelApp.pylab = 'inline' # if you want plotting support always in your notebook
# Notebook config
c.NotebookApp.certfile = u'/home/ubuntu/certs/mycert.pem' #location of your certificate file
c.NotebookApp.ip = '*'
c.NotebookApp.open_browser = False #so that the ipython notebook does not opens up a browser by default
c.NotebookApp.password = u'sha1:98ff0e580111:12798c72623a6eecd54b51c006b1050f0ac1a62d' #the encrypted password we generated above
# Set the port to 8888, the port we set up in the AWS EC2 set-up
c.NotebookApp.port = 8888

Replace the c.NotebookApp.password = u’sha1:98ff0e580111:12798c72623a6eecd54b51c006b1050f0ac1a62d’ with your generated password.

Press esc and type in :wq to save the file.

Create a New Folder

cd ~

mkdir Notebooks

cd Notebooks

Thats it!! You are all set to use Jupyter Notebook.

Run the command

jupyter notebook

Once it starts running, you can access it at the external Ip address of the instance.

https://<external-ip-address>:8888


I hope this blog helps everyone in setting up Google Cloud Instance and Jupyter Notebook. Feel free to comment on the blog.

References:

Run Jupyter Notebooks on Amazon EC2