一:设置编码
$ 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
二:添加源
$ sudo apt update && sudo apt install curl gnupg lsb-release
$ sudo curl -sSL https://raw.githubusercontent.com/ros/rosdistro/master/ros.key -o /usr/share/keyrings/ros-archive-keyring.gpg
$ echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/ros-archive-keyring.gpg] http://packages.ros.org/ros2/ubuntu $(source /etc/os-release && echo $UBUNTU_CODENAME) main" | sudo tee /etc/apt/sources.list.d/ros2.list > /dev/null
若执行倒数第二行报错,参考该解决方案,运行sudo gedit /etc/hosts
,将185.199.108.133 raw.githubusercontent.com
写入,如下如所示:
三:安装ROS2
$ sudo apt update
$ sudo apt upgrade
$ sudo apt install ros-foxy-desktop #注意不同的ubuntu版本号对应的ros-{version}-desktop中的内容也不同
四:设置环境变量
$ source /opt/ros/foxy/setup.bash
$ echo " source /opt/ros/foxy/setup.bash" >> ~/.bashrc
五:测试环节
#第一个终端
$ ros2 run demo_nodes_cpp talker
#另启一个终端
$ ros2 run demo_nodes_cpp listener
至此,大功告成!