Introduction

Before writing any TensorFlow or Keras code, the first practical step is getting a working installation set up correctly — including choosing between CPU-only and GPU-accelerated versions, managing Python environments, and verifying that everything is functioning as expected. While the process is generally straightforward, a few common pitfalls (mismatched CUDA versions, conflicting environments, incorrect package names) trip up many newcomers, making a clear installation walkthrough a worthwhile first step.

Since TensorFlow 2.x, Keras ships bundled directly inside TensorFlow as tf.keras, meaning a single installation typically gives access to both — though standalone Keras 3 (covered later in this section) can also be installed separately for its multi-backend capabilities.

Why Does Proper Installation Matter?

Getting installation right helps to:

  • Avoid frustrating environment conflicts and version mismatch errors down the line
  • Ensure GPU acceleration is actually being used when available, for much faster training
  • Keep project dependencies isolated and reproducible using virtual environments
  • Confirm the installation works correctly before writing any real model code
  • Set a clean foundation for everything covered later in this section (Tensors, Keras APIs, etc.)
  • Avoid wasted debugging time caused by installation issues rather than actual code problems

The Installation Workflow

Whiteboard
Whiteboard diagram

Step 1: Check Python Compatibility

TensorFlow requires a specific range of supported Python versions, which changes with each TensorFlow release. Always check the current TensorFlow documentation for the exact supported version range before installing, since using an unsupported Python version is one of the most common installation failures.

Step 2: Create a Virtual Environment

Using a virtual environment keeps TensorFlow and its dependencies isolated from other projects, preventing version conflicts.

python -m venv tf-env
source tf-env/bin/activate        (macOS/Linux)
tf-env\Scripts\activate           (Windows)

Step 3: Install TensorFlow

pip install tensorflow

This single command installs TensorFlow along with tf.keras bundled inside it — no separate Keras installation is needed for standard TensorFlow 2.x usage.

GPU Support

Modern TensorFlow versions automatically include GPU support within the standard tensorflow package (rather than requiring a separate tensorflow-gpu package, as older versions once did), provided the correct NVIDIA drivers, CUDA, and cuDNN versions are installed on the system separately. Always check TensorFlow's current documentation for the exact required CUDA/cuDNN version pairing, since compatibility requirements change between releases.

Step 4: Verify the Installation

python
import tensorflow as tf

print("TensorFlow version:", tf.__version__)
print("GPU available:", tf.config.list_physical_devices('GPU'))

If a GPU is properly detected and configured, tf.config.list_physical_devices('GPU') will return a non-empty list showing the available GPU device(s); an empty list means TensorFlow will fall back to CPU-only execution.

Installing Standalone Keras 3 (Multi-Backend)

As covered in the earlier Keras topic, Keras 3 can run on TensorFlow, PyTorch, or JAX as its backend. To use this multi-backend version explicitly:

pip install keras
python
import os
os.environ["KERAS_BACKEND"] = "tensorflow"   # or "torch", "jax"
import keras

Common Installation Methods Compared

MethodDescriptionBest For
pip install tensorflowStandard installation via Python's package managerMost users, straightforward setup
conda install tensorflowInstallation via Anaconda/Miniconda environmentsUsers already working within the conda ecosystem
Docker ImagesPre-built TensorFlow containers with dependencies includedConsistent, reproducible environments across machines
Google ColabNo local installation needed — runs in the browser with free GPU accessQuick experimentation without any local setup

CPU-Only vs GPU-Enabled Setup

AspectCPU-OnlyGPU-Enabled
Installation ComplexitySimple — works out of the box after pip installRequires matching NVIDIA drivers, CUDA, and cuDNN versions
Training SpeedSlower, especially for larger modelsSignificantly faster for most deep learning workloads
Hardware RequirementAny standard machineRequires a compatible NVIDIA GPU
Best ForLearning, small-scale experimentation, CPU-bound tasksTraining larger models, production workloads

