CodeTheRobot

Ubuntu Linux for Robotics · Lesson 43 of 96

Creating ROS 2 packages

ros2 pkg create on Ubuntu, package.xml, dependencies with rosdep and building one package.

Estimated time · 45–60 min

Learning goals

What you will understand

  • Explain creating ros 2 packages 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

On Ubuntu you create a package with ros2 pkg create inside ~/ros2_ws/src. Use --build-type ament_cmake for C++ and ament_python for Python, list dependencies with --dependencies, and give it a license. The tool writes package.xml and CMakeLists.txt (or setup.py).

Keep dependencies honest: everything you include must be in package.xml so rosdep can install it on another machine. Build just the new package with colcon build --packages-select, then source install/setup.bash and confirm with ros2 pkg list.

Guided lesson

Work through these steps

  1. 1.

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

  2. 2.

    Read the creating ros 2 packages 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

cd ~/ros2_ws/src
# Packages always live in src.
ros2 pkg create --build-type ament_cmake --license Apache-2.0 robot_nodes --dependencies rclcpp std_msgs
# Create a C++ package with two dependencies.
cd ~/ros2_ws && rosdep install --from-paths src --ignore-src -y
# Make sure every dependency is installed.
colcon build --packages-select robot_nodes --symlink-install
# Build only the new package.
source install/setup.bash
# Make it visible to ROS.
ros2 pkg list | grep robot_nodes
# Confirm the package is registered.

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 Creating ROS 2 packages. Expected result: robot_nodes

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/src
# TODO: create the ament_cmake package robot_nodes depending on rclcpp and std_msgs
cd ~/ros2_ws && colcon build --packages-select robot_nodes && source install/setup.bash
ros2 pkg list | grep robot_nodes

Expected output

robot_nodes
Stuck? Show the full solution (try it yourself first)
cd ~/ros2_ws/src
ros2 pkg create --build-type ament_cmake --license Apache-2.0 robot_nodes --dependencies rclcpp std_msgs
cd ~/ros2_ws && colcon build --packages-select robot_nodes && source install/setup.bash
ros2 pkg list | grep robot_nodes

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

Create one C++ and one Python package, build both, and check that each appears in ros2 pkg list. Remove a dependency from package.xml and describe what rosdep and colcon do differently.

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.