Install ROS 2 on Raspberry Pi 4 (SD card image available)
ROS2 is an excellent framework for robotics applications. You can go further and embed a completed ROS 2 application stack in a small robot base with a Raspberry Pi computer. This post will tell you how to set up ROS 2 on Raspberry Pi 4 - the latest version of Pi family. The 1GB version of Pi 4 is probably too limited for both graphical Ubuntu and ROS 2, so I will use a 2GB RAM Raspberry Pi for this post. There are two installation methods I will introduce here. The first one is using my pre-setup image with Ubuntu + ROS2, and the other is setting up from scratch.
1. Quick install
This quick setup tutorial uses my image of Ubuntu + ROS 2 created on 2021/08/18. It contains:
- Ubuntu 20.04 Server + Lubuntu Desktop environment + LightDM desktop display manager
- ROS 2 Foxy Fitzroy
- Desktop background from AICurious.IO
- SSH + RealVNC Server for remoting
ubuntu; Also used for SSH and VNC
You can download the image here and use Etcher or the official tool rpi-imager to write to your SD card. If you use rpi-imager to burn the image, select to to use custom img file like in the below image.
That's all! Let me know if you are successful with my image or report any problem by commenting at the bottom of this post.
Btw, the tools I used for creating the SD card image are
dd command and PiShink. I also compressed the image with
2. Install from scratch
Installation from scratch will take more time to complete. You should go step-by-step carefully. Please let me know if any error happens by commenting at the bottom.
First, because ROS 2 only officially supports Ubuntu, I choose Ubuntu as the OS for my Rasberry Pi. I use rpi-imager to install the OS. I target ROS 2 Foxy, so I choose Ubuntu 20.04 Server for writing to the Raspberry Pi SD card.
Install Ubuntu Server 20.04 using rpi-imager
Setup the network and update software:
After flashing your OS, we will need a screen, a keyboard, and an internet connection to complete this setup. Thus, I recommend plugging in an ethernet cable. After that, type the following command to get an IP address for your Pi:
You can ping Google to confirm the connection:
Update your system before doing anything else:
sudo apt update sudo apt full-upgrade
You may receive errors while running the above commands because the automatic unattended updating system locks the software database. Just try again after a while. The update may take more than 15 minutes to complete.
Install desktop environment
Because the server version of Ubuntu 20.04 does not have a desktop environment, I have to install one myself. If you don't want to install a desktop, please skip this part. That helps to save your RAM and to increase performance. Here I'm using Lubuntu - a lightweight Ubuntu version. You may want to use
ubuntu-desktop package if you have a Rasberry Pi with more than 2GB of RAM.
sudo apt install lubuntu-desktop
After restarting, you can set up Wifi, install SSH and VNC for remoting. I recommend this script to install RealVNC server on your Raspberry Pi (tested).
My Lubuntu desktop
- If you could not see the login screen after restarting, try pressing Ctrl+Alt+F2, log in, and install
sddmdesktop display manager by following commands. Select
sudo apt install lightdm sudo reboot
You can find a very completed tutorial to set up Ubuntu on Raspberry Pi without a monitor here.
Fix wrong screen resolution: https://askubuntu.com/a/1237141/1122985.
Install ROS 2
After having the Ubuntu on your Raspberry Pi, the next step is installing ROS 2.
Install ROS base
Please copy the commands below one by one and paste them into the command line to complete each step. I think it will be much easier to do the setup over SSH. First, setup locale:
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
Setup sources to get ROS 2:
sudo apt update && sudo apt install curl gnupg2 lsb-release curl -s https://raw.githubusercontent.com/ros/rosdistro/master/ros.asc | sudo apt-key add - 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' sudo apt update
Install ROS2 core packages, including the user interface. You can use
sudo apt install ros-foxy-ros-base if you don't need to run visualization tools on your Raspberry Pi.
sudo apt install ros-foxy-desktop
Install build tools
ROS 2 uses
colcon as the default build tool. Install
sudo apt install python3-colcon-common-extensions
Auto-completion for ROS 2 commands:
sudo apt install python3-argcomplete
Setup the environment
You can have ROS 2 in your environment by running
source /opt/ros/foxy/setup.bash every time you launch a new Terminal. However, I recommend adding it into your
echo "source /opt/ros/foxy/setup.bash" >> ~/.bashrc
Congratulations on successfully setup ROS 2 for Raspberry Pi! Check the environment by opening a new Terminal and type
ros2 doctor. You should see the following screen.
That's all! You can read the official documentation to install ROS 2 Foxy on other OSes. See my next posts if you are curious about the applications I will run on my Pi. Thank you!