Skip to main content

Section 9.4 The ROS 2 Workspace Environment

A ROS 2 Workspace is a dedicated folder structure on your filesystem where you write, build, and source custom ROS 2 packages.
The standard workspace layout follows a convention:
ros2_ws/                       <-- Workspace Root Directory
β”œβ”€β”€ src/                       <-- Source Directory (your packages go here)
β”‚   └── my_robot_package/
β”‚       β”œβ”€β”€ package.xml        <-- Package manifest (dependencies, meta-info)
β”‚       β”œβ”€β”€ setup.py           <-- Build configuration for Python packages
β”‚       └── my_robot_package/
β”‚           β”œβ”€β”€ __init__.py
β”‚           └── robot_node.py
β”œβ”€β”€ build/                     <-- Created automatically during build phase
β”œβ”€β”€ install/                   <-- Houses compiled executables & setup scripts
└── log/                       <-- Build and execution log files

Subsection 9.4.1 Build & Sourcing Workflow

  1. Write custom code in packages within ros2_ws/src/.
  2. Compile and assemble packages using colcon build at the workspace root directory (ros2_ws/).
  3. Overlay the workspace environment onto the active shell environment by sourcing the install setup file:
    source install/setup.bash
    
This step informs system environment variables (such as PATH and PYTHONPATH) where your custom nodes and message definitions live, allowing ros2 run command execution from any terminal path!

Reading Questions 9.4.2 Reading Questions

Check your understanding

1. Exercise 9.4.1: Workspace Sourcing Conceptual Check.

After creating a new ROS 2 package in ros2_ws/src/ and building it with colcon build, your terminal says ros2 run my_pkg my_node cannot be found. What step was likely skipped?
  • You forgot to restart Ubuntu.
  • Incorrect. Restarting the operating system has no effect on whether the workspace environment variables are loaded into your shell.
  • You forgot to source the workspace setup script (source install/setup.bash).
  • Correct! Without sourcing install/setup.bash, your shell’s PATH and PYTHONPATH never learn where your newly built package’s executables live, so ros2 run cannot find them.
  • You forgot to delete the log/ folder.
  • Incorrect. The log/ folder only stores build and execution logs; it has no effect on whether commands are found.
  • You didn’t run colcon build inside the src/ folder.
  • Incorrect. colcon build is meant to be run at the workspace root, not inside src/; running it from the wrong directory would produce a build error, not a missing-command error after a successful build.
You have attempted of activities on this page.