title: Google Colab description: Free hosted notebooks — Google Colab essentials: GPUs/TPUs, drive & data access, secrets, magics, and sharing. category: 25-IDEs-Editors tags: [colab, notebooks, gpu, cloud] status: ready last_updated: 2026-09-06
Google Colab¶
Free Jupyter notebooks with hosted runtimes — including GPU and TPU access. Zero setup, shareable like a Google Doc, and good enough for many ML experiments and tutorials.
Table of Contents¶
- Installation / Setup
- Core Syntax
- Common Operations
- Practical Examples
- Common Patterns
- Best Practices
- Common Mistakes
- Troubleshooting
- Related Cheat Sheets
- Official Documentation
Installation / Setup¶
- Go to colab.research.google.com → sign in.
- File → New notebook, or File → Upload notebook / open any GitHub
.ipynbvia URL.
Choose hardware: Runtime → Change runtime type:
| Runtime | Use for |
|---|---|
| CPU | small data, pandas, plotting |
| T4 GPU | deep learning (free tier) |
| A100 GPU | larger training (paid tiers) |
| TPU | specific TPU-compatible workloads |
Core Syntax¶
Environment & pre-installed packages¶
Colab ships with pandas, NumPy, scikit-learn, matplotlib, PyTorch, TensorFlow, and more. Install anything else per session:
Every session starts fresh — reinstalls and re-mounts are normal.
Data access¶
# 1. Upload a file (small; per-session)
from google.colab import files
files.upload() # browser dialog
# 2. Mount Google Drive (authorize once per session)
from google.colab import drive
drive.mount("/content/drive")
!ls /content/drive/MyDrive/data
# 3. Pull from the web (most reproducible)
!wget -q https://example.com/data.csv
!kaggle datasets download -d user/dataset # needs kaggle.json
Secrets (API keys)¶
from google.colab import userdata
key = userdata.get("OPENAI_API_KEY") # set via the key icon in the left panel
Never paste secrets into code cells — use the secrets manager so shared notebooks stay clean.
Common Operations¶
Save outputs back to Drive¶
df.to_csv("/content/drive/MyDrive/results/model_output.csv", index=False)
model.save("/content/drive/MyDrive/models/model.pt")
# And download to your computer
from google.colab import files
files.download("results/model_output.csv")
Notebook magics (same as Jupyter, plus):¶
%tensorflow_version 2.x # legacy pinning (usually unnecessary now)
%%capture # silence noisy installs
!nvidia-smi # GPU status
!free -h # RAM status
Sharing & GitHub¶
- Share: blue "Share" button → anyone with link (like Docs). Readers can copy.
- Save a copy: File → Save a copy in Drive / as GitHub gist.
- Open from GitHub:
colab.research.google.com/github/<owner>/<repo>/blob/<branch>/<path>.ipynb - Download: File → Download .ipynb / .py.
Save to GitHub¶
# File -> Save a copy in GitHub — needs a linked GitHub account
# then: git clone + git pull like any repo (see git.md)
Practical Examples¶
1. Free GPU training¶
# Runtime -> Change runtime type -> T4 GPU
import torch
x = torch.randn(1000, 1000, device="cuda")
y = (x @ x.T).sum()
print(y.item()) # works on GPU
2. Reproducible Colab start (a standard cell 1)¶
#@title Setup
!pip install -q pandas numpy matplotlib seaborn scikit-learn
!git clone https://github.com/you/project.git # pull your own code
%cd project
3. Use Drive as a dataset store¶
from google.colab import drive
drive.mount("/content/drive")
import pandas as pd
df = pd.read_csv("/content/drive/MyDrive/datasets/train.csv")
Common Patterns¶
# Pin versions to avoid session drift
!pip install -q "pandas==2.2.2" "scikit-learn==1.4.0"
# Run long trainings with checkpointing to Drive (session may time out!)
model.train()
torch.save(model.state_dict(), "/content/drive/MyDrive/checkpoints/last.pt")
# Keep API keys out of the notebook via userdata
Best Practices¶
- Treat sessions as ephemeral — files outside Drive/GitHub vanish when the runtime resets.
- Pin package versions so today's notebook works next month.
- Put data in Drive or GitHub, never re-upload by hand every session.
- Use the secrets manager for keys; never hardcode or print them.
- Save checkpoints to Drive for any run > ~30 minutes (free runtimes disconnect).
- Start from GitHub URLs for versioned, shareable notebooks.
- Commit the
.pyexport (File → Download .py) alongside the.ipynbfor review. - Respect free-tier limits: disconnect idle runtimes and keep Colab for prototyping, not production.
Common Mistakes¶
| Mistake | Consequence | Fix |
|---|---|---|
| Forgetting to enable GPU | Slow "deep learning" | Runtime → Change runtime type → GPU |
| Uploading data each session | Wasted time, lost state | Drive mount / GitHub clone |
| Hardcoding API keys in cells | Leaked secrets on share | userdata.get(...) |
| No version pins | Notebook breaks months later | Pin with == |
| Untracked long training | Work lost on disconnect | Checkpoint to Drive |
%cd inside repo |
Path confusion | Use absolute /content/... paths |
| Downloading huge files to /content | Runtime disk fills | Stream from Drive/cloud storage |
Troubleshooting¶
| Symptom | Cause | Fix |
|---|---|---|
CUDA out of memory |
Batch too big for T4 (16 GB) | Smaller batch, float16, gradient accumulation |
| Session expired mid-run | ~12h idle/max limits | Checkpoints to Drive; shorter runs |
ModuleNotFoundError after restart |
Fresh runtime | Re-run the setup cell (!pip install) |
| Drive permission error | Mount authorization | Re-run drive.mount and accept |
| Kaggle API auth fails | kaggle.json location |
Place in ~/.kaggle/; chmod 600 |
| Slow data loading | Reading from Drive repeatedly | Copy to /content first, or use Parquet |
Related Cheat Sheets¶
- Jupyter — magics and notebook habits (identical in Colab)
- JupyterLab — self-hosted alternative
- VS Code — local IDE option
- git.md — versioning Colab notebooks
- PyTorch · GPU — what GPUs unlock
- Home · Central Navigation Index
Official Documentation¶
Last Updated¶
2026-09-06 — Initial version.