Getting Started with ROS2: Create and Set Up a Workspace

Part 5 of our “Getting Started with ROS2” Series

Sagar Kumar
Spinor
5 min readJul 7, 2024

--

Welcome, fellow readers, to our “Getting Started with ROS2” series! In this series, we aim to provide you with a comprehensive introduction to ROS 2 and will guide you through the fundamentals, its key concepts, and practical applications. If you have never used the Robot Operating System(ROS) before, even ROS 1, or if you want a practical and quick refreshment of the basics then this series is for you. Feel free to explore the links provided for a more in-depth understanding of the concepts discussed.

Prerequisites

Before we start, you must have a basic understanding of ROS2 concepts. Please refer to the following articles in our series:

  1. Getting Started with ROS2: An Introduction — Provides a foundational overview of ROS2.
  2. Getting Started with ROS2: Why ROS2? — Explains the advantages and motivations for using ROS2.
  3. Getting Started with ROS2: Install and Setup ROS2 Humble on Ubuntu 22.04(LTS) — Guides you through installing and setting up ROS2 on Ubuntu.
  4. Getting Started with ROS2: Overview of ROS2 Workspaces, Packages and Nodes

Having a solid grasp of these basics will help you follow along and understand the steps involved in creating and managing workspaces in ROS2.

A brief overview of what a workspace is in ROS2

A workspace in ROS2 is a directory where you develop and manage your ROS2 projects. It serves as a central place to store and organize your ROS2 packages, which include your nodes, libraries, configuration files, and other resources. The workspace is essential for building, testing, and deploying ROS2 applications, as it helps manage dependencies and facilitates a streamlined development workflow.

In short, we cannot save our code wherever we want; we need a bit of organization. Therefore, we have to use workspaces, which is what we share with others when we need to share our work.

Structure of a ROS2 workspace

The standard structure of a ROS2 workspace typically includes several key directories:

  • src: This directory contains the source code of your ROS2 packages. Each package is a self-contained unit of software that includes nodes, libraries, and configuration files. We will cover packages later in this series.
  • install: After building the workspace, this directory contains the installed files necessary to run your packages, including executables and other resources.
  • build: This directory holds intermediate build files generated during the compilation process.
  • log: This directory contains log files that provide information about the build and runtime processes, useful for debugging and monitoring.

Setting up colcon

colcon is a command-line tool used to build and manage multiple packages in a ROS2 workspace. It simplifies the process of compiling, testing, and organizing ROS2 projects, ensuring efficient and streamlined development workflows. With colcon, you can easily create and build a ROS 2 workspace.

If you have already installed ros-humble-desktop and ros-dev-tools, colcon should be included in those packages. You do not need to install colcon separately. You can verify its installation by running:

colcon --version

Regarding autocompletion, colcon can provide command-line autocompletion, but it requires an additional setup. Here’s how you can enable it:

Install the colcon autocompletion script:

sudo apt install python3-colcon-common-extensions

Enable autocompletion in your shell configuration:

For Bash, add the following lines to your ~/.bashrc file:

source /usr/share/colcon_argcomplete/hook/colcon-argcomplete.bash

For Zsh, add the following lines to your ~/.zshrc file:

autoload -Uz compinit
compinit
autoload -Uz bashcompinit
bashcompinit
source /usr/share/colcon_argcomplete/hook/colcon-argcomplete.bash

Reload your shell configuration:

For Bash:

source ~/.bashrc

For Zsh:

source ~/.zshrc

Creating a new Workspace

To start developing with ROS2, you need to create a workspace directory to organize your packages.

mkdir -p ~/ros2_ws/src
cd ~/ros2_ws

The first command creates a new directory named ros2_ws with a subdirectory src where your source code will reside. Replace ros2_ws with the desired name for your workspace.

NOTE: Remember, while you can name your workspace directory anything you like, the src directory must retain its name for ROS2 to function correctly.

Setting Up the Workspace

  • Navigate to the Workspace Directory: Move into the ros2_ws directory i.e. into the root directory of your ROS2 workspace.
cd ~/ros2_ws
  • Build the Packages: This command initiates the build process using colcon, compiling all packages within your workspace. It ensures that your ROS2 project is ready for testing and deployment by generating executable binaries and necessary build artifacts.
colcon build

Note: Since no packages have been created yet, running colcon build in an empty workspace containing only the src folder will not initiate any build process because there are no packages defined for compilation.

If you run colcon build in an empty ROS2 workspace that only has the src folder, here's what will happen:

  1. Initialization: colcon will recognize that there are no packages or build files (CMakeLists.txt, etc.) present in the src directory.
  2. No Packages to Build: Since there are no packages defined in the workspace, colcon will not find anything to build.
  3. Output: You will likely see a message indicating that there are no packages to build or something like this — Summary: 0 packages finished [0.59s].

Source the Workspace

Sourcing the workspace sets up your environment to recognize ROS2 packages and tools installed within it. This step ensures that commands like ros2 and your custom packages are accessible.

  • Command to Source: This command sources the setup.bash script located in the install directory of your workspace, configuring your current terminal session to use the packages and tools installed in your ROS2 workspace.
source ~/ros2_ws/install/setup.bash
  • Add to .bashrc for Convenience: To automate this process for every new terminal session, add the sourcing command to your .bashrc file:
echo "source ~/ros2_ws/install/setup.bash" >> ~/.bashrc

What’s Next?

In the upcoming articles, we will explore nodes and packages in depth, learning how to create custom packages and nodes.

If you’ve made it this far, it means you’re not just interested — you’re committed, and we’re delighted to have you here!

Your interest and engagement inspire us to create more content and share knowledge with fellow learners, the community of robotics, and tech enthusiasts who share our passion.

So, thank you for being a part of this journey with us. Stay tuned for more insightful articles, tutorials, and practical examples as we continue our exploration of ROS 2 together.

--

--

Sagar Kumar
Spinor
Editor for

Sagar is a computer vision and robotics expert with a focus on Perception & Localization | Twitter: twitter.com/sagarcadet | Linkedin: linkedin.com/in/sagark30