Creating A Workspace¶
Create a ROS 2 workspace and a Python package, then build the workspace successfully.
Concept¶
A workspace is a folder that contains all your ROS 2 packages and build artifacts. A package is a self-contained module inside a workspace — it holds your nodes, launch files, and configuration.
src/— your source packages go herebuild/— colcon build artifacts (auto-generated)install/— final installed files; sourced to activate packageslog/— build logs
Steps¶
1. Source ROS 2
Do this in every new terminal, or add to ~/.bashrc:
source /opt/ros/humble/setup.bash2. Create the workspace
mkdir -p ~/ros2_ws/src
cd ~/ros2_ws3. Create the package
cd ~/ros2_ws/src
ros2 pkg create my_package --build-type ament_python --dependencies rclpyThis creates the folder structure:
my_package/
├── package.xml
├── setup.cfg
├── setup.py
└── my_package/
└── __init__.py4. Build the workspace
cd ~/ros2_ws
colcon build5. Source the workspace
Run this after every build, in every terminal that needs the package:
source ~/ros2_ws/install/setup.bash6. Verify
ros2 pkg list | grep my_packageExpected output:
my_package