在ubuntu上配置mujoco,安装mujoco_py

自从deepmind收购Mujoco后,之前在服务器上配置的Mujoco就出现问题了,加之因offlinerl需安装d4rl,依赖mujoco,因此如果mujoco安装失败,后续的安装难以开展。
经过多番尝试,终于首先在本地ubuntu上安装成功mujoco,为后续在服务器上安装探索了路径。

环境:ubuntu 18.04.5 LTS, Anaconda, python 3.8
前提:安装anaconda

一、安装mujoco
1.在Anaconda中创建虚拟环境 anaconda create -n d4rl python=3.8(这里使用python3.8,是因为网上说后续安装gym需要3.8)
2.创建好后通过conda activate d4rl 切换到配置的环境下,注意通过 python --version 查看当前创建的python 环境版本是否是3.8,否则会报错。可能需要关闭zsh,再重新打开,否则虽然切换到d4rl下了,但实际上可能只是base环境。这个问题可能是我的.zshrc文件中的某些设置引发的,暂时未找到原因。
3.虽然mujoco已经更新到mujoco210,但实践中发现,其实mujoco200是匹配的版本。但deepmind的github官网上只有最新版本的mujoco,并没有历史版本的,因此需要在原来的Mujoco页面(roboti.us/download.html)Download下下载 mujoco200 linux(不再需要mjkey文件,mujoco150之前的版本还需要)。
4.在本地机器 /home/zlf 路径下创建隐藏文件夹 .mujoco
5.把下载好的mujoco200 linux 通过 cp ~/Download/mujoco200 linux ~/.mujoco 复制到创建的.mujoco文件夹下。
6.通过解压指令解压mujoco200 linux,并把解压出的文件夹通过指令 mv mujoco200 linux mujoco200, 即把文件夹名称修改为mujoco200
7.通过vim .zshrc 打开 .zshrc文件,并添加路径名称:
export MUJOCO_KEY_PATH=~/.mujoco${MUJOCO_KEY_PATH}
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/home/(用户名)/.mujoco/mujoco200/bin
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/lib/nvidia
8.wq指令保存后,source ~/.zshrc 启动修改
9.到这里Mujoco就配置好了,可以检查一下:
cd ~/.mujoco/mujoco200/bin
./simulate ../model/humanoid.xml
这里可能simulate会报错,上网查一下好像用chmod 777 simulate就可以解决。
当然显卡驱动版本太低的话,只会出现一个黑框,没有图像。
我本人的机器确实只显示了一个黑框:

  黑框关闭后出现了一句:
  ![](https://img2022.cnblogs.com/blog/2010713/202204/2010713-20220419213502076-1228419267.png)

 至此,mujoco安装完成。

 (注意:如果是提示需要安装mikey.txt,则下载mjkey.txt后cp 到mujoco200和mujoco200/bin,即可)

二、安装mujoco_py
1. 在配置环境(python 3.8)下, pip install mujoco_py==2.0.2.10
2.安装好后,python---import mujoco_py,出现报错:
>>> import mujoco_py
Compiling /home/zlf/anaconda3/envs/d4rl/lib/python3.8/site-packages/mujoco_py/cymj.pyx because it depends on /home/zlf/anaconda3/envs/d4rl/lib/python3.8/site-packages/mujoco_py/pxd/mujoco.pxd.
[1/1] Cythonizing /home/zlf/anaconda3/envs/d4rl/lib/python3.8/site-packages/mujoco_py/cymj.pyx
running build_ext
building 'mujoco_py.cymj' extension
creating /home/zlf/anaconda3/envs/d4rl/lib/python3.8/site-packages/mujoco_py/generated/_pyxbld_2.0.2.10_38_linuxcpuextensionbuilder
creating /home/zlf/anaconda3/envs/d4rl/lib/python3.8/site-packages/mujoco_py/generated/_pyxbld_2.0.2.10_38_linuxcpuextensionbuilder/temp.linux-x86_64-3.8
creating /home/zlf/anaconda3/envs/d4rl/lib/python3.8/site-packages/mujoco_py/generated/_pyxbld_2.0.2.10_38_linuxcpuextensionbuilder/temp.linux-x86_64-3.8/home
creating /home/zlf/anaconda3/envs/d4rl/lib/python3.8/site-packages/mujoco_py/generated/_pyxbld_2.0.2.10_38_linuxcpuextensionbuilder/temp.linux-x86_64-3.8/home/zlf
creating /home/zlf/anaconda3/envs/d4rl/lib/python3.8/site-packages/mujoco_py/generated/_pyxbld_2.0.2.10_38_linuxcpuextensionbuilder/temp.linux-x86_64-3.8/home/zlf/anaconda3
creating /home/zlf/anaconda3/envs/d4rl/lib/python3.8/site-packages/mujoco_py/generated/_pyxbld_2.0.2.10_38_linuxcpuextensionbuilder/temp.linux-x86_64-3.8/home/zlf/anaconda3/envs
creating /home/zlf/anaconda3/envs/d4rl/lib/python3.8/site-packages/mujoco_py/generated/_pyxbld_2.0.2.10_38_linuxcpuextensionbuilder/temp.linux-x86_64-3.8/home/zlf/anaconda3/envs/d4rl
creating /home/zlf/anaconda3/envs/d4rl/lib/python3.8/site-packages/mujoco_py/generated/_pyxbld_2.0.2.10_38_linuxcpuextensionbuilder/temp.linux-x86_64-3.8/home/zlf/anaconda3/envs/d4rl/lib
creating /home/zlf/anaconda3/envs/d4rl/lib/python3.8/site-packages/mujoco_py/generated/_pyxbld_2.0.2.10_38_linuxcpuextensionbuilder/temp.linux-x86_64-3.8/home/zlf/anaconda3/envs/d4rl/lib/python3.8
creating /home/zlf/anaconda3/envs/d4rl/lib/python3.8/site-packages/mujoco_py/generated/_pyxbld_2.0.2.10_38_linuxcpuextensionbuilder/temp.linux-x86_64-3.8/home/zlf/anaconda3/envs/d4rl/lib/python3.8/site-packages
creating /home/zlf/anaconda3/envs/d4rl/lib/python3.8/site-packages/mujoco_py/generated/_pyxbld_2.0.2.10_38_linuxcpuextensionbuilder/temp.linux-x86_64-3.8/home/zlf/anaconda3/envs/d4rl/lib/python3.8/site-packages/mujoco_py
creating /home/zlf/anaconda3/envs/d4rl/lib/python3.8/site-packages/mujoco_py/generated/_pyxbld_2.0.2.10_38_linuxcpuextensionbuilder/temp.linux-x86_64-3.8/home/zlf/anaconda3/envs/d4rl/lib/python3.8/site-packages/mujoco_py/gl
gcc -pthread -B /home/zlf/anaconda3/envs/d4rl/compiler_compat -Wl,--sysroot=/ -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -fPIC -I/home/zlf/anaconda3/envs/d4rl/lib/python3.8/site-packages/mujoco_py -I/home/zlf/.mujoco/mujoco200/include -I/home/zlf/anaconda3/envs/d4rl/lib/python3.8/site-packages/numpy/core/include -I/home/zlf/anaconda3/envs/d4rl/include/python3.8 -c /home/zlf/anaconda3/envs/d4rl/lib/python3.8/site-packages/mujoco_py/cymj.c -o /home/zlf/anaconda3/envs/d4rl/lib/python3.8/site-packages/mujoco_py/generated/_pyxbld_2.0.2.10_38_linuxcpuextensionbuilder/temp.linux-x86_64-3.8/home/zlf/anaconda3/envs/d4rl/lib/python3.8/site-packages/mujoco_py/cymj.o -fopenmp -w
gcc -pthread -B /home/zlf/anaconda3/envs/d4rl/compiler_compat -Wl,--sysroot=/ -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -fPIC -I/home/zlf/anaconda3/envs/d4rl/lib/python3.8/site-packages/mujoco_py -I/home/zlf/.mujoco/mujoco200/include -I/home/zlf/anaconda3/envs/d4rl/lib/python3.8/site-packages/numpy/core/include -I/home/zlf/anaconda3/envs/d4rl/include/python3.8 -c /home/zlf/anaconda3/envs/d4rl/lib/python3.8/site-packages/mujoco_py/gl/osmesashim.c -o /home/zlf/anaconda3/envs/d4rl/lib/python3.8/site-packages/mujoco_py/generated/_pyxbld_2.0.2.10_38_linuxcpuextensionbuilder/temp.linux-x86_64-3.8/home/zlf/anaconda3/envs/d4rl/lib/python3.8/site-packages/mujoco_py/gl/osmesashim.o -fopenmp -w
creating /home/zlf/anaconda3/envs/d4rl/lib/python3.8/site-packages/mujoco_py/generated/_pyxbld_2.0.2.10_38_linuxcpuextensionbuilder/lib.linux-x86_64-3.8
creating /home/zlf/anaconda3/envs/d4rl/lib/python3.8/site-packages/mujoco_py/generated/_pyxbld_2.0.2.10_38_linuxcpuextensionbuilder/lib.linux-x86_64-3.8/mujoco_py
gcc -pthread -shared -B /home/zlf/anaconda3/envs/d4rl/compiler_compat -L/home/zlf/anaconda3/envs/d4rl/lib -Wl,-rpath=/home/zlf/anaconda3/envs/d4rl/lib -Wl,--no-as-needed -Wl,--sysroot=/ /home/zlf/anaconda3/envs/d4rl/lib/python3.8/site-packages/mujoco_py/generated/_pyxbld_2.0.2.10_38_linuxcpuextensionbuilder/temp.linux-x86_64-3.8/home/zlf/anaconda3/envs/d4rl/lib/python3.8/site-packages/mujoco_py/cymj.o /home/zlf/anaconda3/envs/d4rl/lib/python3.8/site-packages/mujoco_py/generated/_pyxbld_2.0.2.10_38_linuxcpuextensionbuilder/temp.linux-x86_64-3.8/home/zlf/anaconda3/envs/d4rl/lib/python3.8/site-packages/mujoco_py/gl/osmesashim.o -L/home/zlf/.mujoco/mujoco200/bin -Wl,-R/home/zlf/.mujoco/mujoco200/bin -lmujoco200 -lglewosmesa -lOSMesa -lGL -o /home/zlf/anaconda3/envs/d4rl/lib/python3.8/site-packages/mujoco_py/generated/_pyxbld_2.0.2.10_38_linuxcpuextensionbuilder/lib.linux-x86_64-3.8/mujoco_py/cymj.cpython-38-x86_64-linux-gnu.so -fopenmp
/home/zlf/anaconda3/envs/d4rl/compiler_compat/ld: cannot find -lGL
collect2: error: ld returned 1 exit status
Traceback (most recent call last):
File "/home/zlf/anaconda3/envs/d4rl/lib/python3.8/site-packages/setuptools/_distutils/unixccompiler.py", line 249, in link
self.spawn(linker + ld_args)
File "/home/zlf/anaconda3/envs/d4rl/lib/python3.8/site-packages/setuptools/_distutils/ccompiler.py", line 917, in spawn
spawn(cmd, dry_run=self.dry_run, **kwargs)
File "/home/zlf/anaconda3/envs/d4rl/lib/python3.8/site-packages/setuptools/_distutils/spawn.py", line 68, in spawn
raise DistutilsExecError(
distutils.errors.DistutilsExecError: command '/usr/bin/gcc' failed with exit code 1

      During handling of the above exception, another exception occurred:

      Traceback (most recent call last):
        File "<stdin>", line 1, in <module>
        File "/home/zlf/anaconda3/envs/d4rl/lib/python3.8/site-packages/mujoco_py/__init__.py", line 3, in <module>
          from mujoco_py.builder import cymj, ignore_mujoco_warnings, functions, MujocoException
        File "/home/zlf/anaconda3/envs/d4rl/lib/python3.8/site-packages/mujoco_py/builder.py", line 510, in <module>
          cymj = load_cython_ext(mujoco_path)
        File "/home/zlf/anaconda3/envs/d4rl/lib/python3.8/site-packages/mujoco_py/builder.py", line 105, in load_cython_ext
          cext_so_path = builder.build()
        File "/home/zlf/anaconda3/envs/d4rl/lib/python3.8/site-packages/mujoco_py/builder.py", line 221, in build
          built_so_file_path = self._build_impl()
        File "/home/zlf/anaconda3/envs/d4rl/lib/python3.8/site-packages/mujoco_py/builder.py", line 273, in _build_impl
          so_file_path = super()._build_impl()
        File "/home/zlf/anaconda3/envs/d4rl/lib/python3.8/site-packages/mujoco_py/builder.py", line 244, in _build_impl
          dist.run_commands()
        File "/home/zlf/anaconda3/envs/d4rl/lib/python3.8/site-packages/setuptools/_distutils/dist.py", line 967, in run_commands
          self.run_command(cmd)
        File "/home/zlf/anaconda3/envs/d4rl/lib/python3.8/site-packages/setuptools/dist.py", line 1214, in run_command
          super().run_command(command)
        File "/home/zlf/anaconda3/envs/d4rl/lib/python3.8/site-packages/setuptools/_distutils/dist.py", line 986, in run_command
          cmd_obj.run()
        File "/home/zlf/anaconda3/envs/d4rl/lib/python3.8/site-packages/Cython/Distutils/old_build_ext.py", line 186, in run
          _build_ext.build_ext.run(self)
        File "/home/zlf/anaconda3/envs/d4rl/lib/python3.8/site-packages/setuptools/_distutils/command/build_ext.py", line 339, in run
          self.build_extensions()
        File "/home/zlf/anaconda3/envs/d4rl/lib/python3.8/site-packages/mujoco_py/builder.py", line 144, in build_extensions
          build_ext.build_extensions(self)
        File "/home/zlf/anaconda3/envs/d4rl/lib/python3.8/site-packages/Cython/Distutils/old_build_ext.py", line 195, in build_extensions
          _build_ext.build_ext.build_extensions(self)
        File "/home/zlf/anaconda3/envs/d4rl/lib/python3.8/site-packages/setuptools/_distutils/command/build_ext.py", line 448, in build_extensions
          self._build_extensions_serial()
        File "/home/zlf/anaconda3/envs/d4rl/lib/python3.8/site-packages/setuptools/_distutils/command/build_ext.py", line 473, in _build_extensions_serial
          self.build_extension(ext)
        File "/home/zlf/anaconda3/envs/d4rl/lib/python3.8/site-packages/setuptools/_distutils/command/build_ext.py", line 550, in build_extension
          self.compiler.link_shared_object(
        File "/home/zlf/anaconda3/envs/d4rl/lib/python3.8/site-packages/setuptools/_distutils/ccompiler.py", line 713, in link_shared_object
          self.link(CCompiler.SHARED_OBJECT, objects,
        File "/home/zlf/anaconda3/envs/d4rl/lib/python3.8/site-packages/setuptools/_distutils/unixccompiler.py", line 251, in link
          raise LinkError(msg)
      distutils.errors.LinkError: command '/usr/bin/gcc' failed with exit code 1


      3. 这是因为缺少包,安装:sudo apt-get install libgl1-mesa-dev ,即可。
      4. 再输入 python   ---- import  mujoco_py,出现如下:
          
            >>> import mujoco_py

running build_ext
building 'mujoco_py.cymj' extension
gcc -pthread -B /home/zlf/anaconda3/envs/d4rl/compiler_compat -Wl,--sysroot=/ -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -fPIC -I/home/zlf/anaconda3/envs/d4rl/lib/python3.8/site-packages/mujoco_py -I/home/zlf/.mujoco/mujoco200/include -I/home/zlf/anaconda3/envs/d4rl/lib/python3.8/site-packages/numpy/core/include -I/home/zlf/anaconda3/envs/d4rl/include/python3.8 -c /home/zlf/anaconda3/envs/d4rl/lib/python3.8/site-packages/mujoco_py/cymj.c -o /home/zlf/anaconda3/envs/d4rl/lib/python3.8/site-packages/mujoco_py/generated/_pyxbld_2.0.2.10_38_linuxcpuextensionbuilder/temp.linux-x86_64-3.8/home/zlf/anaconda3/envs/d4rl/lib/python3.8/site-packages/mujoco_py/cymj.o -fopenmp -w
gcc -pthread -B /home/zlf/anaconda3/envs/d4rl/compiler_compat -Wl,--sysroot=/ -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -fPIC -I/home/zlf/anaconda3/envs/d4rl/lib/python3.8/site-packages/mujoco_py -I/home/zlf/.mujoco/mujoco200/include -I/home/zlf/anaconda3/envs/d4rl/lib/python3.8/site-packages/numpy/core/include -I/home/zlf/anaconda3/envs/d4rl/include/python3.8 -c /home/zlf/anaconda3/envs/d4rl/lib/python3.8/site-packages/mujoco_py/gl/osmesashim.c -o /home/zlf/anaconda3/envs/d4rl/lib/python3.8/site-packages/mujoco_py/generated/_pyxbld_2.0.2.10_38_linuxcpuextensionbuilder/temp.linux-x86_64-3.8/home/zlf/anaconda3/envs/d4rl/lib/python3.8/site-packages/mujoco_py/gl/osmesashim.o -fopenmp -w
gcc -pthread -shared -B /home/zlf/anaconda3/envs/d4rl/compiler_compat -L/home/zlf/anaconda3/envs/d4rl/lib -Wl,-rpath=/home/zlf/anaconda3/envs/d4rl/lib -Wl,--no-as-needed -Wl,--sysroot=/ /home/zlf/anaconda3/envs/d4rl/lib/python3.8/site-packages/mujoco_py/generated/_pyxbld_2.0.2.10_38_linuxcpuextensionbuilder/temp.linux-x86_64-3.8/home/zlf/anaconda3/envs/d4rl/lib/python3.8/site-packages/mujoco_py/cymj.o /home/zlf/anaconda3/envs/d4rl/lib/python3.8/site-packages/mujoco_py/generated/_pyxbld_2.0.2.10_38_linuxcpuextensionbuilder/temp.linux-x86_64-3.8/home/zlf/anaconda3/envs/d4rl/lib/python3.8/site-packages/mujoco_py/gl/osmesashim.o -L/home/zlf/.mujoco/mujoco200/bin -Wl,-R/home/zlf/.mujoco/mujoco200/bin -lmujoco200 -lglewosmesa -lOSMesa -lGL -o /home/zlf/anaconda3/envs/d4rl/lib/python3.8/site-packages/mujoco_py/generated/_pyxbld_2.0.2.10_38_linuxcpuextensionbuilder/lib.linux-x86_64-3.8/mujoco_py/cymj.cpython-38-x86_64-linux-gnu.so -fopenmp

即证明mujoco_py 安装成功

    5.安装好mujoco_py之后可以用python检查一下:(直接在命令行输入python,然后输入以下代码):
        import mujoco_py   # 这里第一次import mujoco_py可能要等待一下,会有一些显示,等完成之后再import一次

              import os
              mj_path, _ = mujoco_py.utils.discover_mujoco()     # 这里好像修改了?改成mj_path = mujoco_py.utils.discover_mujoco()
              xml_path = os.path.join(mj_path, 'model', 'humanoid.xml')
              model = mujoco_py.load_model_from_path(xml_path)
              sim = mujoco_py.MjSim(model)
              print(sim.data.qpos)
              # [0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0. 0.]

              sim.step()
              print(sim.data.qpos)
              # [-2.09531783e-19  2.72130735e-05  6.14480786e-22 -3.45474715e-06
              #   7.42993721e-06 -1.40711141e-04 -3.04253586e-04 -2.07559344e-04
              #   8.50646247e-05 -3.45474715e-06  7.42993721e-06 -1.40711141e-04
              #  -3.04253586e-04 -2.07559344e-04 -8.50646247e-05  1.11317030e-04
              #  -7.03465386e-05 -2.22862221e-05 -1.11317030e-04  7.03465386e-05
              #  -2.22862221e-05]
            
            没问题,即mujoco_py安装完毕。

参考文献:

    1.  安装Mujoco、Mujoco_py、D4RL、gym、d3rlpy以及Pycharm远程连接服务器问题 (https://zhuanlan.zhihu.com/p/434073300)
    2.  安装mujoco_py出现:anaconda3/compiler_compat/ld: cannot find -lGL    (https://blog.csdn.net/jiangjiang_jian/article/details/100986205)
    3.   Anaconda-用conda创建python虚拟环境   (zhuanlan.zhihu.com/p/94744929)
    4.   Import error. Trying to rebuild mujoco_py. running build_ext building 'mujoco_py.cymj' extension distutils.errors.CompileError: command '/usr/local/bin/gcc-9' failed with exit status 1 #539  (https://github.com/openai/mujoco-py/issues/539)
posted @ 2022-04-19 20:57  呦呦南山  阅读(3877)  评论(0编辑  收藏  举报