Google Colab
Keras
TensorFlow
Version Control
Machine Learning

How to change Keras/tensorflow version in Google colab?

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

Changing TensorFlow or Keras in Google Colab usually means installing a different pip package version inside the notebook runtime and then restarting that runtime. The main constraint is that Colab has a fixed Python and system image, so not every historical TensorFlow release will still install cleanly.

Check the Current Runtime First

Before installing anything, check what Colab is already using:

python
1import sys
2import tensorflow as tf
3
4print(sys.version)
5print(tf.__version__)

This tells you both the Python version and the TensorFlow version that came with the current runtime.

Install the Version You Need

In Colab, use %pip so the install targets the notebook environment:

python
%pip install tensorflow==2.16.1

If you also need a specific standalone Keras version:

python
%pip install keras==3.4.1

After installation, restart the runtime. Package changes are not fully reliable until the Python process is restarted.

Restart and Verify

After the restart, import the libraries again and verify the versions:

python
1import tensorflow as tf
2import keras
3
4print(tf.__version__)
5print(keras.__version__)

If the version did not change, the most common cause is that the runtime was not actually restarted.

Understand Compatibility Limits

This is where many Colab installs fail. Older TensorFlow wheels support only certain Python versions, and Colab’s base image moves forward over time. So the practical workflow is:

  1. check Colab’s Python version
  2. pick a TensorFlow or Keras version that supports it
  3. install with %pip
  4. restart the runtime
  5. verify the installed versions

If you need a very old TensorFlow stack, a local virtual environment is often easier than trying to force an incompatible Colab runtime.

Keep the Notebook Reproducible

If a notebook depends on a specific version, put the install cell near the top:

python
%pip install tensorflow==2.16.1 keras==3.4.1

That makes it obvious to future readers that the notebook expects a custom environment. This matters because Colab runtimes are ephemeral and revert when a new session starts.

What to Do When Installation Fails

If pip reports dependency conflicts or says no matching distribution was found, the most likely cause is Python-version incompatibility with the version you requested. In that case:

  1. check the current Python version in Colab
  2. choose a newer compatible TensorFlow release
  3. avoid mixing unrelated package pins unless you truly need them

For heavily pinned legacy environments, Colab is often the wrong target and a local virtual environment is easier to control.

That is especially true when the notebook must reproduce an older training run exactly rather than just "approximately" match a recent runtime.

Common Pitfalls

The biggest mistake is installing a package version and then continuing to run cells without restarting the runtime. That mixes old imports with new package files.

Another issue is assuming every TensorFlow release will work with the current Colab Python image. Version compatibility still matters even in notebooks.

People also install unrelated TensorFlow and Keras versions without checking whether those versions are meant to work together.

Finally, remember that Colab resets. Any manual version change disappears in the next fresh session unless the notebook installs it again.

Summary

  • Use %pip install ... in Colab to request a specific TensorFlow or Keras version.
  • Restart the runtime after installation.
  • Verify the new versions with tf.__version__ and keras.__version__.
  • Check Python-version compatibility before trying much older TensorFlow releases.
  • Keep the install cell in the notebook so the environment stays reproducible.

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.