Tensor Flow 📂 Tensors · 2 of 6 29 min read

TensorFlow Setup: Google Colab, Local Install and GPU Check

Set up TensorFlow 2.21 three ways: Google Colab, a local pip install in a virtual environment, or Docker. Build the exact install commands for your system. Learn how TensorFlow reaches the GPU, check which devices it can see, stop it from grabbing all GPU memory, and fix the most common setup errors.

Section 01

The Story — Renting a Kitchen vs Building One

Cloud Kitchen or Home Kitchen?
You want to start cooking today. You have two choices.

You can rent a cloud kitchen. It is ready now. It even has a big oven. But you lose your work if you leave it for too long.

Or you can build your own kitchen. It takes some setup. But it is yours. Nothing gets reset, and you can use it offline.

Google Colab is the cloud kitchen. A local install is your home kitchen. The big oven is the GPU. It makes deep learning many times faster.

In this lesson you will set up TensorFlow in three ways. Then you will check if TensorFlow can see a GPU. This course uses TensorFlow 2.21. It supports Python 3.10 to 3.13.


Section 02

Pick Your Setup

☁️
Google Colab
zero install
Runs in the browser. TensorFlow is already installed. Free GPU for short sessions.
+ Ready in 30 seconds
− Session resets, usage limits
💻
Local pip install
pip + venv
Install on your own laptop or PC inside a virtual environment.
+ Works offline, files stay
− GPU setup depends on OS
🐳
Docker
tensorflow/tensorflow
An official image with everything inside. Good for servers and teams.
+ Same setup everywhere
− Need to learn Docker
🎓
Advice for Beginners

Start with Colab. You can learn the whole of this course there. Set up a local install later, when you build your own projects.


Section 03

Option 1 — Google Colab

☁️ Colab in Four Steps
Step 1
Go to colab.research.google.com and sign in with a Google account.
Step 2
Click New notebook.
Step 3
Open Runtime → Change runtime type. Pick a GPU, for example T4 GPU. Click Save.
Step 4
Run the check below in the first cell.
import tensorflow as tf

print("TensorFlow:", tf.__version__)
print("GPUs:", tf.config.list_physical_devices('GPU'))
OUTPUT — Colab with T4 GPU (version may differ)
TensorFlow: 2.21.0 GPUs: [PhysicalDevice(name='/physical_device:GPU:0', device_type='GPU')]

You can also see the GPU card itself. Put ! in front to run a shell command in Colab.

!nvidia-smi
⏳
Colab Sessions Do Not Last Forever

A free session stops after some idle time, and files in /content are deleted. Save your work to Google Drive. Mount it with drive.mount('/content/drive') after from google.colab import drive.


Section 04

Option 2 — Local Install with pip

01
Check Python
Run python --version. You need 3.10, 3.11, 3.12 or 3.13, and 64-bit Python.
02
Create a Virtual Environment
A venv keeps TensorFlow and its packages away from other projects. This avoids version fights.
03
Upgrade pip
Old pip versions cannot find the new TensorFlow wheels.
04
Install TensorFlow
One command. On Linux with an NVIDIA GPU, add the [and-cuda] extra.
05
Verify
Import TensorFlow and run one small sum.

Linux and macOS

python3 -m venv tf-env
source tf-env/bin/activate
pip install --upgrade pip
pip install tensorflow              # CPU (and Apple Silicon Macs)
# pip install 'tensorflow[and-cuda]'  # Linux + NVIDIA GPU

Windows (PowerShell)

py -m venv tf-env
tf-env\Scripts\activate
python -m pip install --upgrade pip
pip install tensorflow              # CPU only on native Windows

Verify the Install

import tensorflow as tf

print(tf.__version__)
print(tf.reduce_sum(tf.random.normal([1000, 1000])).dtype)
print("Works!")
OUTPUT
2.21.0 <dtype: 'float32'> Works!
PlatformCPUGPU SupportHow
Linux (x86_64)YesNVIDIApip install 'tensorflow[and-cuda]'
Windows nativeYesNoGPU ended after TF 2.10. Use WSL2.
Windows + WSL2YesNVIDIAFollow the Linux steps inside Ubuntu on WSL2
macOS Apple SiliconYesPluginOptional tensorflow-metal. Check it supports your TF version.
macOS IntelNo new buildsNoUse Colab or Docker

