Ubuntu Linux for Robotics · Lesson 36 of 96
Create a ROS 2 workspace
mkdir -p ~/ros2_ws/src, cloning packages into src and the workspace layout.
Estimated time · 30–45 min
Learning goals
What you will understand
- Explain create a ros 2 workspace 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
A ROS 2 workspace is a folder with a src directory that contains packages. Create it with mkdir -p ~/ros2_ws/src, then place packages in src (clone them with git or create your own). The folders build, install and log appear at the workspace root after the first build.
Keep source code only in src and never commit the other folders. You can have several workspaces (for example one for your own code, one for third-party code); they are combined as overlays. Put the workspace inside your home directory and avoid spaces in its path.
Guided lesson
Work through these steps
- 1.
Open a terminal and record the current state before changing anything.
- 2.
Read the create a ros 2 workspace commands and predict what each one does.
- 3.
Run the commands one at a time and compare each output with your prediction.
- 4.
Write the commands that worked in a setup note you can repeat later.
Worked example
Read it before you run it
mkdir -p ~/ros2_ws/src
# Create the workspace and its source folder.
cd ~/ros2_ws/src
# Go into src.
git clone https://github.com/ros2/demos.git -b lyrical
# Clone an example repository into src (use the branch matching your distribution).
cd ~/ros2_ws
# Go back to the workspace root, where colcon must be run.
rosdep install --from-paths src --ignore-src -y
# Install the dependencies of everything in src.
ls
# At this point only src exists; the build folders appear after building.What the example is doing
- 1.Lines without sudo only inspect or change files you own; sudo authorizes a system-wide change.
- 2.Read command output before moving to the next line. Stop if names, versions, disks, or paths differ from the lesson.
- 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.
Demo for Create a ROS 2 workspace. Expected result: src
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.
# TODO: create the folder ~/ros2_ws/src (with parents)
ls ~/ros2_wsExpected output
srcStuck? Show the full solution (try it yourself first)
mkdir -p ~/ros2_ws/src
ls ~/ros2_wsExpected 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 ~/ros2_ws/src, clone the demos repository, install dependencies with rosdep and list the workspace. Explain why you run colcon from the workspace root and not from src.
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
Check ownership and permissions first. Do not add sudo unless the command changes a documented system location.
Check spelling, Ubuntu version, enabled repositories, and whether the previous installation command succeeded.