欢迎来到本教程!本文将详细介绍如何在Ubuntu 22.04系统上同时安装ROS1 Noetic和ROS2 Humble,实现两者的兼容安装,让你在同一台机器上无缝切换使用ROS1和ROS2。无论你是ROS新手还是老手,都能轻松跟随本指南完成安装。
确保你的系统是Ubuntu 22.04,并且已经更新到最新:
sudo apt update && sudo apt upgrade -y ROS1 Noetic是专为Ubuntu 20.04设计的,但在Ubuntu 22.04上也可以通过源码或二进制方式安装。我们推荐使用二进制方式,虽然官方未正式支持,但社区已有解决方案。首先添加源和密钥:
sudo sh -c "echo "deb http://packages.ros.org/ros/ubuntu $(lsb_release -sc) main" > /etc/apt/sources.list.d/ros-latest.list"curl -s https://raw.githubusercontent.com/ros/rosdistro/master/ros.asc | sudo apt-key add -sudo apt update 然后安装ROS1 Noetic基础包:
sudo apt install ros-noetic-desktop 安装完成后,配置环境变量:
echo "source /opt/ros/noetic/setup.bash" >> ~/.bashrcsource ~/.bashrc ROS2 Humble是Ubuntu 22.04的原生版本,安装相对简单。首先设置locale:
sudo apt update && sudo apt install localessudo locale-gen en_US en_US.UTF-8sudo update-locale LC_ALL=en_US.UTF-8 LANG=en_US.UTF-8export LANG=en_US.UTF-8 添加ROS2源:
sudo apt install software-properties-commonsudo add-apt-repository universesudo apt update && sudo apt install curl -ysudo curl -sSL https://raw.githubusercontent.com/ros/rosdistro/master/ros.key -o /usr/share/keyrings/ros-archive-keyring.gpgecho "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/ros-archive-keyring.gpg] http://packages.ros.org/ros2/ubuntu $(. /etc/os-release && echo $UBUNTU_CODENAME) main" | sudo tee /etc/apt/sources.list.d/ros2.list > /dev/nullsudo apt update 安装ROS2 Humble:
sudo apt install ros-humble-desktop 安装完成后,配置环境变量:
echo "source /opt/ros/humble/setup.bash" >> ~/.bashrcsource ~/.bashrc 由于ROS1和ROS2的环境变量会冲突,我们需要分别加载。可以通过编写脚本来切换。创建一个脚本文件,例如switch_ros.sh:
#!/bin/bash# 切换到ROS1 Noeticfunction set_ros1 { source /opt/ros/noetic/setup.bash echo "Switched to ROS1 Noetic"}# 切换到ROS2 Humblefunction set_ros2 { source /opt/ros/humble/setup.bash echo "Switched to ROS2 Humble"}# 默认不自动加载,需要手动执行函数 将此脚本添加到~/.bashrc末尾,然后每次需要切换时运行set_ros1或set_ros2。注意不要同时加载两个环境。
打开新终端,运行set_ros1,然后启动ROS1核心:
roscore 再开一个终端,运行set_ros1,然后运行rostopic list,应该能看到话题列表。
验证ROS2 Humble:新终端运行set_ros2,然后运行C++例子:
ros2 run demo_nodes_cpp talker 另一个终端运行set_ros2,然后运行ros2 run demo_nodes_py listener,应该能看到通信。
sudo apt --fix-broken install,或者手动安装依赖。sudo apt update。通过以上步骤,你已经成功实现了ROS1 Noetic和ROS2 Humble在Ubuntu 22.04上的兼容安装。现在你可以同时开展ROS1和ROS2的项目了!
本文由主机测评网于2026-02-19发表在主机测评网_免费VPS_免费云服务器_免费独立服务器,如有疑问,请联系我们。
本文链接:https://vpshk.cn/20260226001.html