Ubuntu18.04: mujoco、mujoco-py的安装及相关错误解决

目录

安装步骤

1 安装mujoco

2 安装mujoco-py

3 安装mujoco-py可能出现的错误及解决办法

3.1 错误1:No such file or directory #include

3.2 错误2: cannot find -lGL: No such file or directory

3.3 错误3:明明已经按照上述安装步骤完成了mujoco_py的安装,测试代码运行也很正常,但是一旦切换到将要运行代码的文件夹下,再次运行import mujoco_py时,仍出现错误(该错误在python=3.6时遇到):gym.error.DependencyNotInstalled: No module named 'mujoco_py'. (HINT: you need to install mujoco_py, and also perform the setup instructions here: https://github.com/openai/mujoco-py/.)


安装步骤

1 安装mujoco

官方链接地址:https://github.com/openai/mujoco-py

根据上述链接,首先在根目录~/下创建文件夹~/.mujoco:(默认是这个位置,否则后续会出错)

mkdir .mujoco

把从官方下载地址(https://github.com/deepmind/mujoco/releases/tag/2.1.0)下载到的mujoco压缩包传入服务器并解压至~/.mujoco/mujoco210文件夹:

tar -zxvf mujoco210-linux-x86_64.tar.gz  -C ~/.mujoco

编辑环境变量:

vim ~/.bashrc

在其中加入:

export LD_LIBRARY_PATH=~/.mujoco/mujoco210/bin

更新环境变量:

source ~/.bashrc

测试mujoco是否安装成功:

cd ~/.mujoco/mujoco210/bin
./simulate ../model/humanoid.xml

**迷惑的是在服务器端运行上面的第二行代码时,一直报错:

 ERROR: could not initialize GLFW

 查询资料后也并没有成功解决(知道的小伙伴也可以在评论区交流呀~)

2 安装mujoco-py

首先从官网下载代码:

git clone https://github.com/openai/mujoco-py.git

之后,按照Ubuntu20.04安装mujoco_小帅吖的博客-CSDN博客_ubuntu安装mujoco中提到的办法进行安装(比直接使用pip3 install -U 'mujoco-py<2.2,>=2.1'安装要出错少,直接安装的话会缺很多依赖的包)

conda create -n mujo python=3.8
conda activate mujo
cd ~/mujoco-py
pip3 install -U 'mujoco-py<2.2,>=2.1'
pip3 install -r requirements.txt
pip3 install -r requirements.dev.txt
python3 setup.py install

测试一下mujoco_py是否安装成功:

$ python3
import mujoco_py
import os
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安装成功。 

3 安装mujoco-py可能出现的错误及解决办法

3.1 错误1:No such file or directory #include

运行到import mujoco_py时,报错:

/root/code/mujoco-py/mujoco_py/gl/osmesashim.c:1:10: fatal error: GL/osmesa.h: No such file or directory
 #include
          ^~~~~~~~~~~~~
compilation terminated.
Traceback (most recent call last):
  File "/opt/conda/envs/mujo/lib/python3.8/site-packages/setuptools/_distutils/unixccompiler.py", line 186, in _compile
    self.spawn(compiler_so + cc_args + [src, '-o', obj] + extra_postargs)
  File "/opt/conda/envs/mujo/lib/python3.8/site-packages/setuptools/_distutils/ccompiler.py", line 987, in spawn
    spawn(cmd, dry_run=self.dry_run, **kwargs)
  File "/opt/conda/envs/mujo/lib/python3.8/site-packages/setuptools/_distutils/spawn.py", line 70, in spawn
    raise DistutilsExecError(
distutils.errors.DistutilsExecError: command '/usr/bin/gcc' failed with exit code 1

根据 https://github.com/openai/mujoco-py/issues/297以及https://zhuanlan.zhihu.com/p/547442285

执行指令:

sudo apt install libosmesa6-dev
pip install patchelf

3.2 错误2: cannot find -lGL: No such file or directory

之后报错:

/opt/conda/envs/mujo/compiler_compat/ld: cannot find -lGL: No such file or directory
collect2: error: ld returned 1 exit status
Traceback (most recent call last):
  File "/opt/conda/envs/mujo/lib/python3.8/site-packages/setuptools/_distutils/unixccompiler.py", line 267, in link
    self.spawn(linker + ld_args)
  File "/opt/conda/envs/mujo/lib/python3.8/site-packages/setuptools/_distutils/ccompiler.py", line 987, in spawn
    spawn(cmd, dry_run=self.dry_run, **kwargs)
  File "/opt/conda/envs/mujo/lib/python3.8/site-packages/setuptools/_distutils/spawn.py", line 70, in spawn
    raise DistutilsExecError(
distutils.errors.DistutilsExecError: command '/usr/bin/gcc' failed with exit code 1

 执行指令:

sudo apt install libosmesa6-dev

错误消失,成功执行所有的测试代码,mujoco安装成功。

3.3 错误3:明明已经按照上述安装步骤完成了mujoco_py的安装,测试代码运行也很正常,但是一旦切换到将要运行代码的文件夹下,再次运行import mujoco_py时,仍出现错误(该错误在python=3.6时遇到):gym.error.DependencyNotInstalled: No module named 'mujoco_py'. (HINT: you need to install mujoco_py, and also perform the setup instructions here: https://github.com/openai/mujoco-py/.)

初步判断,这应该是由于使用的mujoco版本、mujoco_py版本与python版本不匹配造成的。使用mujoco210,mujoco_py==2.1.x.x版本时,最好使用python3.7及其以上的python版本。

解决方案1:使用python3.7及其以上的python版本重新配置环境再运行项目代码即可。

解决方案2:

参照博客:Mujoco&Mujoco-py安装教程以及常见报错解决方法 - 知乎

执行以下安装指令:

pip install mujoco-py==2.1.2.14

 就不会再出现错误3所述情况了。

你可能感兴趣的:(ubuntu,安装,bug解决方案,linux,运维,服务器)