Creating your first OpenAI Gym environment
We will be going over the steps to set up the OpenAI Gym dependencies and other tools required for training your reinforcement learning agents in detail in Chapter 3, Getting Started with OpenAI Gym and Deep Reinforcement Learning. This section provides a quick way to get started with the OpenAI Gym Python API on Linux and macOS using virtualenv
so that you can get a sneak peak into the Gym!
MacOS and Ubuntu Linux systems come with Python installed by default. You can check which version of Python is installed by running python --version
from a terminal window. If this returns python
followed by a version number, then you are good to proceed to the next steps! If you get an error saying the Python command was not found, then you have to install Python. Please refer to the detailed installation section in Chapter 3, Getting Started with OpenAI Gym and Deep Reinforcement Learning of this book:
- Install
virtualenv
:
$pip install virtualenv
Note
If pip is not installed on your system, you can install it by typing sudo easy_install pip
.
- Create a virtual environment named
openai-gym
using the virtualenv tool:
$virtualenv openai-gym
- Activate the
openai-gym
virtual environment:
$source openai-gym/bin/activate
- Install all the packages for the Gym toolkit from upstream:
$pip install -U gym
Note
If you get permission denied
or failed with error code 1
when you run the pip install
command, it is most likely because the permissions on the directory you are trying to install the package to (the openai-gym
directory inside virtualenv
in this case) needs special/root privileges. You can either run sudo -H pip install -U gym[all]
to solve the issue or change permissions on the openai-gym
directory by running sudo chmod -R o+rw ~/openai-gym
.
- Test to make sure the installation is successful:
$python -c 'import gym; gym.make("CartPole-v0");'
Creating and visualizing a new Gym environment
In just a minute or two, you have created an instance of an OpenAI Gym environment to get started!
Let's open a new Python prompt and import the gym
module:
>>import gym
Once the gym
module is imported, we can use the gym.make
method to create our new environment like this:
>>env = gym.make('CartPole-v0') >>env.reset() env.render()
This will bring up a window like this:

Hooray!