ROS 2 Foundations · Lesson 2 of 6

Lesson 02

Install ROS 2 & Build a Workspace

Time to get ROS 2 running on your machine and create the folder where all your robot code will live: a colcon workspace with your first Python package.

Step 1 — Get a system that can run ROS 2

ROS 2 runs best on Ubuntu Linux. Pick the pairing that matches your distribution:

On Windows or Mac?

The simplest path is Docker — a way to run a ready-made Ubuntu + ROS 2 environment inside your existing OS. Install Docker Desktop, then run the official image:

docker run -it osrf/ros:humble-desktop bash

Step 2 — Install ROS 2 (Ubuntu)

ROS 2 is installed from its official apt repository. These are the key commands for Humble on Ubuntu 22.04. (Always cross-check the current official docs at docs.ros.org — versions change.)

Terminal

# 1. Enable the Ubuntu Universe repository
sudo apt update && sudo apt install -y software-properties-common
sudo add-apt-repository universe

# 2. Add the ROS 2 apt key and repository
sudo apt install -y curl
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

# 3. Install ROS 2 (desktop includes RViz and demos)
sudo apt update
sudo apt install -y ros-humble-desktop

# 4. Install the build tools you'll use to compile packages
sudo apt install -y ros-dev-tools python3-colcon-common-extensions

Step 3 — "Source" the environment

Installing ROS 2 isn't enough — each terminal needs to source ROS 2's setup file so that commands like ros2 become available. Sourcing simply loads a set of environment variables into your current terminal.

Terminal

# Load ROS 2 into this terminal (do this in every new terminal)
source /opt/ros/humble/setup.bash

# Prove it worked — this should print a version and path
ros2 --help
Tip — do it automatically

Typing that every time gets old. Add it to your shell startup file so every new terminal sources ROS 2 for you:

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

Step 4 — Run a demo to confirm it works

ROS 2 ships with a classic "talker/listener" demo. Open two terminals (source ROS 2 in each) and run one command in each:

Terminal 1

ros2 run demo_nodes_cpp talker

Terminal 2

ros2 run demo_nodes_py listener

The talker prints Publishing: 'Hello World: 1', and the listener prints I heard: [Hello World: 1]. You just watched two nodes communicate over a topic — the exact idea from Lesson 1, live on your machine.

Step 5 — Create your own workspace

Your own code lives in a workspace — a folder with a src/ directory inside it. You build the workspace with a tool called colcon.

Terminal

# Make the workspace and its source folder
mkdir -p ~/ros2_ws/src
cd ~/ros2_ws

# Build the (currently empty) workspace
colcon build

# You now have build/ install/ log/ folders alongside src/

Step 6 — Create your first package

Code inside a workspace is organized into packages. Let's make a Python package called my_robot that we'll add to over the next lessons.

Terminal

cd ~/ros2_ws/src
ros2 pkg create --build-type ament_python my_robot

# Build it and "source" the workspace so ROS 2 can find your package
cd ~/ros2_ws
colcon build
source install/setup.bash

# Confirm ROS 2 sees your new package
ros2 pkg list | grep my_robot

Your src/my_robot/ folder now contains:

Remember the two "source" files

source /opt/ros/humble/setup.bash gives you ROS 2 itself. source ~/ros2_ws/install/setup.bash gives you your own packages. Any terminal that runs your code needs both — and you must re-run colcon build after changing certain files (we'll flag when).

Key takeaways