Google Colab — Beginner’s Walkthrough

Christopher Lewis
Analytics Vidhya
Published in
5 min readMar 21, 2021

Google Colab is a great web IDE to use for any type of coding project (especially projects involving bigger datasets or requiring higher computational power), and is my preferred IDE of choice when creating projects. Think of Google Colab as a Jupyter Notebook that runs entirely in the cloud and comes with many core libraries already pre-installed. If you’re wondering about how to set up your own notebook in Google Colab, you’ve come to the right place! In this blog, I will walk through how to set up your own Google Colab, and finish with discussing the pros and cons.

How to Set Up:

  1. Navigate to Google Colab
  2. Select “New Notebook” under the File tab
File > New Notebook

3a. Connect the Notebook to your Google Drive by typing in and running this cell:

Mounting Google Drive via code cell

If you choose to run the above cell, it will ask you to go to a new URL in a browser. Click on that link, and choose the profile you want to sign in with. Be sure to read the to read through the information about what Google Drive will gain access to and make sure you’re ok with it. If you choose to proceed, click the “Allow” button and copy the code, switch back to the notebook, and paste in the code here:

Enter Authorization Code here

3b. Alternatively, you can also mount your Google Drive by clicking on the Google Drive icon located under the “Files” sidebar. This will also prompt the same as above, you just will need to make sure to manually do this every time you want to reconnect to a runtime:

Click on Google Drive folder icon

4. Next, we will select a runtime type to use in our Kernel. In order to select a runtime, click on the “Runtime” tab -> select “change runtime type”-> and select the type of hardware accelerator you want to use for the notebook, and click “save” once you’ve made a selection.

Select type of hardware accelerator

For more information on which type of hardware accelerator to use, it really depends on the type of project and amount of data you are working with. When working with Neural Networks or bigger datasets, I choose the GPU accelerator. The TPU accelerator is a good choice when working with smaller datasets.

If you’d like to see which hardware accelerator was assigned to your notebook, you can copy and paste the below code and run it in your notebook to find out:

gpu_info = !nvidia-smi
gpu_info = '\n'.join(gpu_info)
if gpu_info.find('failed') >= 0:
print('Select the Runtime → "Change runtime type" menu to enable a GPU accelerator, ')
print('and then re-execute this cell.')
else:
print(gpu_info)
Type of hardware accelerator chosen

And that’s it, we’re all set up to begin coding!

To Find a list of Libraries already pre-installed in Google Colab, simply type in:

!pip freeze

And it will print out a list of all the libraries already installed in Google Colab. If you need to install other libraries that aren’t already installed, simply type in:

# Template:
!pip install <library_name>
# example:
!pip install lime

The Pros of Using Colab

  • Free to Use
  • Notebooks are saved on Google Drive
  • Free GPU acceleration
  • Many pre-installed Libraries make it instantly available to use
  • Easy Collaboration

Let’s focus on the Collaboration aspect for one moment. Google Colab allows multiple users to share and edit the same Notebook without having to download or install anything — you just need access to a browser. Say you forgot your laptop at work (for some reason) and need to edit a few things on the notebook before the next day. Simply log on to the same google account that has access to the notebook and voila, access instant access to the notebook and no need to install any libraries.

To share a Google Colab notebook, click on the “Share” button near the top right of the page.

Click on the “Share” button

The Cons of Using Colab

  • if idled for too long, the runtime will disconnect
  • Need to install all libraries not pre-installed every time you instantiate a new session
  • Limited to 12GB of RAM if using the Free version of Google Colab

Unlike Jupyter Notebooks and other IDEs, if Google Colab idles for too long (around 30 minutes for free version and 90 minutes for Colab Pro) the runtime will disconnect and you will lose all local variables. So say you created a model and train it but the training could take a few hours, you need to manually interact with it while the notebook is running. There are some workarounds to this, and if you’d like to dig in deeper on these workarounds here’s a stackoverflow link that discusses the topic.

Thanks for reading, and if you have any questions or comments, feel free to post below!

--

--

Christopher Lewis
Analytics Vidhya

I am an aspiring Data Scientist and Data Analyst skilled in Python, SQL, Tableau, Computer Vision, Deep Learning, and Data Analytics.