Google Colab is a free online Python notebook but you can use it like a regular server following these simple steps.

Check my post about great Google Colab projects.

Table of Contents

Create a new notebook and run the instance

Go to Google Colab and select “New notebook” (you need a Google account to use Colab).

Google Colab notebook list window

Now, click on top-right “Connect” button.

Google Colab main window

By default, a CPU-only instance will be created. If you need GPU, first click on Runtime -> Change runtime type and select “GPU” on “Hardware Accelerator”. Finally, click “Save” and click “Connect”.

Google Colab change runtime type

Google Colab change runtime type

Running simple commands

In each Colab cell you usually type python commands but you can tell python to run a Linux command by adding a ! at the beginning of the command. For example:

!df -h

Then, click on the “Play” button on the left, or press Ctrl + Enter.

Google Colab

You can edit the cell to run other commands or create a new cell (and save previous commands output) by clicking on “+Code”. To type comments, start the line with a #. To do a cd, type %cd.

%cd /content

You can answer command prompts: click on the output, where you are supposed to enter the required answer and a textbox will display. Type the text and press Enter.

Folder structure

If you run !ls and !pwd, you will see your working directory is called /content and it has a folder named sample data. If you click on the folder icon on the left panel, a file explorer will show up, where you can upload and download files, delete or rename files and create new folders and files.

Google Colab

I recommend you to use the “Mount your Google Drive” option (and upload your files there) because it’s faster than using the upload function (click on the three-dot menu next to a folder to upload files there). You can also use wget to download resources from Internet.

Installing programs

Colab instances are based on Ubuntu LTS and APT package manager is installed. You can run apt commands to search and install packages.

# You can update repositories and search for one package in one cell
!apt update
!apt search neofetch
# And install the package in the following cell
!apt install -y neofetch

Google Colab

Example 1: edit a video with ffmpeg

After mounting your Google Drive folder, upload a video to Google Drive and extract the audio with ffmpeg:

# Check file paths with the integrated file browser
!ffmpeg -i drive/MyDrive/20210408_lgg2mini.mp4 -vn drive/MyDrive/audio.mp3

The new file will be on your Google Drive folder (you can also download it from Colab, just press the three-dot menu next to the filename).

Some filters are not included in Colab’s version of ffmpeg (like vidstab). You can download a ffmpeg static build that includes that filter.

Example 2: remove the background of an image

Mount your Google Drive folder or upload an image directly. Then, install virtualenv module (with pip) to create Python environments (it’s always a good idea to install python modules inside isolated enviroments). Finally, install backgroundremover module (https://github.com/nadermx/backgroundremover) and run it:

!pip install virtualenv
!virtualenv -p python3 bgremover
!source ./bgremover/bin/activate && \
pip install backgroundremover && \
backgroundremover -i /content/drive/MyDrive/N331.jpg -o /content/drive/MyDrive/N331.png
  • You need to join the commands with && for Python virtual environment to work properly.
  • Run backgroundremover with your input and output paths (remember that output image format must be ‘png’).