Robotics

MuJoCo

What is MuJoCo?

MuJoCo is a fast and accurate physics simulation engine aimed at research and development in robotics, biomechanics, graphics, and animation. It’s an engine, meaning, it doesn’t provide ready-to-use models or environments to work with, rather it runs environments (like those that OpenAI’s Gym offers).


Installation

macOS

  1. Download it from DeepMind Github
  2. Drag MuJoCo app to Applications folder.
  3. Open MuJoCo.
  4. Drag a xml file from the model folder into the MuJoCo window.

1. Ubuntu System Prerequisites

$ sudo apt update && sudo apt upgrade -y
$ sudo apt install -y python3 python3-pip python3-venv \
  libgl1 libgl1-mesa-dev libosmesa6-dev \
  libegl1 patchelf ffmpeg

Why these matter?

  • libgl / mesa → OpenGL rendering
  • libosmesa → offscreen rendering (headless training)
  • ffmpeg → video export
  • patchelf → MuJoCo binary patching
$ uv venv --python 3.12 --seed mujoco
$ source mujoco/bin/activate

Upgrade pip:

$ pip install --upgrade pip setuptools wheel

3. Install MuJoCo (official method)

$ pip install mujoco

4. Install supporting libraries (typical RL stack)

You will almost always need these:

$ pip install numpy scipy matplotlib \
  glfw gymnasium stable-baselines3 torch \
  imageio imageio-ffmpeg

5. Verify the installation

$ nano test_mujoco.py
import mujoco
import mujoco.viewer

model = mujoco.MjModel.from_xml_string("""
<mujoco>
  <worldbody>
    <geom type="plane" size="1 1 0.1"/>
    <body pos="0 0 0.5">
      <geom type="sphere" size="0.1"/>
    </body>
  </worldbody>
</mujoco>
""")

data = mujoco.MjData(model)

with mujoco.viewer.launch_passive(model, data):
    while True:
        mujoco.mj_step(model, data)

Run it:

$ python test_mujoco.py

If you are training on a server or over SSH:

export MUJOCO_GL=egl

Simultion

URDF to XML


Examples

Load PyTorch Model Weights

import torch
import torch.nn as nn

class PolicyNetwork(nn.Module):
    def __init__(self, obs_dim, act_dim):
        super().__init__()
        self.net = nn.Sequential(
            nn.Linear(obs_dim, 256),
            nn.ReLU(),
            nn.Linear(256, 256),
            nn.ReLU(),
            nn.Linear(256, act_dim),
        )

    def forward(self, obs):
        return self.net(obs)
        
policy = PolicyNetwork(obs_dim, act_dim)
policy.load_state_dict(torch.load("trained_policy.pt"))
policy.eval()  # set to evaluation mode
Previous
Isaac Sim