Skip to content

Instantly share code, notes, and snippets.

@BuildingAtom
Last active June 30, 2025 18:28
Show Gist options
  • Select an option

  • Save BuildingAtom/3119ac9c595324c8001a7454f23bf8c8 to your computer and use it in GitHub Desktop.

Select an option

Save BuildingAtom/3119ac9c595324c8001a7454f23bf8c8 to your computer and use it in GitHub Desktop.
NOTE: Please see @franroldans comment below for a much cleaner solution using free-mujoco-py! [Orig] MuJoCo 2.1 working in Google Colab for OpenAI gym. Just add this as a code block near the top of your notebook to get MuJoCo setup.
#Include this at the top of your colab code
import os
if not os.path.exists('.mujoco_setup_complete'):
# Get the prereqs
!apt-get -qq update
!apt-get -qq install -y libosmesa6-dev libgl1-mesa-glx libglfw3 libgl1-mesa-dev libglew-dev patchelf
# Get Mujoco
!mkdir ~/.mujoco
!wget -q https://mujoco.org/download/mujoco210-linux-x86_64.tar.gz -O mujoco.tar.gz
!tar -zxf mujoco.tar.gz -C "$HOME/.mujoco"
!rm mujoco.tar.gz
# Add it to the actively loaded path and the bashrc path (these only do so much)
!echo 'export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:$HOME/.mujoco/mujoco210/bin' >> ~/.bashrc
!echo 'export LD_PRELOAD=$LD_PRELOAD:/usr/lib/x86_64-linux-gnu/libGLEW.so' >> ~/.bashrc
# THE ANNOYING ONE, FORCE IT INTO LDCONFIG SO WE ACTUALLY GET ACCESS TO IT THIS SESSION
!echo "/root/.mujoco/mujoco210/bin" > /etc/ld.so.conf.d/mujoco_ld_lib_path.conf
!ldconfig
# Install Mujoco-py
!pip3 install -U 'mujoco-py<2.2,>=2.1'
# run once
!touch .mujoco_setup_complete
try:
if _mujoco_run_once:
pass
except NameError:
_mujoco_run_once = False
if not _mujoco_run_once:
# Add it to the actively loaded path and the bashrc path (these only do so much)
try:
os.environ['LD_LIBRARY_PATH']=os.environ['LD_LIBRARY_PATH'] + ':/root/.mujoco/mujoco210/bin'
except KeyError:
os.environ['LD_LIBRARY_PATH']='/root/.mujoco/mujoco210/bin'
try:
os.environ['LD_PRELOAD']=os.environ['LD_PRELOAD'] + ':/usr/lib/x86_64-linux-gnu/libGLEW.so'
except KeyError:
os.environ['LD_PRELOAD']='/usr/lib/x86_64-linux-gnu/libGLEW.so'
# presetup so we don't see output on first env initialization
import mujoco_py
_mujoco_run_once = True
@BuildingAtom
Copy link
Author

hey, this was just made as a quick solution 3-4 years ago at a time when mujoco wasn't as easy to get into python for colab and some of the existing solutions weren't working. I'm not too familiar with the issues anymore, but I think free-mujoco-py takes care of this now, so I'd check that out first. I can look into fixing this if needed, but it'll be a good while before I can get around to it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment