CodeTheRobot

Ubuntu Linux for Robotics · Lesson 35 of 96

Source the ROS 2 environment

setup.bash, what sourcing changes, per-terminal scope and adding it to .bashrc.

Estimated time · 30–45 min

Learning goals

What you will understand

  • Explain source the ros 2 environment 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

Installing ROS 2 does not make its commands available yet. You must source the setup script in every terminal: source /opt/ros/lyrical/setup.bash. The folder is named after the distribution (lyrical, the release named Lyrical Luth). Sourcing sets environment variables such as ROS_DISTRO, PATH and AMENT_PREFIX_PATH that find ROS commands and libraries.

It affects only the current terminal. Putting the line in ~/.bashrc sources it in every new terminal, which is convenient but means every terminal uses this distribution, even for non-ROS work. Verify with printenv ROS_DISTRO.

Guided lesson

Work through these steps

  1. 1.

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

  2. 2.

    Read the source the ros 2 environment 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

ros2 --help
# Before sourcing: 'command not found'.
source /opt/ros/lyrical/setup.bash
# Load ROS 2 into this terminal.
printenv ROS_DISTRO
# Confirm the distribution name (lyrical).
printenv AMENT_PREFIX_PATH
# See which installations ROS will search.
ros2 --help | head -3
# Now the ros2 command works.
echo 'source /opt/ros/lyrical/setup.bash' >> ~/.bashrc
# Optional: source ROS 2 in every new terminal.

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 Source the ROS 2 environment. Expected result: lyrical

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: load the ROS 2 Lyrical environment into this terminal
printenv ROS_DISTRO

Expected output

lyrical
Stuck? Show the full solution (try it yourself first)
source /opt/ros/lyrical/setup.bash
printenv ROS_DISTRO

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

Prove that a new terminal has no ROS environment, source ROS 2 and read the variables it sets, then add the line to ~/.bashrc and open a new terminal to confirm it works.

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.