Section 05

Try It — Install Command Builder

🔧 Build Your Install Commands Interactive

Choose your system. The commands update live. Copy them into your terminal.


Section 06

How TensorFlow Talks to the GPU

Your Python code does not talk to the GPU directly. It passes through several layers. If any layer is missing, TensorFlow quietly falls back to the CPU.

Animated Diagram — The GPU Software Stack
Your Python code (model.fit, tf.matmul) TensorFlow 2.21 (picks GPU:0 if found) CUDA 12 + cuDNN 9 (pip [and-cuda] installs these) NVIDIA driver (you install once — check nvidia-smi) GPU hardware — thousands of small cores layer 1layer 2layer 3layer 4layer 5 request ↓result ↑

The request travels down to the GPU. The result travels back up. Layers 2 and 3 come from pip. Layer 4 you install yourself.


Section 07

GPU Check — The Code

Run this block on any machine. It tells you what TensorFlow can see.

import tensorflow as tf

gpus = tf.config.list_physical_devices('GPU')
print("Num GPUs available:", len(gpus))
for gpu in gpus:
    print("  ", gpu.name, tf.config.experimental.get_device_details(gpu).get('device_name'))

info = tf.sysconfig.get_build_info()
print("Built with CUDA:", info.get('is_cuda_build'))
print("CUDA version   :", info.get('cuda_version'))
print("cuDNN version  :", info.get('cudnn_version'))
✅ Colab / Linux with GPU
Num GPUs available: 1
  /physical_device:GPU:0 Tesla T4
Built with CUDA: True
CUDA version   : 12.5.1
cuDNN version  : 9
💻 Laptop, CPU only
Num GPUs available: 0
 
Built with CUDA: False (Windows / Mac)
CUDA version   : None
cuDNN version  : None
🔎
Zero GPUs Is Not an Error

TensorFlow runs fine on the CPU. It is just slower for big models. All code in the first modules of this course runs well on a CPU.

Where Did My Op Run?

Every tensor remembers its device. You can also force a device with tf.device.

a = tf.random.normal([3, 3])
print("a lives on:", a.device)

with tf.device('/CPU:0'):
    b = tf.matmul(a, a)
print("b lives on:", b.device)
OUTPUT
a lives on: /job:localhost/replica:0/task:0/device:CPU:0 b lives on: /job:localhost/replica:0/task:0/device:CPU:0

On a GPU machine, a.device ends with GPU:0.

Stop TensorFlow from Taking All GPU Memory

By default TensorFlow grabs almost all GPU memory at start. This blocks other notebooks. Turn on memory growth at the very top of your script.

gpus = tf.config.list_physical_devices('GPU')
for gpu in gpus:
    tf.config.experimental.set_memory_growth(gpu, True)   # must run before any op

Feel the Speed — CPU vs GPU

import time

def bench(device, n=4000):
    with tf.device(device):
        x = tf.random.normal([n, n])
        tf.matmul(x, x)                       # warm-up run
        start = time.time()
        y = tf.matmul(x, x)
        _ = y.numpy()                         # wait until finished
    return time.time() - start

print(f"CPU: {bench('/CPU:0'):.3f} s")
if tf.config.list_physical_devices('GPU'):
    print(f"GPU: {bench('/GPU:0'):.3f} s")
SAMPLE OUTPUT — Colab T4 (your numbers will differ)
CPU: 1.9 s (approx.) GPU: 0.03 s (approx.)

Section 08

Try It — Setup Troubleshooter

🧰 What Went Wrong? Interactive

Pick the problem you see. You get the likely cause and the fix.


Section 09

Golden Rules

🔧 Setup — Rules to Remember
1
Learning? Use Colab. It needs no install and has a free GPU.
2
Always install inside a virtual environment. One venv per project.
3
On Linux with NVIDIA, use pip install 'tensorflow[and-cuda]'. You only need the driver yourself.
4
On Windows, use WSL2 for GPU. Native Windows is CPU only.
5
Check your GPU with tf.config.list_physical_devices('GPU'). An empty list means CPU only.
6
Turn on memory growth if you share a GPU between notebooks.