How to Install and Set Up Keras in Python

I was setting up a new deep learning project in Python and needed to install Keras on my system. I’ve been using Keras for over four years now, and even though installation is easy, I’ve seen many beginners struggle with environment setup and dependencies.

In this guide, I’ll walk you through how to install and set up Keras in Python on Windows, macOS, and Linux. I’ll also show you how to verify your installation by running a simple deep learning example.

By the end of this tutorial, you’ll have a fully functional Keras environment ready for your next AI or machine learning project.

What is Keras in Python?

Keras is a high-level neural network API written in Python. It provides an easy-to-use interface for building and training deep learning models. What makes Keras powerful is that it runs on top of popular backends like TensorFlow, JAX, and PyTorch.

In simple terms, Keras allows you to focus on building models instead of worrying about low-level mathematical operations. That’s why I recommend it to anyone getting started with deep learning in Python.

Method 1: Install Keras in Python Using pip (Recommended)

The easiest and most common way to install Keras is by using pip, Python’s package manager. This method works on Windows, macOS, and Linux.

Before installing Keras, make sure you have Python 3.8 or higher installed. You can check your Python version by running this command:

python --version

If you don’t have Python installed, download it from the official Python website. Once installed, open your terminal or command prompt and follow these steps.

Step 1: Create a Virtual Environment

Creating a virtual environment helps keep your Python dependencies isolated. I always recommend this for any machine learning project.

python -m venv keras_env

Activate the environment:

  • Windows:
keras_env\Scripts\activate
  • macOS/Linux:
source keras_env/bin/activate

Once activated, your terminal will show (keras_env) before the prompt, indicating that your environment is active.

Step 2: Install TensorFlow (Keras Backend)

Keras now comes bundled with TensorFlow, so installing TensorFlow automatically installs Keras too. Run this command:

pip install tensorflow

I executed the above example code and added the screenshot below.

Install and Set Up Keras in Python

This will install the latest stable version of TensorFlow, which includes Keras.

If you want the GPU version for faster performance (especially useful for large models), use:

pip install tensorflow[and-cuda]

This installs TensorFlow with GPU support (make sure your system has an NVIDIA GPU and CUDA drivers installed).

Step 3: Verify the Installation

Now that Keras is installed, let’s verify it. Open a Python shell and run:

import tensorflow as tf
print(tf.__version__)
print(tf.keras.__version__)

If you see version numbers without any errors, congratulations, Keras is successfully installed on your system!

Method 2: Install Keras in Python Using Anaconda

If you prefer using Anaconda, this method is perfect for you. Anaconda simplifies package management and is very popular among data scientists in the USA.

Step 1: Create a New Conda Environment

First, open the Anaconda Prompt (on Windows) or Terminal (on macOS/Linux) and create a new environment:

conda create --name keras_env python=3.10

Activate it using:

conda activate keras_env

Step 2: Install TensorFlow (with Keras)

Once the environment is active, install TensorFlow:

conda install -c conda-forge tensorflow

This command installs TensorFlow and Keras from the Conda-Forge channel, which ensures you get the latest stable version.

Step 3: Test Keras Installation

Run the following Python script to check if Keras is working properly:

import tensorflow as tf
from tensorflow import keras

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

If the script prints version numbers, your installation is successful.

Method 3: Install Keras with GPU Support (Advanced Users)

If you’re working on computationally intensive deep learning tasks, installing Keras with GPU support can drastically improve performance.

Step 1: Install NVIDIA CUDA and cuDNN

  1. Download and install CUDA Toolkit from NVIDIA’s official website.
  2. Install cuDNN from NVIDIA cuDNN page.
  3. Add both CUDA and cuDNN paths to your system’s environment variables.

Step 2: Install TensorFlow GPU

Once CUDA and cuDNN are configured, install TensorFlow GPU:

pip install tensorflow[and-cuda]

This single command installs TensorFlow with GPU support and Keras.

Step 3: Verify GPU Access

Run the following Python code to check if TensorFlow detects your GPU:

import tensorflow as tf

print("Num GPUs Available:", len(tf.config.list_physical_devices('GPU')))

If you see a number greater than 0, you’re good to go, Keras is now running on GPU!

Example: Build a Simple Neural Network with Keras in Python

Now that Keras is installed, let’s build a simple neural network to verify everything works perfectly.

This example uses the MNIST dataset, a popular dataset of handwritten digits widely used in the USA for testing machine learning models.

import tensorflow as tf
from tensorflow import keras
from tensorflow.keras import layers

# Load the MNIST dataset
(x_train, y_train), (x_test, y_test) = keras.datasets.mnist.load_data()

# Normalize the data
x_train = x_train / 255.0
x_test = x_test / 255.0

# Build a simple neural network model
model = keras.Sequential([
    layers.Flatten(input_shape=(28, 28)),
    layers.Dense(128, activation='relu'),
    layers.Dropout(0.2),
    layers.Dense(10)
])

# Compile the model
model.compile(optimizer='adam',
              loss=tf.keras.losses.SparseCategoricalCrossentropy(from_logits=True),
              metrics=['accuracy'])

# Train the model
model.fit(x_train, y_train, epochs=5)

# Evaluate the model
test_loss, test_acc = model.evaluate(x_test, y_test, verbose=2)
print("\nTest accuracy:", test_acc)

This Python code trains a simple neural network to recognize handwritten digits. It’s a great way to confirm that your Keras installation is working correctly.

Common Installation Issues and Fixes

Even after 10 years of working with Keras, I still occasionally run into setup issues. Here are some common ones and how to fix them:

  • Issue: “No module named ‘tensorflow’”
    Fix: Ensure you installed TensorFlow in the correct environment. Activate your environment and reinstall:
  pip install tensorflow
  • Issue: “CUDA not found” or “GPU not detected”
    Fix: Check your CUDA installation paths and ensure you installed a compatible version of TensorFlow.
  • Issue: “Permission denied” on macOS/Linux
    Fix: Use sudo or install in a virtual environment to avoid permission issues.

My Personal Tips for Python Keras Setup

After setting up Keras on countless systems, here are a few personal tips that save time:

  • Always use virtual environments to avoid dependency conflicts.
  • Keep your pip and conda updated regularly.
  • Use Jupyter Notebook for quick experimentation — it integrates beautifully with Keras.
  • For large datasets, consider using Google Colab — it’s free and comes with Keras pre-installed.

Setting up Keras in Python is easier than it looks. Whether you’re on Windows, macOS, or Linux, following the steps above ensures a clean and stable installation.

Once installed, you can start building and training deep learning models, from image classification to natural language processing, all with just a few lines of Python code.

You may like to read:

Leave a Comment

51 Python Programs

51 PYTHON PROGRAMS PDF FREE

Download a FREE PDF (112 Pages) Containing 51 Useful Python Programs.

pyython developer roadmap

Aspiring to be a Python developer?

Download a FREE PDF on how to become a Python developer.

Let’s be friends

Be the first to know about sales and special discounts.