Getting Started with Google Colab

google-colab
getting-started
tutorial
Learn the fundamentals of Google Colab, a free cloud-based Jupyter Notebook environment, to write, run, and share Python code in your browser without any setup or installation.
Author

Christian Mills

Published

May 14, 2023

Introduction

In this tutorial, I’ll introduce you to Google Colab, its features, and how to use it to run your code. Google Colab provides a free, cloud-based Jupyter Notebook environment that allows you to write, run, and share Python code in your browser without any setup or installation. A Jupyter Notebook is an interactive web-based tool for creating and sharing documents that contain live code, visualizations, and narrative text, often used in data analysis, visualization, and education.

Access Google Colab

To access Google Colab, follow these steps:

  1. Go to colab.research.google.com.
  2. Sign in with your Google account. If you don’t have a Google account, create one here.

google-colab-welcome-notebook-signed-out

The Notebook Selection Window

After signing in, the Notebook Selection window will pop up. This window displays a list of your recent notebooks, allowing you to access and open them. If this is your first time using Google Colab, you will only see the “Welcome to Colaboratory” notebook listed. The Notebook Selection window also allows you to import Jupyter Notebooks from Google Drive and GitHub or upload a notebook from your computer.

google-colab-welcome-page

The “Welcome to Colaboratory” notebook is already open behind the Notebook Selection window, so we’ll work with that one. Click the cancel button in the bottom right corner of the popup window to view the welcome notebook.

google-colab-welcome-page-exit-popup

Understanding the Notebook Interface

A notebook consists of a list of cells. Google Colab notebooks have two main types of cells: code cells and text cells. Code cells allow you to write and run Python code, while text cells let you add formatted text, images, and equations using Markdown. The first few cells in the welcome notebook are text cells.

google-colab-welcome-notebook-top

Text Cells

You can view and edit the Markdown source of a text cell by double-clicking it. In editor mode, Google Colab will show the Markdown source and the rendered version side-by-side.

google-colab-welcome-notebook-inspect-markdown-source

We can edit the Markdown source, and the rendered version will update in real time.

google-colab-welcome-notebook-edit-markdown-source

You can exit the editor mode by pressing Shift+Enter, clicking the Close Markdown Editor icon in the top-right corner of the text cell, or clicking another cell.

google-colab-welcome-notebook-exit-markdown-editor

To create a new text cell, click the + Text button in the toolbar.

google-colab-welcome-notebook-add-text-cell

Google Colab will add the new Markdown cell below the currently selected cell.

google-colab-welcome-notebook-new-text-cell

Code Cells

To create a new code cell, click the + Code button in the toolbar at the top of the notebook or press Ctrl+M B.

google-colab-welcome-notebook-new-code-cell

Google Colab will add the new code cell below the currently selected cell.

google-colab-welcome-notebook-new-code-cell

You can write Python code in the code cell and execute it by pressing Shift + Enter or clicking the Play button on the left side of the cell. Any output from the code will appear directly below the code cell.

google-colab-welcome-notebook-run-code-cell

We can also use code cells to access the command line by adding an exclamation point at the start of the cell. We can use this ability to install Python packages via the pip package installer.

google-colab-welcome-notebook-access-command-line

Working with Data

Google Colab allows you to upload and download files to and from your computer and connect notebooks to your Google Drive.

Uploading Files

You can upload files from your local machine to use in your Google Colab notebook by following these steps:

  1. Click the Files icon in the left sidebar to open the file browser.

google-colab-open-file-browser
  1. Click the Upload button.

google-colab-upload-button
  1. Go to the file location on your local machine, select it, and click Open to upload it to your Google Colab workspace.

google-colab-select-file-to-upload
  1. Colab will display a warning that the runtime’s files get deleted when it terminates. Click OK in the bottom-right corner of the popup window.

google-colab-runtime-file-warning
  1. The uploaded file will appear in the file browser, and you can access it in your notebook.

google-colab-uploaded-file-in-file-browser
  1. We can view the file by double-clicking it in the file browser or loading it in the notebook via Python.

google-colab-load-image-file

Downloading Files

To download a file from your Google Colab workspace to your local machine, follow these steps:

  1. Locate the file in the file browser.
  2. Right-click the file and select Download.

google-colab-download-file

The file will download to your local machine.

Connecting to Google Drive

Google Colab notebooks can connect to Google Drive to access, store, and manage your files. To do this, follow these steps:

  1. Click the Mount Drive button in the file browser.

google-colab-mount-drive
  1. Google Colab will create a new code cell containing the following code:

    from google.colab import drive
    drive.mount('/content/drive')

google-colab-mount-drive-code-cell
  1. Run the code cell by pressing Shift + Enter or clicking the Play button on the left side of the cell. A popup window will appear, prompting you to authorize access to your Google Drive.

google-colab-mount-drive-code-cell-popup
  1. Click the Connect to Google Drive button to open the authorization page.

google-colab-drive-authorization-page
  1. Sign in with your Google account, and click Allow to grant access.

google-colab-allow-google-drive-to-access-account
  1. Return to your Google Colab notebook. The code cell should have printed a message indicating your Google Drive is now mounted.

google-colab-verify-google-drive-mounted-message
  1. Click the Refresh button in the file browser to update the contents.

