Get Started with Google Colab

Get started with Google Colab

Google Colab, or Colaboratory, is a doppelganger of Jupyter Notebook running on the cloud. Due to its zero-cost, pre-configured Python development environment, Colab has become a favorite among data science enthusiasts for running experiments in recent years.

Colab makes a perfect fit for anyone looking for a quick and affordable environment for training ML models. With the free access to a GPU or TPU, it’s hard to find a better alternative to Colab on the market today.

If you’re already familiar with Jupyter Notebook, you’ll have an easier time figuring out how Colab works. But if that’s not the case, we’ll cover the essentials of getting started with Google Colab in this post.

How Does Google Colab Work?

Colab is basically a cloud-based Python development environment created by Google Research. It gives you free but limited access to Google’s computing resources to run experiments without burdening your local system.

The Colab environment is specially configured for data science-related tasks. It comes with many popular data science libraries—like Pandas, Numpy, and Tensorflow—pre-installed. Most of the time, you only have to visit Colab from your browser and start building models from the get-go.

Colab allows you to create notebooks with blocks of executable code and text. This format is very similar to Jupyter Notebook. It isn’t surprising considering how Colab is based on the open-source Jupyter project.

Colab also allows you to share notebooks with others and collaborate on projects. It has become one of the biggest reasons people choose Colab over their local environments.

Another draw is the free access to GPUs and TPUs. It sounds like a deal too good to be true, considering the cost of using GPUs and TPUs on popular cloud platforms. But Colab’s offer is indeed true, even though it comes with certain limitations that we’ll discuss in later sections.

Who Can Use Google Colab?

Basically, anyone can use Google Colab for Python development purposes. It doesn’t have to be data science related. But its data science-tailored environment attracts the majority of people from this field. Several of its popular use cases include data analysis, data visualization, and machine learning model development and training.

Essential Google Colab Features

Let’s take a look at working with some basic Colab features in this section.

Create a new notebook

You can visit this Google Colab link to create your first notebook. It brings you to a “Welcome To Colaboratory” notebook and a popup window that allows you to create a new notebook or open an existing one.

If you choose the New Notebook option, Colab gives you a new notebook in .ipynb format.

Creating a new Notebook

Creating a new Notebook

Additionally, Colab’s compatibility with Jupyter allows you to open an existing Jupyter notebook through the Upload tab in the previous popup window.

Upload a jupyter notebook into Google Colab

Upload a jupyter notebook into Google Colab

Colab saves all new notebooks in your Google Drive.

Add new cells

You can add a new code cell to Colab using Ctrl+M B keyboard shortcut. If you want to convert a code cell to a text cell, use Ctrl+M M. The interface also provides different methods to add new code or text cells.

You can use code cells to write the usual Python code. Text cells, on the other hand, are used to organize the notebook and give more context to your coding decisions.

Example of code and text cells

Example of code and text cells

Text cells primarily support markdown for adding content. But they also accept other formats like HTML and latex. You can also add images, links, emojis, and other styling elements to the text.

Example of text cell with Markdown content

Example of text cell with Markdown content

Run code cells

Colab provides a few different options for running a code cell.

  • Run the current cell: Use the arrow icon on the cell’s left side or Ctrl+Enter
  • Run the current cell and add a new cell below: Alt+Enter
  • Run the current cell and go to the next cell: Shift+Enter
  • Run all cells: Ctrl+F9

The Runtime option in the notebook’s top menu shows more ways to control how the code execution happens.

Restart and run all menu option

Restart and run all menu option

The first time you use this run operation on your notebook is when Colab allocates you a virtual machine to execute the program. You can have access to it for a maximum of 12 hours. After the time limit, Colab disconnects and deletes your VM.

If you leave the notebook idle for too long, Colab automatically disconnects you even though the time hasn’t run out.

Runtime disconnected alert dialog

Runtime disconnected alert dialog

A Deeper Look into Colab Features

In this section, we’ll look at some special features in Colab that you may find useful in different situations.

Inspect connected host

We already know how Colab assigns a virtual machine to run your program. But it doesn’t disclose the precise memory and disk space bounds of these VMs. In fact, their values are rarely identical across VMs. Usually, you can expect to have close to 12GB of memory and 108GB of disk space.

If you run out of that 12GB memory allocation, Colab allows you to switch to a high-RAM runtime of 25GB.

