Last modified: Apr 03, 2025 By Alexander Williams

Install Stable-Baselines3 in Python Step by Step

Stable-Baselines3 is a popular library for reinforcement learning in Python. It provides easy-to-use implementations of algorithms. This guide will help you install it.

Prerequisites

Before installing Stable-Baselines3, ensure you have Python 3.7 or higher. You also need pip, the Python package installer.

Check your Python version using the command below.

 
import sys
print(sys.version)


3.9.7 (default, Sep 16 2021, 13:09:58)

Step 1: Create a Virtual Environment

It's best to use a virtual environment to avoid conflicts. Run the following commands to create and activate one.


python -m venv sb3_env
source sb3_env/bin/activate  # On Windows, use `sb3_env\Scripts\activate`

Step 2: Install Stable-Baselines3

Use pip to install Stable-Baselines3. Run the command below in your terminal.


pip install stable-baselines3

This will install the latest version. If you encounter a ModuleNotFoundError, check our guide on how to solve ModuleNotFoundError.

Step 3: Verify the Installation

To ensure Stable-Baselines3 is installed, run the following Python code.

 
import stable_baselines3
print(stable_baselines3.__version__)


1.7.0

Step 4: Install Optional Dependencies

For extra features, install optional dependencies like TensorBoard. Use the command below.


pip install tensorboard

Step 5: Test with a Simple Example

Here’s a quick example to test Stable-Baselines3. It trains an agent using PPO.

 
from stable_baselines3 import PPO
from stable_baselines3.common.envs import DummyVecEnv
import gym

env = gym.make('CartPole-v1')
env = DummyVecEnv([lambda: env])

model = PPO('MlpPolicy', env, verbose=1)
model.learn(total_timesteps=10000)

This will train an agent on the CartPole environment. The output shows training progress.

Common Issues and Fixes

If you face errors, ensure all dependencies are installed. Check for compatibility issues.

For GPU support, install PyTorch with CUDA. Use the command below.


pip install torch torchvision torchaudio --extra-index-url https://download.pytorch.org/whl/cu113

Conclusion

Installing Stable-Baselines3 is simple with this guide. Follow each step carefully to avoid errors. Now you’re ready to explore reinforcement learning.