Habitat-Sim 和 Habitat-Lab 是 Meta(原 Facebook)开源的用于具身智能(Embodied AI)研究的仿真平台。Habitat-Sim 提供高效的 3D 场景仿真引擎,Habitat-Lab 则构建在 PyTorch 之上,为强化学习、导航等任务提供算法实现。这套工具链被广泛应用于机器人导航、物体交互等 AI 研究领域。
在 Ubuntu 22.04 上部署这套环境时,由于依赖项复杂且涉及图形渲染,常会遇到各种环境配置问题,特别是 "X Error of failed request: BadAccess" 这类与显示相关的报错。本文将详细记录从零开始的完整安装流程,并针对常见错误提供已验证的解决方案。
注意:虽然 Habitat 支持 CPU 模式,但 GPU 加速对仿真效率提升显著。实测在 RTX 3060 上,GPU 模式比 CPU 模式快 20-30 倍。
首先更新系统并安装编译工具:
bash复制sudo apt update && sudo apt upgrade -y
sudo apt install -y build-essential cmake git wget unzip
安装图形相关依赖(关键预防 X Error):
bash复制sudo apt install -y \
libgl1-mesa-dev \
libglu1-mesa-dev \
mesa-utils \
xorg-dev \
libxcb-xinerama0 \
libxkbcommon-dev \
libxrandr-dev \
libxi-dev
验证 OpenGL 支持:
bash复制glxinfo | grep "OpenGL version"
正常应显示类似 "OpenGL version string: 4.6.0 NVIDIA 510.47.03" 的输出。
建议使用特定版本以确保兼容性:
bash复制git clone --branch v0.2.1 https://github.com/facebookresearch/habitat-sim.git
cd habitat-sim
git submodule update --init --recursive
创建构建目录并配置 CMake:
bash复制mkdir build && cd build
cmake .. \
-DCMAKE_BUILD_TYPE=Release \
-DPYTHON_EXECUTABLE=$(which python) \
-DBUILD_GUI_VIEWERS=ON \
-DBUILD_WITH_BULLET=ON
关键参数说明:
BUILD_GUI_VIEWERS=ON:启用可视化调试工具BUILD_WITH_BULLET=ON:使用 Bullet 物理引擎-DCUDA_TOOLKIT_ROOT_DIR=/usr/local/cuda-11.3启动编译(建议至少 8GB 内存):
bash复制make -j$(nproc)
安装 Python 包:
bash复制cd ..
pip install -e .
验证安装:
python复制import habitat_sim
print(habitat_sim.__version__) # 应输出 0.2.1
bash复制git clone --branch v0.2.1 https://github.com/facebookresearch/habitat-lab.git
cd habitat-lab
pip install -e .
bash复制pip install \
torch==1.10.0+cu113 \
torchvision==0.11.1+cu113 \
-f https://download.pytorch.org/whl/torch_stable.html
注意:必须匹配 CUDA 版本。如果使用 CUDA 11.6,需修改为
+cu116。
运行点导航示例:
bash复制cd habitat-lab
python examples/example.py
正常情况应看到 3D 场景窗口弹出,agent 在房间中移动。
错误现象:
code复制X Error of failed request: BadAccess (attempt to access private resource denied)
Major opcode of failed request: 130 (MIT-SHM)
原因分析:
这是 X11 共享内存(SHM)扩展的权限问题,常见于:
解决方案:
方法1:禁用 MIT-SHM(推荐)
bash复制export QT_X11_NO_MITSHM=1
python examples/example.py
方法2:修改 X11 配置
bash复制sudo nano /etc/X11/xorg.conf
添加:
code复制Section "Extensions"
Option "MIT-SHM" "Disable"
EndSection
然后重启 X11 服务。
方法3:使用虚拟帧缓冲(无头模式)
bash复制sudo apt install xvfb
xvfb-run -a -s "-screen 0 1024x768x24" python examples/example.py
问题1:ImportError: libGL.so.1
code复制解决方案:
sudo apt install libgl1
问题2:EGL 初始化失败
code复制解决方案:
export MESA_GL_VERSION_OVERRIDE=4.1
问题3:CUDA out of memory
code复制修改 examples/example.py 中的分辨率:
config.SIMULATOR.DEPTH_SENSOR.WIDTH = 256
config.SIMULATOR.DEPTH_SENSOR.HEIGHT = 256
测试需要场景数据集:
bash复制python -m habitat_sim.utils.datasets_download \
--uids habitat_test_scenes \
--data-path data/
python复制config.SIMULATOR.RGB_SENSOR.HEIGHT = 480
config.SIMULATOR.RGB_SENSOR.WIDTH = 640
python复制config.SIMULATOR.RENDERER.ENABLE_ASYNC_RENDER = True
python复制from habitat.core.env import VectorEnv
env = VectorEnv([make_env_fn() for _ in range(4)])
yaml复制scene: path/to/your/scene.glb
启动交互式查看器:
bash复制./build/utils/viewer /path/to/scene.glb
快捷键说明:
启用详细日志:
python复制import habitat_sim
habitat_sim.logger.setLevel(habitat_sim.logging.DEBUG)
获取系统信息:
python复制print(habitat_sim.get_build_config())
使用 PyTorch profiler:
python复制with torch.profiler.profile(
activities=[torch.profiler.ProfilerActivity.CPU],
schedule=torch.profiler.schedule(wait=1, warmup=1, active=3),
on_trace_ready=torch.profiler.tensorboard_trace_handler('./log')
) as p:
for _ in range(5):
observations = env.step(action)
p.step()