前言:高考已经过去很多年了,如今做了程序员,哎!
正文:
网上ros2教程很多,自己也失败很多次,这里总结一下吧,欢迎指正!
ros2和ubuntu版本对照关系
- ros2 foxy 对应ubuntu20.04 发布维护日期2020.6----2023.5
- ros2 humble 对应ubuntu22.04 发布维护时间2022.5--2027.5
注意请先不要换源,先使用系统自带的源安装(比国内源满了一点点,但速度还可以,实际自己在刚装完系统换成清华源时,发现ginpg2一直安不上,原源就可以
)
sudo apt-get update
sudo apt-get install curl gnupg2 lsb-release vim
设置语言环境
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
设置软件源
curl -s https://raw.githubusercontent.com/ros/rosdistro/master/ros.asc | sudo apt-key add -
#注意古月和官网的教程使用的是国外的ros源
sudo sh -c 'echo "deb [arch=$(dpkg --print-architecture)] http://packages.ros.org/ros2/ubuntu $(lsb_release -cs) main" > /etc/apt/sources.list.d/ros2-latest.list'
#我这里用清华源,国内下载ros会相对好一些吧
sudo sh -c 'echo "deb [arch=$(dpkg --print-architecture)] http://mirror.tuna.tsinghua.edu.cn/ros2/ubuntu $(lsb_release -cs) main" > /etc/apt/sources.list.d/ros2-latest.list'
sudo apt update
此时会报错呀【修复key】
①导出原key②在更新ros2源里指定加载key的路径
root@5f27b94c113e:/home# apt-get update
Hit:1 http://packages.ros.org/ros2/ubuntu jammy InRelease
Hit:2 http://archive.ubuntu.com/ubuntu jammy InRelease
Hit:3 http://security.ubuntu.com/ubuntu jammy-security InRelease
Hit:4 http://archive.ubuntu.com/ubuntu jammy-updates InRelease
Hit:5 http://archive.ubuntu.com/ubuntu jammy-backports InRelease
Reading package lists... Done
W: http://packages.ros.org/ros2/ubuntu/dists/jammy/InRelease: Key is stored in legacy trusted.gpg keyring (/etc/apt/trusted.gpg), see the DEPRECATION section in apt-key(8) for details.
我们需要修复 Key is stored in legacy trusted.gpg keyring
这里是ros2源相关报的错误
首先查看key list,找到警告相关源的key
这里是 Open Robotics这个
root@5f27b94c113e:/# apt-key list
Warning: apt-key is deprecated. Manage keyring files in trusted.gpg.d instead (see apt-key(8)).
/etc/apt/trusted.gpg
--------------------
pub rsa4096 2019-05-30 [SC] [expires: 2025-06-01]
C1CF 6E31 E6BA DE88 68B1 72B4 F42E D6FB AB17 C654
uid [ unknown] Open Robotics
/etc/apt/trusted.gpg.d/ubuntu-keyring-2012-cdimage.gpg
------------------------------------------------------
pub rsa4096 2012-05-11 [SC]
8439 38DF 228D 22F7 B374 2BC0 D94A A3F0 EFE2 1092
uid [ unknown] Ubuntu CD Image Automatic Signing Key (2012)
/etc/apt/trusted.gpg.d/ubuntu-keyring-2018-archive.gpg
------------------------------------------------------
pub rsa4096 2018-09-17 [SC]
F6EC B376 2474 EDA9 D21B 7022 8719 20D1 991B C93C
uid [ unknown] Ubuntu Archive Automatic Signing Key (2018)
导出相应key到单独文件 注意这里的路径和名称
修改有问题源的配置文件,关联key文件位置. 源配置文件在/etc/apt/sources.list.d目录下
root@5f27b94c113e:/# apt-key export AB17C654|sudo gpg --dearmour -o /etc/apt/trusted.gpg.d/ros.gpg
Warning: apt-key is deprecated. Manage keyring files in trusted.gpg.d instead (see apt-key(8)).
改成如下:
root@5f27b94c113e:/# sudo vim /etc/apt/sources.list.d/ros2-latest.list
deb [arch=amd64 signed-by=/etc/apt/trusted.gpg.d/ros.gpg] http://mirror.tuna.tsinghua.edu.cn/ros2/ubuntu jammy main
再次更新源即可(上面主要涉及到两步操作:①导出原key②在更新ros2源里指定加载key的路径)
root@5f27b94c113e:/# apt-get update
Hit:1 http://mirror.tuna.tsinghua.edu.cn/ros2/ubuntu jammy InRelease
Hit:2 http://security.ubuntu.com/ubuntu jammy-security InRelease
Hit:3 http://archive.ubuntu.com/ubuntu jammy InRelease
Hit:4 http://archive.ubuntu.com/ubuntu jammy-updates InRelease
Hit:5 http://archive.ubuntu.com/ubuntu jammy-backports InRelease
Reading package lists... Done
sudo apt install ros-humble-desktop
等待完成即可!
将/opt/ros/humble/setup.bash 添加到 ~/.bashrc
source ~/.bashrc
测试的话就用小乌龟
ros2 run turtlesim turtlesim_node
再启动键盘控制节点
ros2 run turtlesim turtle
如果后期安装软件出现类似于以下错误,则是ubuntu软件源问题
我们换一下源,清华源 (记得备份原来的)ubuntu " 镜像站使用帮助 | 清华大学开源软件镜像站 | Tsinghua Open Source Mirror
# 默认注释了源码镜像以提高 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
# 预发布软件源,不建议启用
# deb https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-proposed main restricted universe multiverse
# deb-src https://mirrors.tuna.tsinghua.edu.cn/ubuntu/ jammy-proposed main restricted universe multiverse
如有问题请留言,看到会回复的!
附录:
如果是docker开发,则更加简单,
docker pull osrf/ros:humble-desktop
直接安装原版ros2 humble镜像(ubuntu22.04桌面版),apt update报错参考上面步骤解决!