2. Installing Ubuntu and ROS 2
Match the versions first
Danger
Each ROS 2 release supports exactly one Ubuntu version. Install the wrong Ubuntu and you cannot install the ROS 2 you want — you have to reinstall the whole OS. Check this before you start.
ROS 2 |
Ubuntu |
End of life |
|---|---|---|
Humble ← this guide |
22.04 |
May 2027 |
Jazzy |
24.04 |
May 2029 |
Humble is chosen because the mobile-robot packages (Nav2, slam_toolbox, LiDAR
drivers) are the most stable and have the most examples.
Install Ubuntu 22.04
Use Ubuntu Desktop 22.04 LTS, not Server — RViz needs a display.
Download from releases.ubuntu.com/22.04
Tip
Install on real hardware, not VirtualBox or VMware, if you intend to connect real devices. Virtual machines commonly have trouble with USB ports and with clock drift, and clock drift corrupts odometry.
Install ROS 2 Humble
Run these in order, letting each block finish.
① Enable the repositories Ubuntu needs
sudo apt update && sudo apt install -y software-properties-common curl
sudo add-apt-repository universe
② Add the ROS 2 key and source
sudo curl -sSL https://raw.githubusercontent.com/ros/rosdistro/master/ros.key \
-o /usr/share/keyrings/ros-archive-keyring.gpg
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
sudo apt update
sudo apt install -y ros-humble-desktop
ros-humble-desktop includes RViz and the demo nodes. It takes a while and about
3 GB.
④ Build tools
sudo apt install -y python3-colcon-common-extensions python3-rosdep
sudo rosdep init && rosdep update
Prove it works
Installing is not enough — you must source in every new terminal.
source /opt/ros/humble/setup.bash
Then test with two terminals.
Terminal 1
source /opt/ros/humble/setup.bash
ros2 run demo_nodes_cpp talker
Terminal 2
source /opt/ros/humble/setup.bash
ros2 run demo_nodes_py listener
The talker should print Publishing: 'Hello World: 1' repeatedly, and the
listener should print I heard: [Hello World: 1] right behind it.
Important
This test proves two things at once — the install is complete, and node-to-node communication works. If the talker prints but the listener is silent, fix that before going any further.
Sourcing automatically
Typing source every time gets old. Put it in ~/.bashrc:
echo "source /opt/ros/humble/setup.bash" >> ~/.bashrc
Warning
🔴 This cuts both ways. Once you do it you will forget sourcing exists. The
day you use another machine that is not set up, you get ros2: command not found
and wonder why that machine is broken — when you simply have not sourced.
.bashrc types it for you. It does not remove the requirement.
Common problems
Symptom |
Cause |
|---|---|
|
Not sourced: |
|
Ubuntu is not 22.04 — check with |
Talker runs but listener hears nothing |
Terminal 2 not sourced, or a firewall is blocking |
Download is very slow |
Switch to a closer Ubuntu mirror |
Previous: 1. What ROS 2 is · Next: 3. Nodes, topics, messages