iwantcoding.com
🔥 Daily 👥 Rooms 🏆 Top Log in Sign up

Install / Colab

Installing TensorFlow with the right CUDA / Metal / CPU configuration. The two-minute install that actually works on your hardware.

TensorFlow — install

EXAMPLE
# ===== 1. Pick a Python env =====
python -m venv .venv
source .venv/bin/activate
pip install --upgrade pip

# Or conda:
conda create -n tf python=3.11
conda activate tf

# ===== 2. Install (CPU) =====
pip install tensorflow
# Works on macOS / Linux / Windows. Larger wheel.

# Apple Silicon (Mac M-series):
pip install tensorflow tensorflow-metal
# tensorflow-metal enables GPU acceleration through the Mac GPU.

# ===== 3. Install (GPU on Linux / Windows) =====
# Requires CUDA + cuDNN matching the TF version.
# Easiest path: use the official Docker images.
docker run --gpus all -it tensorflow/tensorflow:latest-gpu bash

# Direct pip + system CUDA:
pip install tensorflow[and-cuda]
# Reads CUDA from system; verify with the GPU check below.

# ===== 4. Verify =====
python - <<'PY'
import tensorflow as tf
print('tf', tf.__version__)
print('GPU:', tf.config.list_physical_devices('GPU'))
print('CPU:', tf.config.list_physical_devices('CPU'))

a = tf.constant([1.0, 2.0, 3.0])
b = tf.constant([10.0, 20.0, 30.0])
print(a + b)
PY

# ===== 5. Optional: Keras 3 (multi-backend) =====
pip install keras
# Set backend via env var:
KERAS_BACKEND=tensorflow python ...
# Or 'jax' or 'torch'.

# ===== 6. Smoke train =====
python - <<'PY'
import numpy as np, tensorflow as tf
from tensorflow.keras import Sequential, layers
X = np.random.rand(100, 4); y = np.random.randint(0, 3, 100)
m = Sequential([
    layers.Dense(32, activation='relu', input_shape=(4,)),
    layers.Dense(3, activation='softmax'),
])
m.compile('adam', 'sparse_categorical_crossentropy', metrics=['accuracy'])
m.fit(X, y, epochs=5, verbose=0)
print(m.evaluate(X, y, verbose=0))
PY

# ===== 7. Common companions =====
pip install tensorflow-datasets tensorboard tf-models-official tensorflow-hub
# Useful: tensorboard for live training curves.

# ===== Patterns to internalise =====
# - Pin TF version in requirements.txt
# - For GPU, prefer official Docker over wrestling CUDA on host
# - Set seeds for reproducibility (random, numpy, tf)
# - Use tf.data.cache().prefetch() in input pipelines

# ===== Pitfalls =====
# - CUDA / cuDNN mismatch -> 'Could not load dynamic library'
# - Apple Silicon without tensorflow-metal -> slow CPU only
# - Mixing tensorflow + tensorflow-cpu wheels -> silent conflicts
# - 'pip install tf-nightly' on a stable project; nightlies churn fast

Why it matters

venv + pip install tensorflow is the floor. Apple Silicon adds tensorflow-metal; Linux + GPU is easiest via Docker. Verify with a one-line GPU check, smoke train a tiny model, and pin the version. After that you are in the deep end with everyone else.

Tip: Tweak the snippet with Try it Yourself », then sit the quiz at the bottom of the page.

Example

Example
pip install tensorflow
# Verify
python -c 'import tensorflow as tf; print(tf.__version__)'
Try it Yourself »

Discussion

Loading…