The Story — Renting a Kitchen vs Building One
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.
Pick Your Setup
Start with Colab. You can learn the whole of this course there. Set up a local install later, when you build your own projects.
Option 1 — Google Colab
import tensorflow as tf
print("TensorFlow:", tf.__version__)
print("GPUs:", tf.config.list_physical_devices('GPU'))
You can also see the GPU card itself. Put ! in front to run a shell command in Colab.
!nvidia-smi
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.
Option 2 — Local Install with pip
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!")
| Platform | CPU | GPU Support | How |
|---|---|---|---|
| Linux (x86_64) | Yes | NVIDIA | pip install 'tensorflow[and-cuda]' |
| Windows native | Yes | No | GPU ended after TF 2.10. Use WSL2. |
| Windows + WSL2 | Yes | NVIDIA | Follow the Linux steps inside Ubuntu on WSL2 |
| macOS Apple Silicon | Yes | Plugin | Optional tensorflow-metal. Check it supports your TF version. |
| macOS Intel | No new builds | No | Use Colab or Docker |
Try It — Install Command Builder
Choose your system. The commands update live. Copy them into your terminal.
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.
The request travels down to the GPU. The result travels back up. Layers 2 and 3 come from pip. Layer 4 you install yourself.
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'))
| Num GPUs available: 1 |
| /physical_device:GPU:0 Tesla T4 |
| Built with CUDA: True |
| CUDA version : 12.5.1 |
| cuDNN version : 9 |
| Num GPUs available: 0 |
| Built with CUDA: False (Windows / Mac) |
| CUDA version : None |
| cuDNN version : None |
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)
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")
Try It — Setup Troubleshooter
Pick the problem you see. You get the likely cause and the fix.
Golden Rules
pip install 'tensorflow[and-cuda]'. You only need the driver yourself.tf.config.list_physical_devices('GPU'). An empty list means CPU only.