Last modified: Apr 07, 2025 By Alexander Williams
How to Install Gymnasium in Python Step by Step
Gymnasium is a popular library for developing reinforcement learning algorithms. It provides various environments to test and train AI models. This guide will help you install Gymnasium in Python easily.
Prerequisites
Before installing Gymnasium, ensure you have Python installed. You can check this by running python --version
in your terminal.
python --version
Python 3.8.10
If Python is not installed, download it from the official website. Also, ensure you have pip installed, as it is required for package installation.
Install Gymnasium Using pip
The easiest way to install Gymnasium is via pip. Open your terminal or command prompt and run the following command.
pip install gymnasium
This will download and install the latest version of Gymnasium along with its dependencies.
Verify the Installation
After installation, verify that Gymnasium is installed correctly. Open a Python shell and import the library.
import gymnasium
print(gymnasium.__version__)
0.28.1
If you see the version number, the installation was successful. If you encounter a ModuleNotFoundError
, check our guide on how to solve ModuleNotFoundError.
Install Additional Dependencies
Some Gymnasium environments require additional dependencies. For example, the Atari environments need the gymnasium[atari]
package.
pip install gymnasium[atari]
This will install all necessary packages for Atari games. You can also install other extras like gymnasium[box2d]
for physics-based environments.
Test a Gymnasium Environment
To ensure everything works, let's test a simple environment. Run the following code in your Python shell.
import gymnasium as gym
env = gym.make("CartPole-v1")
observation = env.reset()
print(observation)
(array([-0.012, 0.041, -0.035, -0.02], dtype=float32), {})
This code initializes the CartPole environment and resets it. The output shows the initial observation.
Troubleshooting Common Issues
If you face issues during installation, ensure your pip is up-to-date. Run pip install --upgrade pip
before installing Gymnasium.
If you encounter dependency conflicts, consider using a virtual environment. This isolates your project and avoids conflicts.
Conclusion
Installing Gymnasium in Python is straightforward with pip. Follow the steps above to set up your reinforcement learning environment. For more details, check the official Gymnasium documentation.
Now you're ready to start building and training AI models with Gymnasium. Happy coding!