# How To Install ROS2 Humble Base On Raspberry Pi 4B (FULL INSTALL)

<figure><img src="https://1813945869-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvaPba1OWKgmXxFGX8DOE%2Fuploads%2FfPq2SHrBvvBXHLgOx8To%2Fros_humble_on_pi_img.drawio.png?alt=media&#x26;token=06d1bc51-84ac-411b-9806-9fdf0df2cb5f" alt=""><figcaption></figcaption></figure>

If you'll be building intelligent robotics applications, chances are you’ll want the power of ROS2 running on a compact, efficient platform like the Raspberry Pi 4B. This guide walks you through the complete process of setting up ROS2 Humble on on a Raspberry Pi running Ubuntu 22.04 Server — from the initial installation to configuring essential tools like colcon and rosdep. By the end, you'll be ready to develop and deploy robust robotics applications on this versatile microcomputer.

Let’s dive in and get your Pi humming with ROS2!

### Prerequisite

* First ensure you have the Ubuntu 22.04 LTS server installed on the Raspberry Pi 4B (2GB, 4GB, or 8GB. The higher, the better)
* Also, if you did headless installation, you should be able to communicate with the Pi from your Dev PC (also running Ubuntu 22.04 LTS) via SSH.&#x20;

{% hint style="info" %}
All we'll be doing in this tutorial assumes you are able to communicate with your Raspberry Pi 4B running Ubuntu 22.04 LTS server with SSH
{% endhint %}

<figure><img src="https://1813945869-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvaPba1OWKgmXxFGX8DOE%2Fuploads%2FpgcRawz7svprpCilTfoh%2Fpc-pi-ssh-comm.png?alt=media&#x26;token=c73ae09a-1197-4aa2-8db4-55523e5556ce" alt=""><figcaption></figcaption></figure>

### Installing ROS Humble Base (not Desktop)

Follow the instructions (step by step) to install ros-humble-desktop on your PC.

* Set locale<br>

  ```shellscript
  locale  # check for UTF-8
  ```

  ```shellscript
  sudo apt update && sudo apt install locales
  ```

  ```shellscript
  sudo locale-gen en_US en_US.UTF-8
  ```

  ```shellscript
  sudo update-locale LC_ALL=en_US.UTF-8 LANG=en_US.UTF-8
  ```

  ```shellscript
  export LANG=en_US.UTF-8
  ```

  ```shellscript
  locale  # verify settings
  ```

* Setup sources and required repositories<br>

  ```shellscript
  sudo apt install software-properties-common
  ```

  ```shellscript
  sudo add-apt-repository universe
  ```

  ```shellscript
  sudo apt update && sudo apt install curl -y
  ```

  ```shellscript
  export ROS_APT_SOURCE_VERSION=$(curl -s https://api.github.com/repos/ros-infrastructure/ros-apt-source/releases/latest | grep -F "tag_name" | awk -F\" '{print $4}')
  ```

  ```shellscript
  curl -L -o /tmp/ros2-apt-source.deb "https://github.com/ros-infrastructure/ros-apt-source/releases/download/${ROS_APT_SOURCE_VERSION}/ros2-apt-source_${ROS_APT_SOURCE_VERSION}.$(. /etc/os-release && echo ${UBUNTU_CODENAME:-${VERSION_CODENAME}})_all.deb"
  ```

  ```shellscript
  sudo dpkg -i /tmp/ros2-apt-source.deb
  ```

* Install all the necessary ROS2 development tools - **rosdep**, **colcon**, **vcs-tool**, etc.

  ```sh
  sudo apt update && sudo apt install ros-dev-tools
  ```

* Install ros-humble-ros-base(Bare Bones): Communication libraries, message packages, command line tools. No GUI tools.

  ```shellscript
  sudo apt update && sudo apt upgrade
  ```

  ```sh
  sudo apt install ros-humble-ros-base
  ```

* Source the ros humble environment with this command<br>

  ```sh
  source /opt/ros/humble/setup.bash
  ```

### Add ROS Setup File To .bashrc For Auto Sourcing

```shellscript
echo "source /opt/ros/humble/setup.bash" >> ~/.bashrc
```

Now the ros humble environment setup file will be automatically sourced whenever you open a new terminal.&#x20;

### Setting up colcon for Building ROS2 packages

**colcon** is a build tool required to build your ROS2 packages created in your ros workspace. You have already installed it when you ran the `sudo apt install ros-dev-tools` command previously.\
But we need to set it up for things like tab completion, etc. during builds. Run the following command in your terminal.

* Setup colcon\_cd<br>

  ```shellscript
  echo "source /usr/share/colcon_cd/function/colcon_cd.sh" >> ~/.bashrc
  ```

  ```shellscript
  echo "export _colcon_cd_root=/opt/ros/humble/" >> ~/.bashrc
  ```

