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.
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:
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:
If you also need a specific standalone Keras version:
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:
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:
- check Colab’s Python version
- pick a TensorFlow or Keras version that supports it
- install with
%pip - restart the runtime
- 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:
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:
- check the current Python version in Colab
- choose a newer compatible TensorFlow release
- 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__andkeras.__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
- how to check both training/eval performances in tensorflow object_detection
- How to check if keras tensorflow backend is GPU or CPU version?
- How to check node name of tensorflow graph protocol buffers by c
- How to choose cross-entropy loss in TensorFlow?
- How to change size of plot in xgboost.plot_importance?
- How to change smoothing method of Naive Bayes classifier in NLTK?
- How to change my Git username in terminal?
- How to change the default target branch for merges in Gitlab
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack 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.