Skip to content

Keras - Installation

This chapter guides you through installing Keras, now the official high-level API for TensorFlow. Before proceeding, let’s review the prerequisites.

Ensure your system meets the following requirements:

  • Operating System: Windows, Linux, or macOS.
  • Python: Version 3.8 or higher is recommended. Keras is integrated with TensorFlow, which relies on Python.
  • Package Manager: pip (Python’s package installer) is required.

Keras, as part of TensorFlow, is a Python library. Verify your Python installation by opening a terminal or command prompt and typing python --version or python3 --version. You should see output similar to:

Python 3.9.7

If Python isn’t installed or you have an older version, download the latest stable release from the official Python website (python.org) and follow the installation instructions for your OS.

Section titled “Setting Up a Virtual Environment (Recommended)”

Using a virtual environment is a best practice for Python development. It isolates project dependencies, preventing conflicts between different projects. Python 3 includes the venv module for creating virtual environments.

Navigate to your project directory in the terminal and run the following command. Replace my_keras_env with your desired environment name:

# Linux/macOS
python3 -m venv my_keras_env
# Windows
py -m venv my_keras_env

This creates a directory named my_keras_env containing a copy of the Python interpreter and a pip instance.

Before installing packages, activate the environment:

# Linux/macOS
source my_keras_env/bin/activate
# Windows
.\my_keras_env\Scripts\activate

Your terminal prompt should now indicate that you are working within the virtual environment (e.g., (my_keras_env) ...$).

Keras is now part of TensorFlow 2.x. Installing TensorFlow automatically installs Keras. Ensure your virtual environment is active.

Use pip to install the latest stable version of TensorFlow:

pip install --upgrade pip # Upgrade pip first
pip install tensorflow

This command downloads and installs TensorFlow and its dependencies, including NumPy. You’ll see output indicating the download and installation process.

Note on GPU Support: If you have a compatible NVIDIA GPU and the necessary drivers/CUDA toolkit installed, you can install the GPU-enabled version for significantly faster training: pip install tensorflow[and-cuda]. Check the official TensorFlow documentation for specific GPU setup requirements.

While not strictly required by Keras core, these libraries are frequently used in deep learning workflows for data manipulation, analysis, and visualization:

  • NumPy: (Installed with TensorFlow) Fundamental package for numerical computation.
  • Pandas: Powerful library for data analysis and manipulation.
  • Matplotlib: Widely used library for plotting and visualization.
  • Scikit-learn: Comprehensive library for traditional machine learning algorithms, preprocessing, and evaluation metrics.

Install them using pip within your active virtual environment:

pip install pandas matplotlib scikit-learn

Alternatively, you can create a requirements.txt file listing all dependencies (tensorflow, pandas, etc.) and install them with pip install -r requirements.txt.

Open a Python interpreter within your activated virtual environment (type python or python3) and try importing TensorFlow:

import tensorflow as tf
print(tf.__version__)
# Access Keras through TensorFlow
print(tf.keras.__version__)

If these commands execute without error and print version numbers, your installation is successful.

When you are finished working on your project, you can deactivate the environment:

deactivate

Anaconda is a popular distribution for Python and R, focused on data science. It includes its own package manager (conda) and environment management system.

Open the Anaconda Prompt or terminal and create a new environment (e.g., tf_env) with a specific Python version:

conda create --name tf_env python=3.9 # Or your desired Python version
conda activate tf_env

Install packages using conda (preferred within Anaconda environments):

conda install tensorflow pandas matplotlib scikit-learn

Conda will handle resolving dependencies. You can verify the installation similarly by importing TensorFlow in a Python interpreter within the active tf_env.