google-colab-refresh-file-browser

Your Google Drive should now be mounted and accessible from the file browser.

google-colab-verify-driver-accessible-in-file-browser

You can read, write, and manage your Google Drive files directly from your Google Colab notebook. To access the files, use the path /content/drive/MyDrive/ followed by the file or folder name.

Using Hardware Acceleration

Google Colab offers free access to GPUs and TPUs to accelerate your code. To enable GPU or TPU acceleration:

  1. Click the “Runtime” menu at the top of the notebook.

google-colab-click-runtime-menu
  1. Select “Change runtime type.”

google-colab-runtime-menu-change-runtime-type
  1. Choose “GPU” from the “Hardware accelerator” drop-down menu.

google-colab-choose-gpu-hardware-accelerator
  1. Click “Save.”

google-colab-save-hardware-accelerator-selection
  1. Changing the hardware accelerator requires loading a new runtime. Loading a new runtime will delete any files we added and disconnect Google Drive. Google Colab will show a popup window asking you to confirm you want to delete the current runtime. Click “OK” to confirm.

google-colab-delete-previous-runtime-popup-window
  1. Verify the notebook has GPU access by running the following code in a code cell:

    !nvidia-smi

google-colab-nvidia-smi-results

Your notebook will now use the selected hardware accelerator. Note that free GPU and TPU usage is time-limited. You can run notebooks on the free tier for at most 12 hours at a time (usually less). If you exceed the time allotment, you must wait until it resets (typically about 12 hours). Therefore, only enable hardware acceleration when needed and disable it when you don’t. To disable hardware acceleration, select None from the Hardware Accelerator drop-down menu.

google-colab-disable-hardware-acceleration

Create a New Notebook

To create a new notebook:

  1. Open the File menu in the top-left corner and select New notebook.

google-colab-create-new-notebook

A new notebook will open in a separate tab. The runtime for the previous notebook is still active.

google-colab-new-notebook

You can rename the notebook by clicking the notebook name at the top of the page. For now, we can name it “My First Notebook.”

google-colab-rename-notebook

Save Your Notebook

Google Colab automatically saves your notebooks to a “Colab Notebooks” folder in Google Drive.

google-drive-colab-notebooks-folder

Note the “Welcome to Colaboratory” notebook is not in the folder. Since we did not create that notebook, we must save our copy manually. Switch to that notebook’s tab and click the “Copy to Drive” button.

google-colab-save-copy-to-drive

Google Colab will open our new copy of the notebook in a separate tab.

google-colab-copy-of-welcome-notebook

If we check the “Colab Notebooks” folder in Google Drive, we should now see our copy of the welcome notebook.

google-drive-verify-copy-of-welcome-notebook

Sharing Notebooks

You can share your Google Colab notebook with others, similar to other Google Drive documents. To share your notebook:

  1. Click the “Share” button in the top-right corner of the notebook.

google-colab-share-button
  1. In the sharing settings dialog box, enter the email address of the person you want to share the notebook with.

google-colab-share-notebook-add-people
  1. Select their permission level: “Viewer,” “Commenter,” or “Editor.”

google-colab-share-notebook-set-permission-level

Alternatively, you can create a shareable link with specific access settings (view, comment, or edit). You can share this link with others, and they can access the notebook according to the chosen permission level.

  1. Open the drop-down menu under General Access and select the “Anyone with a link” option.

google-colab-enable-shareable-link
  1. Set the permission level: “Viewer,” “Commenter,” or “Editor.”

google-colab-enable-shareable-link-permission-level
  1. Click “Copy link” to copy the shareable link.

google-colab-enable-shareable-link-copy-link

Version Control with GitHub

Google Colab can save and load notebooks from GitHub repositories, enabling seamless collaboration and tracking of changes in your code.

Saving a notebook to a GitHub repository:

  1. Open the File menu in the top-left corner and select Save a copy in GitHub.

google-colab-file-menu-save-copy-in-github
  1. If you haven’t connected your GitHub account yet, follow the prompts to authorize Google Colab to access your repositories.

github-authorize-google-colab
  1. Choose a repository, branch, and file path for your notebook. You can also update the commit message.

google-colab-copy-to-github-select-repository
  1. Click OK to save the notebook to the specified GitHub repository.

google-colab-copy-to-github-click-ok

Loading a notebook from a GitHub repository:

  1. Go to the Google Colab website.

google-colab-welcome-page{fig-align=“center”}

  1. Click the GitHub tab in the Notebook Selection window.

google-colab-selection-window-click-github-tab
  1. Enter the URL for the GitHub repository containing the notebook you want to open. You can also search for one by entering a username or organization and repository name.

google-colab-enter-github-repo-url
  1. Select the notebook you want to open, and it will open in a new tab.

Conclusion

You’ve now learned the fundamentals of Google Colab. This tutorial covered creating and editing cells, working with data, hardware acceleration, and saving and sharing notebooks via Google Drive and GitHub. Keep exploring Google Colab to uncover more features that can enhance your projects.


Next Steps
  • Feel free to post questions or problems related to this tutorial in the comments below. I try to make time to address them on Thursdays and Fridays.
  • If you would like to explore my services for your project, you can reach out via email at [email protected]