Menu

Setting Up TensorFlow

Setting Up TensorFlow

Getting TensorFlow installed is a five-minute process when you follow the right steps. The section below covers the standard CPU installation and the GPU-enabled setup, plus how to verify everything is working correctly.

Installing TensorFlow with pip

# Step 1: Create a virtual environment

python -m venv tf_env

# Step 2: Activate it

# macOS / Linux:

source tf_env/bin/activate

# Windows (Command Prompt):

tf_env\Scripts\activate

# Windows (PowerShell):

tf_env\Scripts\Activate.ps1

# Step 3: Upgrade pip to avoid resolver issues

pip install --upgrade pip

# Step 4: Install TensorFlow

pip install tensorflow       # CPU-only (works everywhere)

# Step 5: Verify the installation

python -c "import tensorflow as tf; print(tf.__version__)"

# Expected: 2. x.x  (e.g. 2.20.0 as of August 2025)

Common Installation Issues

On Windows: if you see a 'Microsoft Visual C++ Redistributable' error, download and install it from Microsoft's official website, then retry. On Linux: verify your GLIBC version with ldd --version (TF 2.x needs GLIBC 2.17+). On macOS Apple Silicon: TensorFlow 2.13+ includes native Metal GPU support; install with pip install tensorflow-macos tensorflow-metal.

GPU vs CPU Setup

By default, the pip install tensorflow command installs a CPU-only build. This is perfectly sufficient for learning and for running the examples in this tutorial. However, once you start training deeper models on larger datasets, a GPU can cut training time by 10x to 50x.

import tensorflow as tf

# Check physical devices

gpus = tf.config.list_physical_devices('GPU')

cpus = tf.config.list_physical_devices('CPU')

print(f'CPUs available: {len(cpus)}')

print(f'GPUs available: {len(gpus)}')

if gpus:

for gpu in gpus:

print(f'  {gpu.name}  ({gpu.device_type})')

# Optional: enable memory growth so TF doesn't grab all VRAM

tf.config.experimental.set_memory_growth(gpus[0], True)

Else:
print('No GPU detected. Running on CPU.')

After setup, use this snippet to confirm TensorFlow can see your GPU. If gpus is an empty list, double-check your CUDA version against the compatibility table on tensorflow.org.

Free GPU Option

If you do not have a dedicated GPU, Google Colab provides free T4 GPU access with TensorFlow pre-installed. Open colab.research.google.com, click Runtime > Change runtime type, select GPU, and run your training notebooks there. Kaggle Notebooks also offer 30 hours per week of free GPU time.