How to Use GPU in Google Colab: Step-by-Step
In this article
To use a GPU in Google Colab, open your notebook, go to Runtime > Change runtime type, select GPU under Hardware accelerator, and click Save. Colab will then reconnect to a machine with a GPU. You can verify it by running !nvidia-smi in a code cell, which lists the GPU model and memory. This guide walks you through the full process, from enabling the GPU to writing code that actually uses it, plus common pitfalls and fixes.
What You'll Need
- A Google account (a free one works; Colab offers limited free GPU time).
- Access to Google Colab at colab.research.google.com.
- A notebook open (new or existing).
- Basic familiarity with running cells (Shift+Enter).
No local GPU or installation is required; the GPU runs in the cloud. Note that menu labels can differ slightly by Colab version or region, but the core path is stable.
Step-by-Step
- Open your notebook. Go to colab.research.google.com and open a notebook or create a new one.
- Open Runtime settings. In the top menu, click Runtime > Change runtime type. A dialog appears.
- Select GPU. Under Hardware accelerator, choose GPU from the dropdown. (Options may include T4, P100, or others; you cannot pick a specific model, Colab assigns one.)
- Save. Click Save. Colab will restart the runtime and connect you to a GPU-enabled machine. This may take a few seconds.
- Verify the GPU. In a new code cell, run
!nvidia-smi. You should see a table with the GPU name, memory usage, and driver version. If you see an error like "command not found," the GPU is not enabled. - Check with Python. Alternatively, run
import torch; print(torch.cuda.is_available())if you use PyTorch. It should printTrue. For TensorFlow, runimport tensorflow as tf; print(tf.config.list_physical_devices('GPU')). - Write GPU-aware code. For PyTorch, move tensors and models to the GPU:
device = torch.device('cuda' if torch.cuda.is_available() else 'cpu'), thenmodel.to(device)andtensor.to(device). For TensorFlow, GPU usage is automatic for supported operations; you can verify withtf.config.list_physical_devices('GPU'). - Run your training or computation. Execute your cells. The GPU will be used for operations that support it. Monitor usage with
!nvidia-smiin a separate cell (run it periodically) to see memory and utilization. - Manage sessions. Colab disconnects after a period of inactivity (often around 90 minutes) and has a maximum session length (up to 12 hours on free tier). Save your work to Google Drive or download it. To reconnect with GPU, repeat steps 2–4 if needed.
Common Mistakes
- Forgetting to switch runtime type. Many users write GPU code but leave the accelerator set to None or CPU, so it runs slowly on CPU.
- Not moving data to the GPU. In PyTorch, if you move the model but not the input tensors (or vice versa), you'll get a device mismatch error.
- Assuming all operations use the GPU. Some operations are CPU-only; check documentation. Also, small operations may not benefit from GPU.
- Ignoring memory limits. Free Colab GPUs have limited memory (often 12–16 GB). Large batch sizes or models can cause out-of-memory errors.
- Leaving the tab idle. Colab disconnects idle sessions, losing unsaved state. Use a keep-alive approach or save frequently.
- Expecting a specific GPU model. Colab assigns available GPUs; you cannot choose. The model may vary between sessions.
Tips
- Use
!nvidia-smiregularly to monitor GPU memory and utilization. - For PyTorch, use
torch.cuda.empty_cache()to free unused memory, but note it doesn't always help. - Save checkpoints to Google Drive to avoid losing progress on disconnect.
- Use smaller batch sizes if you hit memory errors; gradient accumulation can help simulate larger batches.
- Consider Colab Pro for longer runtimes and better GPUs, but free tier is fine for learning.
- When using TensorFlow, enable GPU memory growth to avoid allocating all memory at once:
tf.config.experimental.set_memory_growth(gpu, True).
Troubleshooting
If !nvidia-smi shows "command not found" or no GPU, first confirm you saved the runtime type change. Then try Runtime > Disconnect and delete runtime, then reconnect and re-check. If still no GPU, you may have hit usage limits (free tier has quotas). Wait and try later, or consider Colab Pro. If you get a CUDA out-of-memory error, reduce batch size, use a smaller model, or clear memory with torch.cuda.empty_cache(). If PyTorch says CUDA not available but nvidia-smi works, ensure you installed the GPU version of PyTorch (Colab preinstalls it, but custom installs can break it). Reinstall with !pip install torch --upgrade if needed. For TensorFlow, if GPU not detected, check that you are using a GPU runtime and that TensorFlow version supports CUDA (Colab's preinstalled version does). Sometimes a factory reset of the runtime (Runtime > Disconnect and delete runtime) fixes detection issues. If you see "Cannot connect to GPU," it's often a temporary Colab issue; retry after a few minutes.
Frequently Asked Questions
Can I choose which GPU model I get?
No, Colab assigns an available GPU automatically. You cannot select a specific model like T4 or P100.
How long can I use a GPU in Colab for free?
Free tier has dynamic limits, but sessions typically last up to 12 hours and disconnect after about 90 minutes of inactivity. Usage is subject to quotas.
Do I need to install CUDA or drivers?
No, Colab's GPU runtime comes preconfigured with CUDA and drivers. You just need to enable the GPU and use compatible libraries.
Why is my code still slow after enabling GPU?
Your code may not be using the GPU. Check that you moved data and model to the GPU (PyTorch) or that operations are GPU-supported (TensorFlow). Also, some tasks are inherently CPU-bound.
Can I use the GPU with other libraries like scikit-learn?
Most scikit-learn algorithms are CPU-only. GPU acceleration requires libraries with GPU support, such as PyTorch, TensorFlow, or RAPIDS (cuDF, cuML).
What happens to my files when the runtime disconnects?
Files saved only in the Colab VM are lost. Save important data to Google Drive or download it before disconnecting.