Install Keras Python Library on Ubuntu
In this tutorial, we shall learn to install Keras Python Neural Network Library on Ubuntu. Keras is a high-level deep learning API for building and training neural networks. Current Keras versions run with a backend such as TensorFlow, JAX, or PyTorch. Older Keras tutorials may also refer to Theano, CNTK, MXNet, or other legacy backend setups, so the installation method you choose should match the code you want to run.
For most Ubuntu users, the practical setup is to install Keras in a separate Python environment and install TensorFlow as the backend. This keeps the deep learning packages separate from the system Python installation.
Prerequisites for Installing Keras on Ubuntu
We shall use Anaconda distribution of Python for developing Deep Learning Applications with Keras.
So, we shall Install Anaconda Python.
After installing Anaconda, open a terminal and confirm that Python and pip are available.
python --version
python -m pip --version
If these commands point to your Anaconda installation or to the environment you plan to use, continue with the Keras installation. If they point to the system Python, activate your conda environment first.
Create a Conda Environment for Keras Deep Learning Projects
It is better to install Keras inside a dedicated environment instead of installing it globally. This avoids dependency conflicts with other Python projects.
conda create -n keras-env python=3.11 pip
conda activate keras-env
The environment name keras-env is only an example. You may use any name that helps you identify your Keras setup later.
Install Keras
To install Keras python library, open a Terminal and run the following pip command.
$ pip install keras
Console/Terminal Output
~$ pip install keras
Collecting keras
Downloading Keras-2.1.2-py2.py3-none-any.whl (304kB)
100% |????????????????????????????????| 307kB 855kB/s
Requirement already satisfied: scipy>=0.14 in /usr/lib/anaconda3/lib/python3.6/site-packages (from keras)
Requirement already satisfied: numpy>=1.9.1 in /usr/lib/anaconda3/lib/python3.6/site-packages (from keras)
Requirement already satisfied: six>=1.9.0 in /usr/lib/anaconda3/lib/python3.6/site-packages (from keras)
Requirement already satisfied: pyyaml in /usr/lib/anaconda3/lib/python3.6/site-packages (from keras)
Installing collected packages: keras
Successfully installed keras-2.1.2
The terminal output above shows an older Keras 2 installation. On a current Ubuntu setup, you can upgrade pip and install the latest Keras package with the following command.
python -m pip install --upgrade pip
python -m pip install --upgrade keras
Install TensorFlow Backend for Keras on Ubuntu
Installing the keras package gives you the Keras API. To train and run models, Keras also needs a backend. TensorFlow is a common backend choice for Ubuntu beginners because many Keras examples use it.
python -m pip install --upgrade tensorflow
You may also install Keras and TensorFlow together in one command.
python -m pip install --upgrade keras tensorflow
If your project specifically uses JAX or PyTorch as the Keras backend, install that backend instead of TensorFlow. Keep only the backend your project needs unless you have a reason to test multiple backends.
Verify Keras Installation in Ubuntu Terminal
After installation, import Keras and print the installed version.
python -c "import keras; print(keras.__version__)"
To check the active Keras backend, run the following command.
python -c "import keras; print(keras.backend.backend())"
If Keras imports without an error and prints a backend name such as tensorflow, the installation is ready for basic examples.
Run a Small Keras Neural Network Check
The following small example builds a simple Keras model and runs one prediction. It is useful for checking whether Keras and the backend are working together.
import numpy as np
import keras
from keras import layers
model = keras.Sequential([
layers.Input(shape=(2,)),
layers.Dense(1)
])
x = np.array([[1.0, 2.0]])
y = model.predict(x)
print(y.shape)
The output shape should be:
(1, 1)
Select TensorFlow, JAX, or PyTorch Backend for Keras
Keras can use different backends. The backend must be selected before importing Keras in your Python program. For a terminal session, you can set the backend using an environment variable.
export KERAS_BACKEND=tensorflow
python your_script.py
Use tensorflow, jax, or torch according to the backend installed in your environment. For most beginner examples on Ubuntu, TensorFlow is a straightforward choice.
Install Keras for Older Tutorials That Use Keras 2
The original command output in this tutorial shows Keras 2.1.2, which was common in older deep learning tutorials. If you are reproducing old code, use a separate environment and install the exact versions expected by that project. Do not downgrade packages in your main Python environment.
conda create -n keras2-env python=3.6 pip
conda activate keras2-env
python -m pip install "keras==2.1.2"
This legacy setup is only for older examples. New Keras projects should use the current Keras installation method and a supported backend.
Common Keras Installation Errors on Ubuntu
If Keras does not work after installation, check the Python environment and backend first. Most installation issues come from installing Keras in one environment and running Python from another environment.
- No module named keras: Activate the correct conda environment and run
python -m pip install keras. - No backend found: Install a backend such as TensorFlow with
python -m pip install tensorflow. - Wrong Python version: Create a fresh conda environment with a Python version supported by the packages you need.
- Old tutorial fails with new Keras: Use a legacy environment for the old tutorial or update the code for current Keras imports and APIs.
- pip installs into the wrong location: Prefer
python -m pipover plainpipso that pip belongs to the active Python interpreter.
Do You Need pip install Keras or TensorFlow Keras?
If your code uses import keras, install the standalone keras package and a backend. If your code uses from tensorflow import keras or tensorflow.keras, install TensorFlow. When following an existing project, check the import statements before deciding which package to install.
For many current examples, this setup is enough:
python -m pip install --upgrade keras tensorflow
FAQs on Installing Keras Python Library on Ubuntu
How to install Keras in Ubuntu?
Create and activate a Python environment, then run python -m pip install --upgrade keras. Install a backend such as TensorFlow with python -m pip install --upgrade tensorflow.
How to install Keras library in Python?
Use pip from the Python interpreter you plan to run: python -m pip install keras. This helps avoid installing Keras into a different Python environment by mistake.
Do I need to pip install Keras separately?
Install keras separately when your code imports keras directly. If your code uses tensorflow.keras, install TensorFlow. Current standalone Keras also needs a backend such as TensorFlow, JAX, or PyTorch.
Is Keras a deep learning library?
Keras is a deep learning API used to build neural network models. It provides layers, models, losses, optimizers, and training utilities while using a backend framework to perform the actual computation.
Why does Keras import fail after installation?
The most common reason is an environment mismatch. Check python --version, python -m pip --version, and confirm that Keras and its backend are installed in the same environment you are running.
QA Checklist for This Keras Ubuntu Installation Tutorial
- Confirm that the Anaconda prerequisite link is retained and unchanged.
- Confirm that the original
$ pip install kerascommand block and original terminal output block remain unchanged. - Check that new command-line examples use the
language-bashclass. - Check that Python examples use the
language-pythonclass and output-only examples use theoutputclass. - Verify that the article explains the need for a Keras backend such as TensorFlow, JAX, or PyTorch.
- Verify that the tutorial distinguishes current Keras installation from older Keras 2 tutorial requirements.
Conclusion
Keras Python Library is successfully installed when it imports without errors and can run a small model using the selected backend. On Ubuntu, the safest approach is to use Anaconda, create a separate environment, install Keras with pip, install a backend such as TensorFlow, and verify the setup before running larger deep learning examples.
TutorialKart.com