Keras
GPU
installation
deep learning
machine learning

How to install Keras with gpu support?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Introduction

Installing Keras with GPU support is not really a “Keras-only” setup problem anymore. In current Keras, GPU access comes from the backend you run underneath it, most commonly TensorFlow, JAX, or PyTorch. That means the practical question is usually: how do I install Keras with a backend that can see my GPU reliably?

For most users, the simplest path is Keras 3 with the TensorFlow backend. On a supported Linux system, or on Windows through WSL2, that usually means installing keras and a GPU-enabled TensorFlow environment in a clean virtual environment, then verifying that TensorFlow detects the device before you start training models.

Understand Where GPU Support Comes From

Keras 3 is multi-backend. You install Keras itself, then choose a backend with KERAS_BACKEND. If you want CUDA GPU acceleration with the TensorFlow backend, you should configure TensorFlow for GPU use first.

A clean virtual environment is the safest starting point:

bash
1python3 -m venv .venv
2source .venv/bin/activate
3python -m pip install --upgrade pip
4pip install --upgrade keras tensorflow[and-cuda]

That tensorflow[and-cuda] extra is the important part for the TensorFlow backend on supported platforms. Keras itself does not ship a separate modern “GPU edition.”

Before importing Keras, set the backend if you want to be explicit:

bash
export KERAS_BACKEND=tensorflow

Then verify the install:

python
1import tensorflow as tf
2import keras
3
4print("TensorFlow:", tf.__version__)
5print("Keras:", keras.__version__)
6print("GPUs:", tf.config.list_physical_devices("GPU"))

If the GPU list is empty, fix the environment first. Do not start debugging model code until device detection is working.

Check the Platform Requirements First

A lot of failed installs happen because the platform is not aligned with current TensorFlow GPU support.

In practice, the most stable path is:

  • Linux with an NVIDIA GPU and current driver
  • Windows by using WSL2 with an NVIDIA GPU
  • a clean Python environment dedicated to this stack

Native macOS does not use the NVIDIA CUDA path described by TensorFlow GPU instructions. Native Windows TensorFlow GPU support after TensorFlow 2.10 is not the normal path either, which is why WSL2 is the safer recommendation there.

You also need the NVIDIA driver installed on the host. Without a working driver, Python package installation may succeed while runtime GPU detection still fails.

A quick host-level check is:

bash
nvidia-smi

If that command fails, solve the driver issue before touching Python packages again.

Run a Small Keras GPU Test

Once TensorFlow sees the GPU, run a very small Keras program. This confirms the backend, import path, and training loop are all healthy.

python
1import os
2os.environ["KERAS_BACKEND"] = "tensorflow"
3
4import numpy as np
5import keras
6from keras import layers
7
8x = np.random.random((512, 20)).astype("float32")
9y = np.random.randint(0, 2, size=(512, 1)).astype("float32")
10
11model = keras.Sequential([
12    layers.Dense(32, activation="relu", input_shape=(20,)),
13    layers.Dense(1, activation="sigmoid"),
14])
15
16model.compile(optimizer="adam", loss="binary_crossentropy")
17model.fit(x, y, epochs=2, batch_size=32, verbose=1)

This example is intentionally small. The point is not speed, it is verifying that Keras can build, compile, and train successfully in the configured environment.

If you want stronger confirmation that ops land on the GPU, inspect TensorFlow startup logs or query the visible devices explicitly:

python
1import tensorflow as tf
2
3for device in tf.config.list_logical_devices():
4    print(device)

Keep the Environment Reproducible

GPU Python stacks are sensitive to version drift. Keep the environment isolated and write down the working versions.

bash
pip freeze > requirements.txt

For team use, pinning versions is worth the effort. It is much easier to reproduce a known-good environment than to recover one after an accidental package upgrade.

If you use Keras with a different backend, such as JAX or PyTorch, the same idea applies: install Keras, configure the backend, then follow that backend's GPU installation instructions. The Keras API stays similar, but the device setup comes from the selected backend.

Common Pitfalls

The most common mistake is searching for a separate keras-gpu package. That is not the modern installation model. GPU support comes from the backend.

Another common problem is mixing old TensorFlow, CUDA, and Python versions in the same environment. Clean virtual environments save time here.

A third issue is importing Keras before setting KERAS_BACKEND. Backend selection should happen before the first Keras import.

Finally, do not trust package installation alone. Always verify with tf.config.list_physical_devices("GPU") and a tiny training run.

Summary

  • Install Keras and a GPU-capable backend, rather than looking for a separate Keras GPU package.
  • For the TensorFlow backend, pip install keras tensorflow[and-cuda] is the current practical path on supported systems.
  • Use Linux or WSL2 with a working NVIDIA driver for the least friction.
  • Set KERAS_BACKEND=tensorflow before importing Keras if you want explicit backend selection.
  • Verify the GPU with TensorFlow device detection and a small Keras training run.

Course illustration
Course illustration

All Rights Reserved.