Last modified: Jun 14, 2025 By Alexander Williams

How to Install Keras in Python for Neural Networks

Keras is a high-level neural networks API. It runs on top of TensorFlow, Theano, or CNTK. This guide will help you install Keras in Python.

Prerequisites for Installing Keras

Before installing Keras, ensure you have Python 3.6 or later. You also need a backend like TensorFlow. Check our guide on installing Theano for an alternative backend.

It's recommended to use a virtual environment. This keeps your project dependencies isolated.

Step 1: Install Python

If you don't have Python, download it from python.org. Verify the installation with:


python --version

You should see the Python version number as output.

Step 2: Create a Virtual Environment

Create and activate a virtual environment:


python -m venv keras_env
source keras_env/bin/activate  # Linux/Mac
keras_env\Scripts\activate  # Windows

Step 3: Install TensorFlow Backend

Keras needs a backend. TensorFlow is the most popular choice. Install it with pip:


pip install tensorflow

For GPU support, use tensorflow-gpu instead.

Step 4: Install Keras

Now install Keras using pip:


pip install keras

This will install the latest stable version of Keras.

Step 5: Verify the Installation

Check if Keras installed correctly. Run Python and try importing it:

 
import keras
print(keras.__version__)

You should see the Keras version number without errors.

Basic Keras Example

Here's a simple neural network example to test your installation:


from keras.models import Sequential
from keras.layers import Dense

# Create model
model = Sequential()
model.add(Dense(units=64, activation='relu', input_dim=100))
model.add(Dense(units=10, activation='softmax'))

# Compile model
model.compile(loss='categorical_crossentropy',
              optimizer='sgd',
              metrics=['accuracy'])

print("Model created successfully!")

This code creates a basic neural network model.

Troubleshooting Common Issues

If you get errors, try these solutions:

1. Dependency conflicts: Use a virtual environment to avoid this.

2. Backend issues: Ensure TensorFlow is properly installed.

3. Version mismatch: Check compatibility between Keras and TensorFlow versions.

Using Keras with Other Backends

While TensorFlow is default, you can use Theano or CNTK. For Theano setup, see our Theano installation guide.

Integrating Keras with Other Libraries

Keras works well with data visualization tools. For plotting results, consider Plotly for interactive visualizations.

Conclusion

Installing Keras is straightforward with pip. Always use a virtual environment. TensorFlow is the recommended backend. Verify your installation with a simple test script.

Now you're ready to build neural networks with Keras!