Installing ROS2

Setting up the language environment

Make sure your language environment supports UTF-8. If you are using a minimal environment, such as docker’s containers, then the language environment may be something like POSIX or something like that. We need to perform the following tests. However, if you are using another language that supports UTF-8, that should be fine.

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

Setting up the source

Add the ROS2 apt repository to your system by doing this:

sudo apt install software-properties-common
sudo add-apt-repository universe

Add the ROS2 installer source information:.

sudo apt update && sudo apt install curl
sudo curl -sSL https://raw.githubusercontent.com/ros/rosdistro/master/ros.key -o /usr/share/keyrings/ros-archive-keyring.gpg

Update the system source.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

Install ROS2 packages

Update apt repository.

sudo apt update

Upgrade the packages already installed on the system.

sudo apt upgrade

Recommended desktop versions to install: ROS, RViz, demos, tutorials.

sudo apt install ros-foxy-desktop

Environment Setup

Set up your environment using the file under sourcing:

source /opt/ros/foxy/setup.bash

Installation Parameter Completion

The ROS2 command line tool uses argcomplete for auto-completion. So, if you want to do auto-completion, you need to install argcomplete.

sudo apt install -y python3-argcomplete 

Test example

In a terminal A, after sourcing the setup file, run a c++ application that publishes messages.

source /opt/ros/foxy/setup.bash 
ros2 run demo_nodes_cpp talker 

In another terminal B, source the setup file and run a Python application that receives messages.

source /opt/ros/foxy/setup.bash 
ros2 run demo_nodes_py listener 

This way you can see that the talker is posting the message and the listener is printing the received message. This verifies that both the C++ and Python APIs are working.

The result is as follows:

Terminal A

neardi@LPA3588:~$ ros2 run demo_nodes_cpp talker
[INFO] [1672131270.256481314] [talker]: Publishing: 'Hello World: 1'
[INFO] [1672131271.256501645] [talker]: Publishing: 'Hello World: 2'
[INFO] [1672131272.256387812] [talker]: Publishing: 'Hello World: 3'
[INFO] [1672131273.256458682] [talker]: Publishing: 'Hello World: 4'
[INFO] [1672131274.256557592] [talker]: Publishing: 'Hello World: 5'
[INFO] [1672131275.256358218] [talker]: Publishing: 'Hello World: 6'

Terminal B

neardi@LPA3588:~$ ros2 run demo_nodes_py listener
[INFO] [1672131270.308648992] [listener]: I heard: [Hello World: 1]
[INFO] [1672131271.259757148] [listener]: I heard: [Hello World: 2]
[INFO] [1672131272.259962390] [listener]: I heard: [Hello World: 3]
[INFO] [1672131273.259896764] [listener]: I heard: [Hello World: 4]
[INFO] [1672131274.260288792] [listener]: I heard: [Hello World: 5]
[INFO] [1672131275.262437902] [listener]: I heard: [Hello World: 6]