Ubuntu Linux for Robotics · Lesson 37 of 96
Build with colcon
colcon build, options, reading the output and running your first executable.
Estimated time · 45–60 min
Learning goals
What you will understand
- Explain build with colcon in your own words
- Use it in a small workstation example
- Verify the result instead of assuming it worked
Understand the idea first
The mental model
colcon build compiles every package in src in dependency order, installing the result into install/. Useful options: --symlink-install (edits to Python and config files apply without rebuilding), --packages-select name (build one package), --packages-up-to name, and --cmake-args -DCMAKE_BUILD_TYPE=Release for a faster build.
After building you must source the workspace (source install/setup.bash) so the shell can find the new executables. Read the summary at the end: 'Summary: N packages finished' means success. Warnings are worth fixing; the first error is usually the only one that matters.
Guided lesson
Work through these steps
- 1.
Open a terminal and record the current state before changing anything.
- 2.
Read the build with colcon commands and predict what each one does.
- 3.
Run the commands one at a time and compare each output with your prediction.
- 4.
Write the commands that worked in a setup note you can repeat later.
Worked example
Read it before you run it
cd ~/ros2_ws
# Always build from the workspace root.
source /opt/ros/lyrical/setup.bash
# The underlay must be sourced before building.
colcon build --symlink-install
# Build all packages.
colcon build --packages-select demo_nodes_cpp
# Rebuild only one package.
source install/setup.bash
# Make the built packages visible in this terminal.
ros2 run demo_nodes_cpp talker
# Run an executable from the workspace.What the example is doing
- 1.Lines without sudo only inspect or change files you own; sudo authorizes a system-wide change.
- 2.Read command output before moving to the next line. Stop if names, versions, disks, or paths differ from the lesson.
- 3.Comments beginning with # explain safety checks and are not executed by the shell.
Watch it run · Build and run
See what this code does
The terminal types this lesson's practice solution and runs it. The scene shows the kind of robot behaviour that code drives. It plays by itself and loops.
Demo for Build with colcon. Expected result: Summary: N packages finished
Practice code
Now type it yourself
Open a terminal, run the starter line by line (or save it as practice.sh and run bash practice.sh), and replace every TODO with your own command.
cd ~/ros2_ws
source /opt/ros/lyrical/setup.bash
# TODO: build the workspace with colcon, using symlink installExpected output
Summary: N packages finishedStuck? Show the full solution (try it yourself first)
cd ~/ros2_ws
source /opt/ros/lyrical/setup.bash
colcon build --symlink-installExpected result
How you know it worked
- Each command completes without an unexplained error.
- Your notes contain the command, the important output, and what it proves.
Practical exercise
Apply the lesson
Build the workspace, read the build summary, break one C++ file on purpose to see the error, fix it, rebuild only that package with --packages-select, and run the result.
Before you continue
- You recorded the starting state
- Every command and output is understood
- The change can be repeated from your notes
If something goes wrong
Check ownership and permissions first. Do not add sudo unless the command changes a documented system location.
Check spelling, Ubuntu version, enabled repositories, and whether the previous installation command succeeded.