Skip to content

Instantly share code, notes, and snippets.

@BuildingAtom
Last active June 7, 2023 13:55
Show Gist options
  • Star 13 You must be signed in to star a gist
  • Fork 4 You must be signed in to fork a gist
  • Save BuildingAtom/3119ac9c595324c8001a7454f23bf8c8 to your computer and use it in GitHub Desktop.
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
@Dania0322
Copy link

Do you know why I am having this error?

Missing path to your environment variable.
Current values LD_LIBRARY_PATH=/usr/local/nvidia/lib:/usr/local/nvidia/lib64:/root/.mujoco/mujoco210/bin
Please add following line to .bashrc:
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/lib/nvidia

@franroldans
Copy link

franroldans commented May 6, 2022

I solved that by replacing this cell with the following:

!apt-get install -y \
   libgl1-mesa-dev \
   libgl1-mesa-glx \
   libglew-dev \
   libosmesa6-dev \
   software-properties-common

!apt-get install -y patchelf 

!pip install free-mujoco-py

@Dania0322
Copy link

Thank you, I really appreciate your help

@BuildingAtom
Copy link
Author

BuildingAtom commented May 7, 2022

@Dania0322 Yeah, there's probably a mistake I have in there with the messy way I tried to fill in some of the environment variables. I needed this for a project, so there's a bit of garbage in this gist that I don't exactly like. If ever needed, I may come back later to improve this, but at the moment, what @franroldans shared is a much cleaner solution since free-mujoco-py exists. I've noted this in the description too now.

Thanks @franroldans!

@Krishnendu1984
Copy link

after installing this free version when I am making env=gym.make('Humanoid-v2') and when i write env.render()
I am getting error
GLFW error (code %d): %s 65544 b'X11: The DISPLAY environment variable is missing'
GLFW error (code %d): %s 65544 b'X11: The DISPLAY environment variable is missing'


GlfwError Traceback (most recent call last)

in ()
----> 1 env.render()

4 frames

/usr/local/lib/python3.7/dist-packages/mujoco_py/mjviewer.py in init(self, sim)
26
27 def init(self, sim):
---> 28 super().init(sim)
29
30 self._gui_lock = Lock()

mjrendercontext.pyx in mujoco_py.cymj.MjRenderContextWindow.init()

mjrendercontext.pyx in mujoco_py.cymj.MjRenderContext.init()

mjrendercontext.pyx in mujoco_py.cymj.MjRenderContext._setup_opengl_context()

opengl_context.pyx in mujoco_py.cymj.GlfwContext.init()

opengl_context.pyx in mujoco_py.cymj.GlfwContext._init_glfw()

GlfwError: Failed to initialize GLFW
Kindly tell me why i am unable to view

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