3.19. Building a Package#

To get the most out of this step, refer to the “Creating a package” guide

You can actually build multiple packages at a time by running the colcon build command in your workspace directory. If you want to build each package individually, separate them into different directories, and build each one at a time.

For our purposes, place all of the packages you want to build within the same workspace, usually src. If you are following our guide from before, it should be in your ros2_ws directory, so on Linux, navigate to it via cd ~/ros2_ws, and on Windows, navigate to it via cd \ros2_ws

To actually build your packages in Linux, run the command:

colcon build

and on Windows:

colcon build --merge-install

Note: if you have a high number of packages, this command can take a while to run.

3.20. Source your setup file#

Remember from our general installation and testing tutorial that we always have to source the ROS2 installation in order to get ROS2 to run.

On Linux, run the following command within your ros2_ws directory:

source install/local_setup.bash

And on Windows, run the following command:

call install/local_setup.bat

This will allow you to use anything within your packages.

3.21. Using your ROS2 package#

Lastly, if you want to run your ROS2 package, you can do so using the following format, where you enter the package name and node name (if applicable):

ros2 run pkg_name node_name

If you followed the tutorial in our package creation guide, the command should be as follows:

ros2 run my_package my_node

And then the executeable in your ROS2 package should run without any issues.

3.22. Review Quiz#