Photo by Marília Castelli on Unsplash
I recently wanted to install ROS 2 and setup ROS Bridge on a Raspberry Pi 4B device, for a robotics project.
I couldn’t find a good guide on how to do that, and the available guides were too outdated. So, after losing hope finding a good article, I asked ChatGPT for instructions, and told him that I wanted to install ROS2 on my Raspberry PI running Raspbian OS. Then I followed all instructions given by ChatGPT and after spending hours compiling ROS2 from source and facing many errors, I gave up. I couldn’t get ROS 2 to work on my Raspberry Pi, at least using the instructions given by ChatGPT.
Anyway I’m writing this article with step by step instructions on how to install ROS 2 on a RaspberryPi, so that I can use this as a reference for my future projects.
Installing Ubuntu
To get started with the installation, you’ll need to install Ubuntu on your Raspberry Pi. (Trust me, Raspbian OS will not work for this)
For this I suggest using Ubuntu Server 24.04 (64 bit), Ubuntu desktop will also work fine, but I prefer Ubuntu Server as its somewhat lightweight. You can use RaspberryPi imager to flash an SD card with Ubuntu. Don’t forget to configure network settings and SSH, otherwise you won’t be able to access your Raspberry Pi using SSH.
Installing Prerequisites for ROS 2
In this article, I’m installing ROS 2 Jazzy Jalisco, the latest version of ROS 2 as of this writing
Get started by making sure your Ubuntu packages are up-to date
sudo apt update && sudo apt upgrade -y
Then we can install some prerequisites for ROS 2 development
sudo apt install -y software-properties-common curl tmux git
Here, tmux is not a requirement, but rather a suggestion from me. When we are logged into a remote server through ssh, terminal multiplexers like tmux allows us to simultaneously open multiple terminals using a single ssh session.
To avoid unexpected issues with ROS 2 it’s recommended to set correct locales. For this, we can use
sudo apt install locales
sudo locale-gen en_US.UTF-8
sudo update-locale LC_ALL=en_US.UTF-8 LANG=en_US.UTF-8
Installing ROS 2 Jazzy
First we need to add the necessary repositories to our system that ros2 depends on.
sudo add-apt-repository universe
Then we add ROS 2 GPG keys
sudo curl -sSL https://raw.githubusercontent.com/ros/rosdistro/master/ros.key -o /usr/share/keyrings/ros-archive-keyring.gpg
Then we can integrate ROS 2 repositories into our system
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
sudo apt update
Now it’s time to install the ros packages
sudo apt install ros-jazzy-desktop ros-dev-tools
Also it’ll be useful to install colcon, a build tool used to build and manage ROS workspaces
sudo apt install python3-colcon-common-extensions
Setting up ROS 2
Now we need to source the ros2 setup file.
source /opt/ros/jazzy/setup.bash
Since we don’t want to do this every time we use ROS, it’s a good idea to put this command in our .bashrc file
echo "source /opt/ros/jazzy/setup.bash" >> ~/.bashrc
Setting up ROS Bridge
We need ROS Bridge if we are planning to connect to our ROS 2 node from a computer and listen or subscribe to specific topics. Follow these steps if this is intended.
ROS Bridge typically uses a websocket connection. So, for this to work we need to start a websocket server on the Raspberry Pi and connect to it using the local area ip address of our Pi
To install ROS Bridge server,
sudo apt install ros-jazzy-rosbridge-server
Then we can start our ROS Bridge server using
ros2 launch rosbridge_server rosbridge_websocket_launch.xml
This will start the websocket server using the port 9090 we can access it from our network through ws://<Raspberry_PI_IP>:9090
Testing the ROS Bridge
We can connect to the ROS Bridge server through any client, and then listen to some topic, for this example I’m going to listen to /my_topic that publishes a String value
We can start our publisher using
ros2 topic pub /my_topic std_msgs/String "data: 'Hello world'" -1
Now any client listening on topic /my_topic should see the message
Here’s a demo of me using an android app to connect to the ROS bridge:
Connecting to ROS Bridge using android and listening to a Pose2D topic