全文基于ROS2官方教程的实践与总结。
如果不是在一个最小环境比如docker容器中运行,那么可以不进行下面的设置(虚拟机或实机装Ubuntu22下不需要设置):
locale # check for UTF-8
sudo apt update && sudo apt install locales
sudo locale-gen en_US en_US.UTF-8
sudo update-locale LC_ALL=en_US.UTF-8 LANG=en_US.UTF-8
export LANG=en_US.UTF-8
locale # verify settings
首先我们配置国内源,这里是清华源:
# 备份
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak
# 编辑该文件
sudo vi /etc/apt/sources.list
# 注释掉之前的,添加下面的内容
# 默认注释了源码镜像以提高 apt update 速度,如有需要可自行取消注释
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-updates main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-backports main restricted universe multiverse
deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-security main restricted universe multiverse
然后执行:
sudo apt update
然后添加ROS2的apt仓库,首先需要添加GPG秘钥:
sudo apt install curl -y
sudo curl -sSL https://raw.githubusercontent.com/ros/rosdistro/master/ros.key -o /usr/share/keyrings/ros-archive-keyring.gpg
这里会报错:
Failed to connect to raw.githubusercontent.com port 443 after 25 ms: Connection refused
和安装ROS1一样,主要是raw.githubusercontent.com
地址没法在国内访问。
【解决办法】
在这个地址:https://sites.ipaddress.com/raw.githubusercontent.com/ 中查询得到四个ip,我ping第一个可以通。
然后修改/etc/hosts
文件,添加如下内容:
185.199.108.133 raw.githubusercontent.com
或者报错:
curl: (35) OpenSSL SSL_connect: Connection reset by peer in connection to raw.githubusercontent.com:443
此时发现地址是可以ping通的,但拉不下ros.key文件来,可以手动在浏览器访问https://raw.githubusercontent.com/ros/rosdistro/master/ros.key
,会自动下载ros.key
文件,将文件改名为ros-archive-keyring.gpg
放入/usr/share/keyrings/
目录即可。
然后增加仓库到sources list(实机上是在/etc/apt/sources.list.d/中添加了ros2.list文件):
echo "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/null
记住一定要先升级一下系统包,因为官方说明:
由于Ubuntu 22.04的早期更新中,在安装ROS 2之前更新systemd和udev相关的软件包是很重要的。在新安装的系统上安装ROS
2的依赖项而不进行升级可能会触发关键系统包的删除。
sudo apt update
sudo apt upgrade
执行update时,报错Failed to fetch http://packages.ros.org/ros2/ubuntu/dists/jammy/InRelease
,这是由于该网址被屏蔽了导致,可以手动修改sources.list文件改为清华源:
cd /etc/apt/sources.list.d
sudo cp ros2.list ros2.list.bak
vi ros2.list
# 将http://packages.ros.org/ros2/ubuntu修改为http://mirror.tuna.tsinghua.edu.cn/ros2/ubuntu
sudo apt update
sudo apt upgrade
安装ROS2,这里安装的是humble最新稳定版,直接装桌面版,这样可以用可视化工具:
sudo apt install ros-humble-desktop
写入环境变量
source /opt/ros/humble/setup.bash
echo "source /opt/ros/humble/setup.bash" >> ~/.bashrc
测试
ros2 run turtlesim turtlesim_node
安装完毕。