Guides 11792 Published by

This guide walks you through installing a clean Keras/TensorFlow stack on Ubuntu, starting with setting up an isolated virtual environment so your system Python stays untouched. It then covers essential system packages, upgrades pip and setuptools, chooses the right TensorFlow release for CPU or GPU, and optionally pulls in standalone Keras 3.x if extra features are desired. After the installation it offers a quick sanity‑check script that imports TensorFlow, prints version numbers, builds a simple model, and confirms everything is wired correctly. Finally, the author shares common pitfalls—such as mismatched Python versions or forgotten virtual environment activation—and gives troubleshooting tips for CUDA setup and driver compatibility.



Install Keras With TensorFlow Backend on Ubuntu

You’re probably tired of wrestling with missing libraries or incompatible Python versions when you try to get deep‑learning frameworks up and running. This guide will walk you through installing a clean, working Keras installation that talks to TensorFlow on an Ubuntu system—no extra fluff, just the steps that actually work.

1. Why a Virtual Environment Is Your Friend

If you’ve ever installed something with pip only to discover it broke your system Python or got tangled in dependency hell, you know why isolation matters. A virtualenv keeps Keras and TensorFlow out of reach from other packages on your machine, so you can upgrade or uninstall them without touching the rest of your setup.

sudo apt-get update
sudo apt-get install python3-venv
python3 -m venv keras-env
source keras-env/bin/activate

The source step activates the environment, so subsequent pip installs go straight to this sandboxed Python. If you forget and run pip globally, you’ll see all sorts of “conflict” errors later.

2. Make Sure Your System Is Ready

Keras/TensorFlow pull in a lot of compiled code that likes to eat your CPU cache. A quick system prep can save headaches later:

sudo apt-get install build-essential libssl-dev libffi-dev python3-dev

These packages provide the C compiler and headers TensorFlow needs to build its extensions. On a fresh Ubuntu install you’ll otherwise hit “Could not find module ‘libpython3.x’” errors.

3. Install pip First, Then Upgrade It

If you’re on an older Ubuntu (like 16.04), the bundled pip can be ancient. Grab the newest version before installing anything else:

python -m ensurepip --upgrade
pip install --upgrade pip setuptools wheel

Newer wheels for TensorFlow and Keras are built against recent pip versions. Using an old pip will force you to compile from source, which takes forever.

4. Get the Right Version of TensorFlow

Decide whether you want CPU‑only or GPU support:

# For CPU only
pip install tensorflow==2.15

# For GPU (if you have a CUDA‑capable NVIDIA card)
# pip install tensorflow-gpu==2.15

The tensorflow package already bundles the GPU runtime if your hardware and drivers are compatible; installing tensorflow-gpu is no longer necessary on TensorFlow 2.x, but some people still use the old naming convention.

5. Finally Pull in Keras

Keras used to be a separate project, but since TF 2.x it lives inside the TensorFlow package itself (tf.keras). Still, many folks like to install the standalone library for extra utilities:

pip install keras==3.0.0

The 3.x Keras release is fully compatible with TensorFlow 2.x but offers a cleaner API surface and some experimental layers that aren’t in tf.keras. If you only need the basics, you can skip this step.

6. Verify Your Setup

Run a quick sanity check:

import tensorflow as tf
print("TensorFlow version:", tf.__version__)
print("Keras version:", tf.keras.__version__)

# Simple model to make sure everything is wired up
model = tf.keras.Sequential([
    tf.keras.layers.Dense(10, activation='relu', input_shape=(5,)),
    tf.keras.layers.Dense(1)
])
model.compile(optimizer='adam', loss='mse')
print("Model built successfully")

If you see the TensorFlow and Keras version numbers without any import errors, congratulations—you’re good to go.

7. Common Pitfalls (and My Own Oops)

I once installed tensorflow==1.x on a system with Python 3.9 because I didn’t realize that older TF releases drop support for newer Python versions. The result? A cryptic error about “cannot import name ‘_tensor_util’” while trying to run any model.

Another time, I forgot to activate my virtual environment and ran pip install tensorflow. It installed into the system site‑packages, clobbered a pre‑existing TensorFlow 1.x that was being used by another project on the same machine. The fix? Delete the rogue package with sudo pip uninstall tensorflow (yes, you need sudo there), then reinstall inside your own environment.

8. Optional: Enable CUDA for GPU Training

If you’re ready to harness your NVIDIA card:

1. Install drivers and CUDA Toolkit per NVIDIA’s instructions.
2. Verify nvidia-smi shows a running driver.
3. Re‑install TensorFlow with the GPU flag (see step 4) after ensuring CUDA 11.x is on your path.

Without matching CUDA/cuDNN versions, you’ll hit “Could not load cuDNN library” errors that look like mystic curses.

9. Wrap‑up

You’ve just turned a potentially painful installation into a few straightforward commands. Drop this guide next time someone asks how to get Keras working on Ubuntu without the usual headache of mismatched dependencies or broken Python versions.

Happy coding, and may your models converge before you hit Ctrl‑C!