* Setup colcon tab completion<br>

  ```sh
  echo "source /usr/share/colcon_argcomplete/hook/colcon-argcomplete.bash" >> ~/.bashrc
  ```

With all the above added to the `.bashrc` file, the colcon environment and functions will be automatically activated whenever you open a new terminal as well as automatic sourcing of the ros-humble environment.

{% hint style="info" %}
To learn more about colcon check the link to its documentation [here](https://docs.ros.org/en/jazzy/Tutorials/Beginner-Client-Libraries/Colcon-Tutorial.html)
{% endhint %}

you should see the following highlighted text (in the image below) at the bottom of your `.bashrc` file.  Run this command:&#x20;

```shellscript
sudo nano ~/.bashrc
```

<figure><img src="https://1813945869-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvaPba1OWKgmXxFGX8DOE%2Fuploads%2FliVvxRUMzmUueINNkH0s%2Fros_bashrc_sourcing.png?alt=media&#x26;token=3dc39e21-8737-4571-a67e-0d8e6c699d88" alt=""><figcaption></figcaption></figure>

### Intialize and Update rosdep

rosdep is a dependency management utility that can work with packages and external libraries. It is a command line utility for identifying and installing dependencies to build or install a package.

It is most often invoked before before building a package with colcon. It will basiclally download and installing the necessary dependencies a specified in the `package.xml`file so you don't have to manually install each dependencies again.

**rosdep** was already installed when you ran the `sudo apt install ros-dev-tools` command previously.

{% hint style="info" %}
To learn more about rosdep check out its documentation [here](https://docs.ros.org/en/jazzy/Tutorials/Intermediate/Rosdep.html)
{% endhint %}

Follow the instructions below to instialize and update rosdep:

```shellscript
sudo apt update
```

```shellscript
sudo rosdep init
```

```shellscript
rosdep update
```

### Optional (Install Cyclone DDS)

If you would be using ROS2 for navigation and some other high speed data transfer, install and use the **Cyclone DDS** in place of the default **Fast DDS.**

```shellscript
sudo apt install ros-humble-rmw-cyclonedds-cpp
```

```shellscript
export RMW_IMPLEMENTATION=rmw_cyclonedds_cpp
```

```shellscript
echo "export RMW_IMPLEMENTATION=rmw_cyclonedds_cpp" >> ~/.bashrc
```

***

### Creating A ROS2 Workspace

***

<figure><img src="https://1813945869-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2FvaPba1OWKgmXxFGX8DOE%2Fuploads%2FpgcRawz7svprpCilTfoh%2Fpc-pi-ssh-comm.png?alt=media&#x26;token=c73ae09a-1197-4aa2-8db4-55523e5556ce" alt=""><figcaption></figcaption></figure>

* In the root folder of your Raspberry Pi (i.e the home directory), create a **ros workspace** folder. I would be using this workspace name - `ros-ws`.<br>

  ```sh
  mkdir -p ~/ros-ws/src
  ```

* change directory into the root **ros workspace** folder

  ```sh
  cd ~/ros-ws
  ```

* build the **ros workspace** initially with colcon (while still in the root workspace folder)

  ```sh
  colcon build
  ```

  \
  you should now see three other folders - **build**, **install**, and **log** - apart from the **src** folder already created earlier. Run `ls`  to see them in terminal

* Let's setup automatic sourcing for the created workspace - `ros-ws`- in the `.bashrc` file so that it is automatically sourced whenever you open a new terminal. Run this in terminal:

  ```sh
  echo "source ~/ros-ws/install/setup.bash" >> ~/.bashrc
  ```

### Test  with helloworld package

* Open a new terminal (to auto source your ros2 environment and workspace) and cd into your ros workspace's **src** folder

  ```sh
  cd ~/ros-ws/src
  ```

* Clone this [**helloworld\_ros2**](https://github.com/samuko-things/helloworld_ros2) package in the src folder

  ```sh
  git clone https://github.com/samuko-things/helloworld_ros2.git
  ```

* Change directory into the root **ros workspace** folder<br>

  ```sh
  cd ~/ros-ws
  ```

* Optionally, run rosdep to install the neccessary package dependencies specified in the package's `package.xml`file<br>

  ```sh
  rosdep install -i --from-path src --rosdistro humble -y
  ```

* Build the package with colcon<br>

  ```sh
  colcon build --symlink-install
  ```

* Open two seperate terminals. In one of the terminals, run the c++ talker node:<br>

  ```sh
  ros2 run helloworld_cpp talker
  ```

* In the other terminal, run a python listener node:<br>

  ```sh
  ros2 run helloworld_py listener

  ```

{% hint style="info" %}
**FINALLY:** you can also setup and build the **helloworld\_ros2** package in your **DEV PC** then run talker and listener in the **DEV PC** and **RASPBERRY PI** respectively to see if both communicates with each other via ROS2
{% endhint %}
