Google Colab: Hosted Notebooks, GPUs and Workflows
Google Colaboratory (Colab) is a hosted Jupyter notebook service from Google. You open a notebook in the browser, sign in with a Google account, and code runs on a virtual machine in Google’s cloud, with common data-science and machine-learning libraries preinstalled and optional GPU or TPU accelerators. Notebooks are stored in Google Drive and can be shared and edited like a Google Doc. It is the quickest way to try a model, teach a class or run a GPU experiment without installing anything.
Colab uses the standard .ipynb format, so everything in Jupyter notebooks: kernels, magics and rich output applies: cells, magics such as %pip and %timeit, and ! shell commands. This page covers what is specific to Colab. The running example trains a small model that predicts late deliveries for an invented OrdersPortal shop. The Colab-specific samples (google.colab modules, Drive, GPU) need a Colab runtime and were not run here; they were checked against the google.colab source in the googlecolab/colabtools repository and the PyTorch 2.x API.
Topics on this page
Section titled “Topics on this page”| Topic | What it answers |
|---|---|
| Colab and local Jupyter | When to use which |
| Plans and limits | Free tier, paid plans, compute units and session limits |
| Runtimes and accelerators | Choosing CPU, GPU or TPU and checking what you got |
| Files and Google Drive | Mounting Drive, uploading and downloading |
| Installing packages | What is preinstalled and what survives a reset |
| Secrets | Keeping API keys out of notebooks |
| Forms | Turning variables into input fields |
| AI assistance | Gemini features inside Colab |
| GitHub | Opening, saving and cloning repositories |
| Training on a GPU | A PyTorch loop with checkpoints to Drive |
| GPU memory | Mixed precision, smaller batches and clearing memory |
| Monitoring training | TensorBoard and experiment trackers |
| Saving work before a reset | What is lost and how to protect it |
Colab and local Jupyter
Section titled “Colab and local Jupyter”| Google Colab | Local Jupyter | |
|---|---|---|
| Setup | None: a browser and a Google account | Install Python, JupyterLab and packages |
| Hardware | Google’s VMs; GPUs and TPUs on demand, subject to availability | Your machine or your own server |
| Storage | Temporary VM disk; Google Drive for anything you keep | Your disk |
| Installed packages | Reset with the runtime | Persist in your environment |
| Session length | Limited (see below); idle sessions are disconnected | Runs as long as your machine does |
| Collaboration | Share like a Google Doc, with comments | Git, JupyterHub or file sharing |
Choose Colab for short experiments, teaching, demos and occasional GPU work. Choose local Jupyter or a managed workbench (for example Vertex AI Workbench or Colab Enterprise on Google Cloud) for long jobs, private data that must stay in your own environment, or reproducible team work with pinned environments.
Plans and limits
Section titled “Plans and limits”Colab has a free tier and paid options: Colab Pro and Pro+ subscriptions and Pay As You Go. Paid usage is measured in compute units: each runtime type consumes units at a different rate, and when your balance runs out you fall back to free-tier limits. Prices and unit allowances change, so check the Colab signup page rather than relying on figures from articles.
The important limits, from the Colab FAQ:
- Session length. Free-tier notebooks can run for at most 12 hours, and often less depending on demand. Idle notebooks are disconnected sooner. Pro+ with enough compute units allows longer continuous (background) execution.
- No guaranteed hardware. GPU and TPU types and availability vary over time, also on paid plans; premium GPUs are offered subject to availability.
- Acceptable use. The free tier is for interactive notebook work. The FAQ lists activities that are not allowed (for example cryptocurrency mining and some kinds of remote access or proxying), and scripts that fake activity to keep a session alive break the intent of the limits. If a job needs to run unattended for days, it belongs on a VM or a managed training service.
Pitfall: planning a deadline around a free GPU. You may get a CPU-only runtime at a busy time. Keep a CPU fallback in your code (next section) and checkpoint often.
Runtimes and accelerators
Section titled “Runtimes and accelerators”Choose hardware with Runtime > Change runtime type; the dialog lists the CPU, GPU and TPU options currently available to your account. Runtime > Disconnect and delete runtime releases the VM (and its compute units). Changing the runtime type starts a fresh VM, so variables and installed packages are lost.
Always check what you actually received, and write code that works without a GPU:
# fragment: run in a Colab cellimport torch
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")print("Using:", device)if device.type == "cuda": props = torch.cuda.get_device_properties(0) print(f"GPU: {props.name}, {props.total_memory / 1e9:.1f} GB")!nvidia-smi shows the GPU model, memory use and running processes. Files you create outside Drive live on the VM’s local disk under /content, which is fast but temporary.
TPUs are mainly used with JAX and TensorFlow. Check the runtime’s current TPU type and follow the framework’s TPU guide rather than older TensorFlow 1.x-style code, which no longer applies to current TPU runtimes.
Files and Google Drive
Section titled “Files and Google Drive”Mount Google Drive to read datasets and save results that survive the runtime:
# fragment: run in Colabfrom google.colab import drivedrive.mount("/content/drive") # asks you to authorize access
import pandas as pdorders = pd.read_csv("/content/drive/MyDrive/ordersportal/orders.csv")orders.to_parquet("/content/drive/MyDrive/ordersportal/orders.parquet")For small files, google.colab.files uploads from and downloads to your computer:
from google.colab import filesuploaded = files.upload() # dict of {filename: bytes}files.download("late_delivery_report.csv") # browser downloadDrive is a network file system, so reading thousands of small files from it is slow. Copy an archive to local disk once and unpack it there: !cp /content/drive/MyDrive/ordersportal/images.zip /content/ then !unzip -q /content/images.zip -d /content/data. Before you disconnect, call drive.flush_and_unmount() to make sure pending writes reach Drive.
Installing packages
Section titled “Installing packages”Colab images include the usual stack: NumPy, pandas, scikit-learn, Matplotlib, PyTorch, TensorFlow, JAX and Hugging Face libraries among others. Check versions rather than assuming them (import torch; torch.__version__), because Google updates the image regularly.
Install anything else with %pip, and put the installs in the first cell with pinned versions, because every new runtime starts from the image again:
%pip install -q lightgbm==4.6.0 python-dotenvIf you upgrade a preinstalled package that is already imported, Colab asks you to restart the session (Runtime > Restart session) so the new version loads. Avoid replacing core packages like torch unless you need to; the preinstalled builds match the runtime’s CUDA version.
Secrets
Section titled “Secrets”Colab Secrets (the key icon in the left sidebar) store values such as API keys in your Google account rather than in the notebook. You grant each notebook access to each secret, and read it with userdata.get():
# fragment: run in Colabfrom google.colab import userdata
api_key = userdata.get("ORDERSPORTAL_API_KEY")userdata.get() raises SecretNotFoundError if no secret has that name and NotebookAccessError if this notebook has not been granted access, so a missing key fails loudly instead of sending None to an API. Secrets are not copied when you share the notebook; each user adds their own. Libraries that read environment variables can be fed with os.environ["HF_TOKEN"] = userdata.get("HF_TOKEN").
Pitfall: printing the secret. Anything a cell prints is saved in the notebook output and shared with it. Never print(api_key) to “check” it.
A #@param comment turns a variable into a form field shown next to the code, and #@title names the cell. It is a quick way to let non-programmers change inputs.
#@title Training settingslearning_rate = 0.001 #@param {type:"number"}epochs = 10 #@param {type:"slider", min:1, max:50}model_type = "mlp" #@param ["mlp", "gradient_boosting"]use_class_weights = True #@param {type:"boolean"}Outside Colab these are ordinary comments, so the notebook still runs in JupyterLab.
AI assistance
Section titled “AI assistance”Colab includes Gemini-based features: generating code from a natural-language prompt, explaining code, and suggesting fixes when a cell raises an error, plus an agent that can plan and run multi-step analyses on request. Availability and limits depend on your plan and region, and the menus change often, so look for the Gemini button in the notebook toolbar rather than memorized shortcuts. Treat generated code like any other code: read it before running it, especially anything that deletes files or calls external services.
GitHub
Section titled “GitHub”- Open a notebook from GitHub: File > Open notebook > GitHub, or replace
github.comwithcolab.research.google.com/githubin a notebook’s URL. - Save a copy to GitHub: File > Save a copy in GitHub (authorize once; you choose the repository, branch and commit message).
- Clone a repository to use its code:
!git clone https://github.com/ordersportal-demo/analytics.gitthen%cd analytics.
To push from the runtime, use a fine-grained personal access token stored in Colab Secrets and configure it for the clone rather than typing it into a cell, and set user.name and user.email with git config first. For regular development, prefer working in a local clone and using Colab only to run notebooks.
Training on a GPU
Section titled “Training on a GPU”The training loop below uses synthetic delivery data so it runs on any runtime. It moves the model and each batch to the same device and saves a checkpoint to Drive after every epoch, so a disconnect costs at most one epoch.
# fragment: run in Colab after mounting Drive; not run here (PyTorch not installed in this environment)from pathlib import Pathimport torchfrom torch import nnfrom torch.utils.data import DataLoader, TensorDataset
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")torch.manual_seed(42)
# Synthetic features: distance_km, items, carrier_load, weekend flag -> late (1) or on time (0)X = torch.rand(5000, 4)y = ((X[:, 0] * 0.6 + X[:, 2] * 0.5 + X[:, 3] * 0.2) > 0.7).long()loader = DataLoader(TensorDataset(X, y), batch_size=128, shuffle=True)
model = nn.Sequential(nn.Linear(4, 32), nn.ReLU(), nn.Dropout(0.1), nn.Linear(32, 2)).to(device)optimizer = torch.optim.Adam(model.parameters(), lr=1e-3)loss_fn = nn.CrossEntropyLoss()
ckpt_path = Path("/content/drive/MyDrive/ordersportal/late_delivery.pt")ckpt_path.parent.mkdir(parents=True, exist_ok=True)start_epoch = 0if ckpt_path.exists(): # resume after a disconnect ckpt = torch.load(ckpt_path, map_location=device) model.load_state_dict(ckpt["model"]) optimizer.load_state_dict(ckpt["optimizer"]) start_epoch = ckpt["epoch"] + 1
for epoch in range(start_epoch, 10): model.train() total = 0.0 for xb, yb in loader: xb, yb = xb.to(device), yb.to(device) optimizer.zero_grad() loss = loss_fn(model(xb), yb) loss.backward() optimizer.step() total += loss.item() print(f"epoch {epoch + 1}: loss {total / len(loader):.4f}") torch.save({"epoch": epoch, "model": model.state_dict(), "optimizer": optimizer.state_dict()}, ckpt_path)Pitfalls:
- Tensors on different devices. “Expected all tensors to be on the same device” means a batch or a new tensor was not moved with
.to(device). - Checkpointing only at the end. A ten-hour run that saves once is lost when the runtime resets in hour nine.
- Reading data from Drive inside the loop. Copy it to
/contentfirst; Drive I/O can leave the GPU idle most of the time.
GPU memory
Section titled “GPU memory”When a run fails with “CUDA out of memory”, in order of effort: reduce the batch size; use automatic mixed precision, which runs most operations in 16-bit and roughly halves activation memory; use gradient accumulation to keep the effective batch size; and, for Hugging Face models, enable gradient checkpointing (model.gradient_checkpointing_enable()), which trades compute for memory. Current PyTorch spells mixed precision with the device type; the older torch.cuda.amp names are deprecated.
# fragment: mixed precision inside the training loop abovescaler = torch.amp.GradScaler("cuda")for xb, yb in loader: xb, yb = xb.to(device), yb.to(device) optimizer.zero_grad() with torch.amp.autocast("cuda", dtype=torch.float16): loss = loss_fn(model(xb), yb) scaler.scale(loss).backward() scaler.step(optimizer) scaler.update()del large tensors you no longer need and call torch.cuda.empty_cache() to return cached memory, and check usage with !nvidia-smi or torch.cuda.memory_allocated(). If memory stays full after an error, restarting the session is often the quickest fix.
Monitoring training
Section titled “Monitoring training”TensorBoard runs inline in Colab: %load_ext tensorboard and %tensorboard --logdir /content/logs, with PyTorch writing scalars through torch.utils.tensorboard.SummaryWriter. Write the log directory to Drive if you want to keep it. Hosted experiment trackers such as Weights & Biases or MLflow work too; store their API keys in Secrets and log the configuration (learning rate, epochs, data version) with each run so results stay comparable.
Saving work before a reset
Section titled “Saving work before a reset”When a runtime is recycled, everything on the VM is gone: variables, installed packages, and every file under /content that is not in Drive. Before long or important runs:
- Save the notebook (it lives in Drive, but unsaved edits are not).
- Write outputs, checkpoints and logs to Drive, or push them to Cloud Storage or a Git repository.
- Download small results with
files.download(). - Make the first cells rebuild the environment: installs, Drive mount, imports and data copy.
Interview questions
Section titled “Interview questions”1. Why might a Colab notebook that ran yesterday fail today?
The runtime image changed (a preinstalled package was upgraded), a %pip install at the top was unpinned, a different accelerator was assigned, or files the notebook expected under /content were lost with the old VM. Pin installs, check versions in the first cell, and keep data in Drive or Cloud Storage.
2. How should a Colab notebook handle API keys?
Store them in Colab Secrets and read them with google.colab.userdata.get(). They stay out of the notebook file, are not shared with it, and each collaborator grants access separately. Never print them.
3. What makes training on a free GPU robust to disconnects? Saving model and optimizer state to Drive every epoch (or every N steps) and resuming from the latest checkpoint on start, copying data to local disk for speed, and code that falls back to CPU when no GPU is assigned.
4. When is Colab the wrong tool? For unattended multi-day jobs, production pipelines, workloads with strict data residency or confidentiality requirements, or teams that need identical pinned environments. Use a VM, a managed training service, Colab Enterprise or a JupyterHub instead.
5. Senior scenario: an analytics team wants to standardize on Colab for model experiments with customer data. What do you recommend? Separate prototyping from regulated data: consumer Colab for public or synthetic data and teaching, and Colab Enterprise or Vertex AI Workbench inside the company’s Google Cloud project (with IAM, VPC controls and audit logs) for customer data. Standardize a starter notebook that pins packages, mounts the approved storage, reads credentials from a secret store and checkpoints to project storage, and move anything scheduled into a pipeline rather than a browser session.
Related pages
Section titled “Related pages”- Jupyter notebooks: kernels, magics and rich output — the notebook basics Colab builds on
- Jupyter best practices and tools — reproducibility, version control and testing for any notebook
- Google Cloud overview — the platform behind Colab Enterprise and Vertex AI
- Python packages for AI and machine learning — the libraries you will use in Colab