Installation

MuJoCo-rs

MuJoCo-rs can be added to your project like so:

  • With visualization/rendering support:

    cargo add mujoco-rs --no-default-features --features "viewer viewer-ui renderer renderer-winit-fallback"
    
  • Without visualization/rendering support:

    cargo add mujoco-rs --no-default-features
    

See Optional Cargo features for information about available Cargo features. We strongly recommend to disable any rendering-related features when rendering is not needed, which will reduce compilation time.

Then MuJoCo installation needs to be configured, as described below. MuJoCo is the actual physics engine that MuJoCo-rs depends on.

Dependencies

MuJoCo

Dynamic linking (official MuJoCo build)

Note

MuJoCo official builds are available only in the form of a shared/dynamic library. This is the preferred way of providing MuJoCo as it involves the least amount of work.

When the goal is to provide MuJoCo as a dynamic dependency, you can either tell MuJoCo-rs to download MuJoCo automatically or you can download MuJoCo yourself.

To make MuJoCo-rs automatically download MuJoCo, enable MuJoCo-rs’s Cargo feature auto-download-mujoco:

cargo add mujoco-rs --no-default-features --features "auto-download-mujoco"

Then, set the environmental variable MUJOCO_DOWNLOAD_DIR to the absolute path into which the MuJoCo library will be extracted. Note that a subdirectory will be created automatically in the format mujoco-x.y.z, where x.y.z is the MuJoCo version.

export MUJOCO_DOWNLOAD_DIR=/home/user/libraries/

You should now be able to compile and run your crate.

Regardless of whether MuJoCo is downloaded by MuJoCo-rs or you provided it manually, you may encounter runtime errors about the library not being found. This can happen if the library is not located in a standard location nor added to the OS-dependent path environmental variable. You can fix these kind of errors like so:

Either copy libmujoco.so and libmujoco.so.x.y.z to the standard location (i.e., /usr/lib/)

or,

Update the LD_LIBRARY_PATH environment variable. Assuming libmujoco.so is located in /path/to/mujoco/lib/:

export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/path/to/mujoco/lib/

Static linking

Official MuJoCo builds include the precompiled MuJoCo library only in its shared (dynamic) form. To statically link, you’ll need to compile MuJoCo yourself. MuJoCo’s build system doesn’t (yet) support static linking, which is why we provide a modified MuJoCo repository with static linking support.

While MuJoCo-rs already provides a Rust-native 3D viewer, we understand that some projects wish to use the original C++ based 3D viewer (also named Simulate). To enable this, the modified MuJoCo repository also includes changes that support a safe interface between Rust and the C++ Simulate code.

To build statically linkable libraries, perform the following steps:

  1. Clone MuJoCo-rs’s repository,

  2. Change your directory to the cloned repository,

  3. Run commands:

    git submodule update --init --recursive
    cd ./mujoco/
    cmake -B build -S . -DBUILD_SHARED_LIBS:BOOL=OFF -DMUJOCO_HARDEN:BOOL=OFF -DCMAKE_BUILD_TYPE:STRING=Release -DCMAKE_INTERPROCEDURAL_OPTIMIZATION:BOOL=OFF -DMUJOCO_BUILD_EXAMPLES:BOOL=OFF
    cmake --build build --parallel --target glfw libmujoco_simulate --config=Release
    

    This was tested with the gcc compiler.

    Note that -DCMAKE_INTERPROCEDURAL_OPTIMIZATION:BOOL=OFF disables link-time optimization, thus resulting in slightly lower performance. Enabling it causes compatibility problems on the Linux platform. See the attention block below for more info.

    See also

    See this Dockerfile for a reproducible build environment which, to our knowledge, matches MuJoCo’s official build environment. The Dockerfile also installs the Rust toolchain, however our testing showed that static libraries built in the container also work outside of the container, even on the rust-lld linker.

    The Dockerfile defines a container running Ubuntu 22.04 and uses clang-13/clang++-13 as the compiler.

  4. Set the environmental variable MUJOCO_STATIC_LINK_DIR to the absolute path of the lib/ subdirectory inside mujoco/build/. Bash example:

    export MUJOCO_STATIC_LINK_DIR=/path/mujoco/build/lib/
    
  5. Build MuJoCo-rs

    cargo build
    

Attention

Link-time optimization

Above CMake configuration command disables link-time optimization (LTO). This results in worse performance but allows the compiled code to be used with the rust-lld linker, which is the default linker since Rust 1.90.0 on the x86_64-unknown-linux-gnu target.

If performance is critical for you, link-time optimization of MuJoCo code can be enabled during configuration:

cmake -B build -S . -DCMAKE_INTERPROCEDURAL_OPTIMIZATION:BOOL=ON ...

When LTO is enabled, the system linker must be used, because rust-lld doesn’t know how to read extra LTO information produced by other linkers:

RUSTFLAGS="-C linker-features=-lld" cargo build

In performance critical cases, it is also recommended to use the official MuJoCo shared (dynamic) libraries, which are generally more optimized. Performance gain with LTO enabled on static builds is about 10% more steps per second.