Key Properties of the Installation Process

  • TensorFlow 2.x includes tf.keras bundled in, so one installation covers both by default.
  • Standard pip install tensorflow now includes GPU support automatically, given the correct system-level NVIDIA setup.
  • Virtual environments are strongly recommended to avoid dependency conflicts between projects.
  • Verifying the installation with a quick version and GPU-availability check catches most setup issues immediately.
  • Standalone Keras 3 can be installed separately for its multi-backend (TensorFlow/PyTorch/JAX) capabilities.

Where Does Correct Installation Matter Most?

ContextWhy It Matters
Local DevelopmentAvoiding environment conflicts across multiple projects
GPU-Accelerated TrainingEnsuring CUDA/cuDNN versions are correctly matched for speed
Team/Collaborative ProjectsReproducible environments prevent "works on my machine" issues
Cloud/Production DeploymentConsistent, verified installations across deployment environments
Educational/Learning ContextsA smooth first setup avoids early frustration before real learning begins

Advantages

  • Modern installation via pip is simple and well-documented for most use cases
  • GPU support is now bundled into the standard package rather than requiring a separate install
  • Virtual environments and Docker options provide strong isolation and reproducibility
  • Free cloud options like Google Colab remove the installation step entirely for quick experimentation
  • Verification steps are quick and clearly indicate whether setup succeeded

Limitations

  • GPU setup still requires correctly matching NVIDIA driver, CUDA, and cuDNN versions outside of pip itself
  • Version compatibility between Python, TensorFlow, and GPU libraries can change between releases
  • Some operating system and hardware combinations (e.g., certain ARM-based systems) have more limited support
  • Installation issues can be genuinely difficult to debug for newcomers unfamiliar with environment management
  • Standalone Keras 3 backend switching adds an extra configuration step beyond default TensorFlow installation

Real-World Examples

ScenarioInstallation Approach
Learning TensorFlow for the first timepip install tensorflow, or use Google Colab to skip setup entirely
Setting up a GPU workstation for trainingpip install tensorflow + manually installed matching CUDA/cuDNN
Deploying to a reproducible production environmentDocker image with a pinned TensorFlow version
Team project with multiple contributorsVirtual environment with a requirements.txt pinning exact versions
Experimenting with Keras 3's multi-backend featurepip install keras + setting the KERAS_BACKEND environment variable

Best Practices

  • Always use a virtual environment (or conda environment) rather than installing TensorFlow globally.
  • Check TensorFlow's official documentation for the current supported Python and CUDA/cuDNN version matrix before installing.
  • Verify the installation immediately with a version check and GPU availability check.
  • Use Google Colab for quick experimentation if local GPU setup isn't readily available.
  • Pin exact package versions in a requirements file for reproducible team or production environments.

Interview Tip

A common interview question is:

"What's included when you run pip install tensorflow, and do you need to install Keras separately?"

A strong answer is:

Running pip install tensorflow installs TensorFlow along with tf.keras bundled directly inside it, so no separate Keras installation is needed for standard usage — this has been the case since TensorFlow 2.x, when Keras became TensorFlow's official high-level API. GPU support is also included in the standard package rather than requiring a separate tensorflow-gpu install like in older versions, though it still depends on the correct NVIDIA drivers, CUDA, and cuDNN being installed separately at the system level. Standalone Keras 3, with its multi-backend support for TensorFlow, PyTorch, and JAX, can also be installed separately via pip install keras if that flexibility is specifically needed.

Mentioning the historical shift away from separate tensorflow-gpu packages shows current, accurate knowledge.

Conclusion

A correct, verified installation is the essential first step before diving into TensorFlow and Keras, and modern tooling has made this process considerably simpler than in earlier years — a single pip install tensorflow now covers both TensorFlow and tf.keras, with GPU support included by default given the right system setup. With installation covered, the next topic explores Tensors, the fundamental data structure that everything in TensorFlow is built around.