Colab provides two scaled graphs to give you an idea about your resource usage. They plot your overtime RAM and disk utilization to help you figure out if you’re close to exceeding the limits.

Cloud resources view

Cloud resources view

Use GPU or TPU acceleration

If you’re running particularly computationally heavy, time-consuming experiments on Colab, you can select GPUs or TPUs to accelerate the execution.

You will find this “Change runtime type” option under the “Runtime” menu. It leads you to a popup window to select the hardware accelerator type. By default, this value is set to none. If you want to use a GPU or a TPU for your work, select the appropriate option from the dropdown and save the changes.

Notebook settings screen where the user can select the hardware accelerator

Notebook settings screen where the user can select the hardware accelerator

Colab’s GPU and TPU access comes with dynamically changing limits. Colab doesn’t disclose the lifetimes of the allocated GPUs or idle timeouts. But you can expect the maximum lifetime to last about 12 hours like the VMs.

The most commonly assigned GPU in Colab is K80. Faster GPUs like T4 are more difficult to see and often follow restricted quotas.

Colab also employs a mechanism to prioritize GPU and TPU resources for users who have used fewer resources in the near past. Users who often run long computations are more likely to face tighter limitations in this scenario.

Upload files from your device

Colab allows you to upload files from your local device to use in your experiments. The following files.upload method prompts you to select the necessary files and handle the operation.

from google.colab import files
uploaded = files.upload()

Inline file upload form added by code

Inline file upload form added by code

Once it completes, you’ll be able to see the newly added files in the Files tab on the left pane.

The side panel <code>Files</code> host all uploaded files

The side panel Files host all uploaded files

Mount your Google Drive

You can mount your Google Drive on the allocated VM using the drive.mount method.

from google.colab import drive

This code opens a popup window that prompts you to authorize the mounting operation.

Confirmation dialog asking to confirm the connection to Google Drive

Confirmation dialog asking to confirm the connection to Google Drive

After giving permission, you’ll be able to view and access your Drive file system through the Files tab.

The drive mount appears in the <code>Files</code> side panel

The drive mount appears in the Files side panel

Share the notebook

As we mentioned before, you can add other collaborators to a notebook as editors, commenters, or viewers. It gives them access to all code, text, and outputs that appear in your notebook.

But it doesn’t share any resources or content you’re using with the others. They won’t be able to access your VM environment, including the local file system or new libraries you’ve installed. Collaborators have to set up such content manually before running the notebook.

Run terminal commands

Colab allows you to run terminal commands on a code cell by using ! as a prefix. This method can list the files in the current directory, install new Python libraries, clone GitHub repositories, or execute any other terminal-bound task.

Running terminal commands like git or pip on a cell

Running terminal commands like git or pip on a cell

Colab Pro and Pro+

Despite the many benefits of Colab’s free tier, it comes with limited resources that aren’t enough in certain situations. Colab Pro and Colab Pro+ provide a paid solution to overcome this problem.

Plan comparison and pricing

Plan comparison and pricing

Colab Pro and Pro+ give you access to longer runtimes, faster GPUs, and more memory than the free tier. Colab Pro+ has the additional benefit of executing notebooks in the background, even when you close the browser tab.

Both paid plans come with the promise of minor fluctuations in resource allocations from one VM to another.

The following table shows a brief comparison between Colab Pro and Pro+.

Colab ProColab Pro+
Maximum runtime24 hours24 hours
Maximum RAM32GB52GB (with high-RAM option)
GPU typesK80, T4, P100, or V100K80, T4, P100, or V100
Background operationNoYes


Colab is an incredibly valuable tool, particularly for hobby data scientists, for conducting their experiments. It gives you access to computing resources, an easy-to-use development environment, and a collaborative working space at zero cost on your end.

Although not free, Colab Pro and Pro+ come with higher resource guarantees at an affordable price to more regular and professional users.

These three plans combined have made Google Colab a formidable opponent for challenging more expensive data science platforms in the market today. If you, too, are looking for a place to practice and perfect your skills in this field, Colab is sure to become a perfect fit for you.

Thanks for reading!

If you liked what you saw, please support my work!

Anjalee Sudasinghe - Author @ Live Code Stream

Anjalee Sudasinghe

I’m a software engineering student who loves web development. I also have a habit of automating everyday stuff with code. I’ve discovered I love writing about programming as much as actual programming.

I’m extremely lucky to join the Live Code Stream community as a writer and share my love for programming with others one article at a time.