# Install ROS 2 on Raspberry Pi 4 (SD card image available)

**Published:** August 17, 2021
**Tags:** Embedded


---

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 vietanh.dev
- SSH + RealVNC Server for remoting
- Username: `ubuntu`, Password: `ubuntu`; Also used for SSH and VNC

You can download the image [here](https://1drv.ms/u/s!Av71xxzl6mYZgdcO5j2RYoOvlvPnJA?e=fWrXx2) and use [Etcher](https://www.balena.io/etcher/) or the official tool [rpi-imager](https://www.raspberrypi.org/software/) 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.

![](/posts-data/2021-08-17-install-ros2-pi4/rpi-imager-custom-img.jpg)

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](https://github.com/Drewsif/PiShrink). I also compressed the image with `zip`.

## 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.

### Install Ubuntu

First, because ROS 2 only officially supports Ubuntu, I choose Ubuntu as the OS for my Rasberry Pi. I use [rpi-imager](https://www.raspberrypi.org/software/) 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](/posts-data/2021-08-17-install-ros2-pi4/rpi-imager.png)

**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:

```
sudo dhclient
```

You can ping Google to confirm the connection:

```
ping google.com
```

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](https://gist.github.com/vietanhdev/c2cb96eb6cf5b9232a04dfbe8bdaba2f) to install RealVNC server on your Raspberry Pi (tested).

![My Lubuntu desktop](/posts-data/2021-08-17-install-ros2-pi4/my-desktop.jpg)

**Some notes**

- If you could not see the login screen after restarting, try pressing Ctrl+Alt+F2, log in, and install `lightdm` to replace `sddm` desktop display manager by following commands. Select `lightdm` when asked.

```
sudo apt install lightdm
sudo reboot
```

- You can find a very completed tutorial to set up Ubuntu on Raspberry Pi without a monitor [here](https://roboticsbackend.com/install-ubuntu-on-raspberry-pi-without-monitor/).

- Fix wrong screen resolution: [https://askubuntu.com/a/1237141/1122985](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 `colcon` first:

```
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 `~/.bashrc` file:

```
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.

![ROS2 Doctor](/posts-data/2021-08-17-install-ros2-pi4/ros2-doctor.jpg)

That's all! You can read [the official documentation](https://docs.ros.org/en/foxy/Installation.html) 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!

