tensorflow
GPU
machine learning
troubleshooting
deep learning

Tensorflow will not run on GPU

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

Introduction

When TensorFlow does not use the GPU, the root cause is usually not the model code. It is almost always an environment problem: unsupported platform, missing driver, incompatible installation path, or a Python environment that cannot see the GPU runtime.

Start With Platform Reality

Before debugging package versions, verify that your platform can use TensorFlow GPU at all.

Current official TensorFlow guidance is roughly:

  • Linux with an NVIDIA GPU is the main supported CUDA path
  • Windows should generally use WSL2 for modern GPU support
  • native Windows GPU support stopped after TensorFlow 2.10
  • macOS has no official CUDA-based TensorFlow GPU support

That means a perfectly written model will still run on CPU if the platform itself is outside the supported path. Many hours are wasted because developers debug TensorFlow code when the real issue is “this operating system and install combination cannot provide the GPU backend you expect.”

Verify the Environment First

Start with the simplest checks possible. First confirm that the operating system can see the NVIDIA driver:

bash
nvidia-smi

If that command fails, TensorFlow will not be the first thing to fix. The GPU driver or the host runtime is not ready yet.

Then verify that TensorFlow can see any GPU devices:

bash
python3 -c "import tensorflow as tf; print(tf.__version__); print(tf.config.list_physical_devices('GPU'))"

If the output is an empty list, the problem is still environmental. TensorFlow is installed, but GPU discovery failed.

On a recent Linux setup, the official pip path is typically:

bash
python3 -m pip install --upgrade pip
python3 -m pip install 'tensorflow[and-cuda]'

If you are on native Windows and trying TensorFlow 2.11 or newer with CUDA, stop there and reassess. Official TensorFlow guidance is to use WSL2 for newer GPU setups. On macOS, standard TensorFlow installs do not provide official GPU acceleration through the usual NVIDIA CUDA route.

Confirm TensorFlow Is Actually Placing Work on the GPU

Even when a GPU is visible, it is useful to confirm that operations are really being placed there.

python
1import tensorflow as tf
2
3tf.debugging.set_log_device_placement(True)
4
5gpus = tf.config.list_physical_devices("GPU")
6print("Visible GPUs:", gpus)
7
8if gpus:
9    with tf.device("/GPU:0"):
10        a = tf.random.normal((2000, 2000))
11        b = tf.random.normal((2000, 2000))
12        c = tf.matmul(a, b)
13    print("Result shape:", c.shape)
14else:
15    print("No GPU detected")

This script does two useful things. It prints the list of visible devices, and it asks TensorFlow to log device placement. If the matrix multiply still lands on CPU, you know discovery and execution are not aligned.

Common Reasons GPU Detection Fails

One common cause is version mismatch. The TensorFlow package, NVIDIA driver, and CUDA-related runtime must be compatible. Modern pip installation is simpler than the old manual CUDA workflow, but older tutorials still point people toward stale combinations.

Another cause is conflicting environments. A machine may have one system-level CUDA installation, one virtual environment, and an outdated notebook kernel. The shell where you installed TensorFlow might not be the same Python environment that runs your training code.

A third cause is memory behavior. TensorFlow may see the GPU but fail when it tries to allocate memory. In that case, limiting aggressive allocation can help:

python
1import tensorflow as tf
2
3gpus = tf.config.list_physical_devices("GPU")
4for gpu in gpus:
5    tf.config.experimental.set_memory_growth(gpu, True)
6
7print(tf.config.list_physical_devices("GPU"))

That setting does not fix installation issues, but it can make an otherwise valid setup more stable on shared workstations.

A Practical Troubleshooting Order

Use a strict order instead of changing many variables at once:

  1. verify the hardware with nvidia-smi
  2. verify the Python environment and TensorFlow version
  3. check tf.config.list_physical_devices('GPU')
  4. run a tiny placement test
  5. only then inspect model-specific code

If you are still blocked, isolate the environment completely. Create a new virtual environment, install only TensorFlow through the current official instructions, and retest before adding Jupyter, extra ML packages, or custom CUDA paths.

For Linux teams that want the lowest-friction setup, TensorFlow’s official Docker images can also simplify GPU usage because the container bundles the user-space dependencies and leaves only the NVIDIA driver on the host.

Common Pitfalls

The most common pitfall is following an old blog post that assumes a manual tensorflow-gpu package or a deprecated CUDA combination. Modern installation guidance has changed significantly.

Another frequent mistake is assuming that “GPU detected by the system” means “GPU available to TensorFlow.” Driver visibility and TensorFlow runtime compatibility are related, but they are not the same check.

A third issue is using native Windows with a recent TensorFlow release and expecting CUDA support. For modern versions, WSL2 is the practical supported route.

Finally, many people debug their model before verifying the platform. If tf.config.list_physical_devices('GPU') is empty, the model code is not the first thing to investigate.

Summary

  • TensorFlow GPU failures are usually environment issues, not model-code issues.
  • Verify platform support before changing package versions.
  • Use nvidia-smi and tf.config.list_physical_devices('GPU') as the first checks.
  • Native Windows support is limited for newer TensorFlow GPU setups, while macOS has no official CUDA-based path.
  • A clean virtual environment or official Docker image is often the fastest way to remove dependency confusion.

Related reading
Free course
Beginner
7 lessons
2 hours
Tackling System Design Interview Problems

A short course that equips you with the skills to approach system design interviews methodically.

Start the free course
Track what you have practised

A free account saves your progress, solutions and study plan across every problem on Codemia.

ML System Design practice on Codemia

Design recommenders, ranking systems and training pipelines the way ML interviews actually ask for them, with worked solutions.

Practice ML system design

All Rights Reserved.