ROS2 Humble源码编译实战:Ubuntu系统下的完整指南与避坑手册
1. 环境准备打造编译ROS2 Humble的完美起点第一次在Ubuntu上编译ROS2 Humble时我花了整整两天时间解决各种环境问题。现在回想起来如果当时有人告诉我这些细节至少能节省80%的时间。下面是我总结的完整环境配置方案特别针对国内开发者优化。系统要求不是简单的版本匹配就行。虽然官方说Ubuntu 20.04/22.04都支持但我实测发现Ubuntu 22.04的默认Python版本(3.10)会导致某些包兼容性问题Ubuntu 20.04的默认Python3.8更稳定但需要手动安装更高版本的pip建议新手选择Ubuntu 20.04 LTS稳定性最好。磁盘空间官方说20GB但实际建议预留50GB因为源码下载约2GB编译中间文件可能占用15-20GB安装后的空间约8GBLocale设置看似简单却暗藏杀机。很多人忽略这一步直接编译结果遇到奇怪的编码错误。正确的做法是sudo apt update sudo apt install locales 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脚本还是报编码错误。重启后运行locale确认所有项都显示en_US.UTF-8才算成功。2. 软件源配置突破网络限制的实战技巧官方源在国内访问就像早高峰的地铁——又慢又容易断。经过多次尝试我找到了最稳定的配置方案。2.1 基础源配置先确保Universe仓库启用sudo apt install software-properties-common sudo add-apt-repository universe2.2 密钥获取的三种方案官方方法curl -sSL https://raw.githubusercontent.com...十有八九会失败。我收集了三种替代方案方案A国内镜像站sudo curl -sSL https://gitee.com/JCxiaohu/rosdistro/blob/master/ros.key -o /usr/share/keyrings/ros-archive-keyring.gpg方案B直接添加公钥sudo apt-key adv --keyserver hkp://keyserver.ubuntu.com:80 --recv-key F42ED6FBAB17C654方案C离线下载浏览器访问https://raw.githubusercontent.com/ros/rosdistro/master/ros.key保存为ros.key执行sudo cp ros.key /usr/share/keyrings/ros-archive-keyring.gpg2.3 源列表配置技巧执行这个命令时要注意$(lsb_release -cs)的返回值echo deb [arch$(dpkg --print-architecture)] http://packages.ros.org/ros2/ubuntu $(lsb_release -cs) main | sudo tee /etc/apt/sources.list.d/ros2-latest.list如果系统是Ubuntu 20.04但显示focal或者22.04显示jammy都是正常的。如果显示其他值说明系统识别有问题需要手动替换为正确的代号。3. 依赖安装那些官方没告诉你的细节依赖问题就像乐高积木少了一块——看似小事却能让你前功尽弃。这里分享几个关键点。3.1 基础工具安装先安装这些基础工具sudo apt update sudo apt install -y \ python3-flake8-docstrings \ python3-pip \ python3-pytest-cov \ ros-dev-tools特别注意如果遇到python3-flake8-docstrings安装失败可能是pip版本太旧。先升级pippython3 -m pip install --upgrade pip3.2 系统特定依赖Ubuntu 20.04和22.04需要的包不同Ubuntu 22.04sudo apt install -y \ python3-flake8-blind-except \ python3-flake8-builtins \ python3-flake8-class-newline \ python3-flake8-comprehensions \ python3-flake8-deprecated \ python3-flake8-import-order \ python3-flake8-quotes \ python3-pytest-repeat \ python3-pytest-rerunfailuresUbuntu 20.04python3 -m pip install -U \ flake8-blind-except \ flake8-builtins \ flake8-class-newline \ flake8-comprehensions \ flake8-deprecated \ flake8-import-order \ flake8-quotes \ pytest5.3 \ pytest-repeat \ pytest-rerunfailures3.3 常见依赖问题解决我遇到最棘手的问题是python3-rosdep的依赖冲突。解决方法sudo apt remove python3-rosdep python3 -m pip install rosdep sudo rosdep init rosdep update如果rosdep update失败试试这个rosdep update --include-eol-distros4. 源码获取与编译从下载到成功的完整路径源码获取就像网购——看似简单但快递(网络)可能让你抓狂。下面是我的实战经验。4.1 源码下载的两种方式官方方式mkdir -p ~/ros2_humble/src cd ~/ros2_humble vcs import --input https://raw.githubusercontent.com/ros2/ros2/humble/ros2.repos src国内优化方案浏览器访问https://raw.githubusercontent.com/ros2/ros2/humble/ros2.repos保存为ros2.repos执行vcs import --input ros2.repos src4.2 编译参数优化基础编译命令colcon build --symlink-install但直接这样编译容易出问题推荐使用colcon build --symlink-install --parallel-workers 4 --cmake-args -DCMAKE_BUILD_TYPERelease参数说明--parallel-workers 4限制并行任务数避免内存不足-DCMAKE_BUILD_TYPERelease优化编译结果4.3 常见编译错误解决内存不足 症状编译被系统强制终止 解决sudo fallocate -l 4G /swapfile sudo chmod 600 /swapfile sudo mkswap /swapfile sudo swapon /swapfile网络下载失败在浏览器打开报错中的URL下载文件找到对应的CMakeLists.txt文件将URL替换为本地路径如# 原内容 ExternalProject_Add(... URL https://example.com/file.tar.gz ...) # 修改为 ExternalProject_Add(... URL file:///home/user/downloads/file.tar.gz ...)5. 环境配置与测试验证你的劳动成果编译完成只是开始正确配置环境才能使用。这是我总结的最佳实践。5.1 环境变量设置每次打开新终端都需要执行. ~/ros2_humble/install/local_setup.bash为了方便可以添加到.bashrcecho source ~/ros2_humble/install/local_setup.bash ~/.bashrc5.2 基础功能测试开两个终端分别执行# 终端1 ros2 run demo_nodes_cpp talker # 终端2 ros2 run demo_nodes_py listener应该能看到talker发送消息listener接收消息。5.3 高级测试技巧测试RViz2ros2 run rviz2 rviz2测试TurtleSimros2 run turtlesim turtlesim_node ros2 run turtlesim turtle_teleop_key如果这些都能正常运行说明你的ROS2 Humble编译安装完全成功。