CodeTheRobot

Ubuntu Linux for Robotics · Lesson 38 of 96

Overlay workspaces

Underlay and overlay, sourcing order, shadowing packages and checking which one is used.

Estimated time · 30–45 min

Learning goals

What you will understand

  • Explain overlay workspaces 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

The installed ROS 2 distribution is the underlay. A workspace you build on top of it is an overlay, and you can stack more overlays. Source them in order: the underlay first, then the overlay, so packages in the overlay shadow (replace) packages with the same name below.

This lets you test a modified copy of a package without touching the system installation. If you see the old behaviour, check which copy is used with ros2 pkg prefix package_name and the AMENT_PREFIX_PATH order. Open a clean terminal to remove an overlay: sourcing does not have an undo.

Guided lesson

Work through these steps

  1. 1.

    Open a terminal and record the current state before changing anything.

  2. 2.

    Read the overlay workspaces commands and predict what each one does.

  3. 3.

    Run the commands one at a time and compare each output with your prediction.

  4. 4.

    Write the commands that worked in a setup note you can repeat later.

Worked example

Read it before you run it

source /opt/ros/lyrical/setup.bash
# Underlay: the installed distribution.
source ~/ros2_ws/install/setup.bash
# Overlay: your workspace, on top of the underlay.
ros2 pkg prefix demo_nodes_cpp
# Which copy of the package is active (system or overlay)?
echo $AMENT_PREFIX_PATH | tr ':' '\n'
# List the search path, one entry per line; earlier entries win.
ros2 pkg list | grep -c .
# Count packages visible from the combined environment.

What the example is doing

  1. 1.Lines without sudo only inspect or change files you own; sudo authorizes a system-wide change.
  2. 2.Read command output before moving to the next line. Stop if names, versions, disks, or paths differ from the lesson.
  3. 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.

sourcecompilerprogramBUILD AND RUN

Demo for Overlay workspaces. Expected result: /home/you/ros2_ws/install/demo_nodes_cpp

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.

source /opt/ros/lyrical/setup.bash
source ~/ros2_ws/install/setup.bash
# TODO: print the install prefix of demo_nodes_cpp to see which copy is active

Expected output

/home/you/ros2_ws/install/demo_nodes_cpp
Stuck? Show the full solution (try it yourself first)
source /opt/ros/lyrical/setup.bash
source ~/ros2_ws/install/setup.bash
ros2 pkg prefix demo_nodes_cpp

Expected 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

Copy demo_nodes_cpp into your workspace, change its talker message, build, and prove with ros2 pkg prefix that the overlay version shadows the system one. Then open a clean terminal and prove the original is used again.

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

A command says permission denied.

Check ownership and permissions first. Do not add sudo unless the command changes a documented system location.

A command or package is not found.

Check spelling, Ubuntu version, enabled repositories, and whether the previous installation command succeeded.