保姆级避坑指南:Ubuntu 20.04下ROS2 Humble源码编译全流程(附rosdep update失败解决方案)

张开发
2026/6/28 14:28:38 15 分钟阅读
保姆级避坑指南:Ubuntu 20.04下ROS2 Humble源码编译全流程(附rosdep update失败解决方案)
Ubuntu 20.04下ROS2 Humble源码编译避坑实战手册作为一名长期在机器人开发一线工作的工程师我深知从源码编译ROS2对于初学者来说就像在雷区中行走。每次看到新手在编译过程中反复碰壁最终放弃的场景都让我想起自己当年踩过的那些坑。这份指南将带你避开90%的常见陷阱用最短的时间完成ROS2 Humble的源码编译。1. 环境准备那些官方文档没告诉你的细节在开始下载源码之前环境配置的完备性直接决定了后续编译的成功率。很多教程会轻描淡写地带过这部分但实际上这里埋着最多的地雷。1.1 系统基础配置首先检查你的Ubuntu 20.04是否满足以下基本要求至少50GB的可用磁盘空间源码编译会占用大量空间稳定的网络连接最好能访问国际网络8GB以上内存16GB更佳关键一步是设置正确的locale这会影响后续很多工具的运行sudo locale-gen en_US en_US.UTF-8 sudo update-locale LC_ALLen_US.UTF-8 LANGen_US.UTF-8 export LANGen_US.UTF-8注意如果不执行这一步后续可能会遇到各种奇怪的编码错误特别是Python相关的报错。1.2 依赖安装的隐藏陷阱官方文档列出的依赖看似简单但有几个特别容易出问题的地方sudo apt update sudo apt install -y \ build-essential \ cmake \ git \ python3-colcon-common-extensions \ python3-lark-parser \ python3-pip \ python3-rosdep \ python3-vcstool \ wgetPython版本问题是最大的坑之一。Ubuntu 20.04默认的Python3是3.8版本但有些工具会意外调用系统其他Python版本。检查并确保python3 --version # 应该显示3.8.x which python3 # 应该显示/usr/bin/python3如果发现版本不对可以通过以下命令修正sudo update-alternatives --config python32. 源码获取当网络成为最大障碍获取ROS2源码本应是最简单的步骤但在实际环境中却常常成为第一道难关。2.1 创建工作空间mkdir -p ~/ros2_humble/src cd ~/ros2_humble wget https://raw.githubusercontent.com/ros2/ros2/humble/ros2.repos2.2 使用vcs工具导入源码理想情况下只需执行vcs import src ros2.repos但现实往往很骨感你可能会遇到下载速度极慢几KB/s连接超时中断某些仓库无法克隆解决方案是使用国内镜像源或代理。对于vcs import可以修改ros2.repos文件中的URLsed -i s/github.com/hub.fastgit.org/g ros2.repos或者使用更可靠的方法 - 预先下载所有仓库#!/bin/bash mkdir -p src while read -r line; do if [[ $line *git* ]]; then repo$(echo $line | awk -F {print $3}) path$(echo $line | awk -F {print $1}) git clone https://hub.fastgit.org/${repo#https://github.com/} src/$path fi done ros2.repos3. rosdep初始化跨越那道墙rosdep init和rosdep update堪称ROS安装过程中的终极杀手90%的失败都发生在这里。3.1 初始化rosdepsudo rosdep init这一步可能会报错rosdep already initialized。这是因为系统里可能已经有残留配置。彻底清理后再试sudo rm -rf /etc/ros/rosdep/sources.list.d/* sudo rosdep init3.2 rosdep update的终极解决方案直接运行rosdep update几乎肯定会失败。我们需要多管齐下方法一使用国内镜像sudo pip install rosdepc sudo rosdepc init rosdepc update方法二手动修改源编辑/etc/ros/rosdep/sources.list.d/20-default.list将内容替换为# os-specific listings first yaml https://ghproxy.com/https://raw.githubusercontent.com/ros/rosdistro/master/rosdep/osx-homebrew.yaml osx # generic yaml https://ghproxy.com/https://raw.githubusercontent.com/ros/rosdistro/master/rosdep/base.yaml yaml https://ghproxy.com/https://raw.githubusercontent.com/ros/rosdistro/master/rosdep/python.yaml yaml https://ghproxy.com/https://raw.githubusercontent.com/ros/rosdistro/master/rosdep/ruby.yaml gbpdistro https://ghproxy.com/https://raw.githubusercontent.com/ros/rosdistro/master/releases/fuerte.yaml fuerte # newer distributions (Groovy, Hydro, ...) must not be listed anymore, they are being fetched from the rosdistro index.yaml instead然后再次尝试rosdep update方法三离线模式如果网络实在无法解决可以尝试离线方式在一台能正常rosdep update的机器上执行然后将/etc/ros/rosdep/sources.cache复制到目标机器使用--osubuntu:focal参数跳过部分检查4. 编译过程中的常见陷阱当你好不容易走到编译这一步新的挑战才刚刚开始。4.1 内存不足问题ROS2 Humble的编译对内存要求较高如果内存不足可能导致编译卡住无响应随机段错误奇怪的模板错误解决方案# 限制并行编译任务数 colcon build --symlink-install --parallel-workers 2 # 或者使用更少内存的编译选项 colcon build --symlink-install --cmake-args -DCMAKE_BUILD_TYPERelease4.2 Python包冲突ROS2依赖大量Python包很容易与其他项目产生冲突。建议# 创建独立的Python虚拟环境 python3 -m venv ~/ros2_humble/venv source ~/ros2_humble/venv/bin/activate # 然后再执行编译4.3 重复包名错误如果看到类似Duplicate package names not supported的错误通常是因为重复执行了vcs import导致某些包被多次导入手动克隆了某些仓库导致冲突解决方法# 彻底清理src目录 rm -rf ~/ros2_humble/src/* # 重新导入 vcs import src ros2.repos5. 测试与验证确保一切正常编译完成后不要急着庆祝先运行几个基本测试# 在新终端中 source ~/ros2_humble/install/local_setup.bash ros2 run demo_nodes_cpp talker # 在另一个新终端中 source ~/ros2_humble/install/local_setup.bash ros2 run demo_nodes_cpp listener如果能看到talker发送、listener接收消息说明基本功能正常。但为了全面验证建议再测试# 测试Python接口 ros2 run demo_nodes_py talker_py ros2 run demo_nodes_py listener_py # 测试ROS1桥接如果安装了 ros2 run ros1_bridge dynamic_bridge6. 高级技巧与优化建议经过多次编译实践我总结出几个能大幅提升体验的技巧使用ccache加速后续编译sudo apt install ccache echo export PATH/usr/lib/ccache:$PATH ~/.bashrc source ~/.bashrc # 后续编译会自动使用缓存选择性编译如果只修改了某个包可以只重新编译它colcon build --symlink-install --packages-select YOUR_PACKAGE_NAME内存监控编译过程中监控内存使用watch -n 1 free -h如果发现内存不足可以及时调整并行任务数。

更多文章