見出し画像

Installing Open AI Gym with Anaconda

Using Anaconda / Miniconda

# update conda at first
$ conda update conda

#if there is a new package just answer Y to install new package

After that, Install OpenAI Gym on Conda, I prefer install gym in the conda environment, so I use below command to create and also install gym in it.

# parameter description
# -n name_of_your_environment -> choose your name for the environment
# -c conda-forge -> include channel conda-forge to search the dependencies packages.
# python=3.9 -> in this case I use python 3.9 
# gym-all -> install all gym packages, so will not get undefined package

$ conda create -n gym_env python=3.10

If no error occurred, meaning your installation was succeeded. Let's try it, but before that we should activate the environment

$ conda activate gym_env
(gym_env) $ conda install pip

Then after that install gym on the active gym_env environment, in order to use pip in conda environment, you have to install pip like above. 

After you install pip on conda, please find the real path by using this command (it will show you where the pip in conda environment  reside at /Users/myusername/opt/miniconda3/envs/gym_env/ )

$ which -a pip

# Output
# /Users/myusername/.pyenv/shims/pip
# /Users/myusername/opt/miniconda3/envs/gym_env/bin/pip

Then install gym with the pip inside of the conda environment by typing below command.

$ /Users/myusername/opt/miniconda3/envs/gym_env/bin/pip install gym

if you don't want to use the long path live above, you can make a shell function, put it on th .zshrc or .bash_profile file, so when you want to use pip on conda environment, you can call it using cpip.

cpip() {
    ERROR_MSG="Not in a conda environment."
    ERROR_MSG="$ERROR_MSG\nUse \`source activate ENV\`"
    ERROR_MSG="$ERROR_MSG to enter a conda environment."

    [ -z "$CONDA_DEFAULT_ENV" ] && echo "$ERROR_MSG" && return 1

    ERROR_MSG='Pip not installed in current conda environment.'
    ERROR_MSG="$ERROR_MSG\nUse \`conda install pip\`"
    ERROR_MSG="$ERROR_MSG to install pip in current conda environment."

    [ -e "$CONDA_PREFIX/bin/pip" ] || (echo "$ERROR_MSG" && return 2)

    PIP="$CONDA_PREFIX/bin/pip"
    "$PIP" "$@"
}


After install gym,  you need to install sample environments such as atari or classic_control, box2d, etc. So to install the package for thats environment type: 👇🏻

# for atari environment
$ /Users/myusername/opt/miniconda3/envs/gym_env/bin/pip install gym[atari]

# for classic control environment
$ /Users/myusername/opt/miniconda3/envs/gym_env/bin/pip install gym[classic_control]

The result will be like this 👇🏻

Install classic control environment

Issue when you install on your mac!🙆‍♀️
Mac by default doesn't recognize escape character '[' and ']' (square bracket), so it will show zsh: no matches found: gym[all]

Don't panic if you see this error

There are 2 solutions:

Option 1: Add this line into your .zshrc or .bash_profile file

alias rake='noglob rake'


Option 2:
 using escape character in terminal like this

$ pip install gym\[all\]


TIME TO TEST

To see whether your installation is succeeded or not, let's test it. By running these commands.

Python Console Screen

After you enter the last command env.render(), than the new window will pop-up, like this.

OpenAI Gym CartPole Environment

Others Library To Install

You need to install another libraries for gym development. Here are the instructions.

Instruction For Ubuntu

$ sudo apt-get update
$ sudo apt-get install -y build-essential cmake python-numpy python-opengl \
libboost-all-dev zlibig-dev libsdl2-dev libav-tools xorg-dev libjpeg-dev swig

Instruction For MacOs

$ brew install cmake boost sdl2 swig wget
$ brew install boot-python --with-python3



In case you want to remove the environment and the packages inside it. Run this command

$ conda env remove -n gym_env

Will be continued… 

Find the video of these OpenAI Gym series in my channel.


この記事が気に入ったらサポートをしてみませんか?