The error
$ ros2 topic list
ros2: command not found
# also printed as: bash: ros2: command not found · zsh: command not found: ros2The short answer
ROS 2 installs under /opt/ros/<distro>, which is not on your PATH until you source its setup file. Run source /opt/ros/jazzy/setup.bash (with your distro name), then add that line to ~/.bashrc so every new terminal gets it. If /opt/ros does not exist, ROS 2 is not installed for your Ubuntu version.
The 30-second fix (Ubuntu, bash)
ls /opt/ros/ # which distro is installed? e.g. jazzy
source /opt/ros/jazzy/setup.bash # use the name ls printed
ros2 --help # should now list the ros2 commands
echo "source /opt/ros/jazzy/setup.bash" >> ~/.bashrc # every new terminalWhat is actually going on
When you type a command, the shell looks for a program with that name in the folders listed in the PATH variable. The ROS 2 packages install into /opt/ros/<distro> (for example /opt/ros/jazzy), and that folder is deliberately not on PATH.
That is by design. ROS 2 lets you keep several distributions, or several of your own workspaces, on one computer and choose which one a terminal uses. You choose by sourcing a setup file: source runs the script inside your current shell, which adds the ROS 2 folders to PATH and sets variables such as ROS_DISTRO and ROS_VERSION. The official tutorial puts it plainly: without sourcing the setup files, you will not be able to access ROS 2 commands.
Sourcing only changes the shell it runs in. A new terminal tab, an SSH session or the terminal inside VS Code starts clean, which is why ros2 worked yesterday and does not today.
Find your cause
Most likely first. Run the check before you change anything; stop at the first one that matches.
- 1. The setup file is not sourced in this terminal
- 2. The distro name is wrong, or nothing is installed
- 3. You are in zsh (or sh), not bash
- 4. The line is in ~/.bashrc, but this shell did not read it
- 5. Inside Docker:
sourceis not found, or the environment vanishes - 6. ROS is installed, but not the command-line tools
1. The setup file is not sourced in this terminal
You will see: It worked in another terminal or an earlier session. echo $ROS_DISTRO prints an empty line.
Why: Each new shell starts without ROS 2. This is the most common cause, and the official “Configuring environment” tutorial exists for exactly this reason.
Check
printenv | grep -i ROSFix for this terminal
source /opt/ros/jazzy/setup.bashFix for every new terminal
echo "source /opt/ros/jazzy/setup.bash" >> ~/.bashrcAfter sourcing, the check should show ROS_VERSION=2, ROS_PYTHON_VERSION=3 and ROS_DISTRO=jazzy (or your distro). Replace jazzy with humble or lyrical if that is what you installed.
2. The distro name is wrong, or nothing is installed
You will see: A line such as bash: /opt/ros/humble/setup.bash: No such file or directory appears just before the command-not-found error.
Why: Tutorials hard-code a distro name, and each distro’s Ubuntu packages are built for one Ubuntu release: Humble for Ubuntu 22.04 (Jammy), Jazzy for Ubuntu 24.04 (Noble), Lyrical for Ubuntu 26.04 (Resolute). Follow a Humble tutorial on Ubuntu 24.04 and apt has no ros-humble-* packages to install, so /opt/ros/humble never appears.
Check what is installed and which Ubuntu you run
ls /opt/ros/
grep VERSION_CODENAME /etc/os-releaseIf ls printed a folder, use that name
source /opt/ros/<name-ls-printed>/setup.bashIf /opt/ros does not exist (after adding the ROS 2 apt repository as the install guide describes)
sudo apt update
sudo apt install ros-jazzy-desktop # Ubuntu 24.04; 22.04: ros-humble-desktop; 26.04: ros-lyrical-desktopros-<distro>-desktop includes the tools, demos and RViz; ros-<distro>-ros-base is the smaller set without GUI tools. Both include the ros2 command.
3. You are in zsh (or sh), not bash
You will see: The error reads zsh: command not found: ros2, or ps -p $$ shows zsh.
Why: setup.bash is written for bash. ROS 2 ships a setup file per shell — setup.bash, setup.zsh and setup.sh — and you should source the one that matches the shell you are typing into.
Check which shell you are typing into
ps -p $$ -o comm=Fix for zsh
source /opt/ros/jazzy/setup.zsh
echo "source /opt/ros/jazzy/setup.zsh" >> ~/.zshrc4. The line is in ~/.bashrc, but this shell did not read it
You will see: You added the source line, yet the error is still there.
Why: bash reads ~/.bashrc when an interactive terminal starts, so terminals that were already open never see the new line. Login shells read ~/.bash_profile or ~/.profile; Ubuntu’s default ~/.profile pulls in ~/.bashrc, but creating a ~/.bash_profile stops that. Commands run non-interactively — ssh robot 'ros2 topic list', cron, systemd — skip the end of Ubuntu’s ~/.bashrc entirely, so a source line appended there never runs for them. A typo in the path prints a “No such file or directory” line at the top of every new terminal that is easy to miss.
Check what your startup file actually says
grep -n "opt/ros" ~/.bashrcReload it in the current terminal (or open a new one)
source ~/.bashrcKeep exactly one /opt/ros/<distro> line. Two distros sourced one after the other mix their paths, which produces stranger errors than this one.
5. Inside Docker: source is not found, or the environment vanishes
You will see: /bin/sh: 1: source: not found during docker build, or ros2: not found in a later RUN line.
Why: On Linux, Dockerfile RUN instructions use /bin/sh -c unless you say otherwise, and on Ubuntu /bin/sh is dash, which has no source command (its equivalent is .). Each RUN also starts a fresh shell, so sourcing on one line does nothing for the next.
Fix in the Dockerfile
SHELL ["/bin/bash", "-c"]
RUN source /opt/ros/jazzy/setup.bash && colcon buildThe official ros images on Docker Hub set an entrypoint, /ros_entrypoint.sh, that sources /opt/ros/$ROS_DISTRO/setup.bash before running your command — so docker run … ros2 topic list works, but RUN lines during the build are not covered by it.
6. ROS is installed, but not the command-line tools
You will see: /opt/ros/<distro> exists and is sourced, ROS_DISTRO is set, and ros2 is still missing.
Why: The ros2 command comes from the ros2cli packages. They are part of the standard ros_core set defined in REP 2001, and therefore of ros-base and desktop. A slim image built by installing individual packages (say, only ros-jazzy-rclcpp) can end up without them.
Check
ls /opt/ros/jazzy/bin/ros2Fix
sudo apt install ros-jazzy-ros-baseros2 works, but your own package is “not found”
ros2 run my_package my_node answering Package 'my_package' not found is the same problem one level up. colcon build writes your packages into the workspace’s install/ folder, and the terminal only knows about them after you source that folder’s setup file.
The official workspace tutorial adds one rule that saves hours: source the overlay in a new terminal, not the one you built in. Sourcing an overlay where you built, or building where an overlay is sourced, “may create complex issues”.
Build in one terminal, then in a NEW terminal
source /opt/ros/jazzy/setup.bash
cd ~/ros2_ws
source install/local_setup.bash
ros2 run my_package my_noderos2 starts, then crashes with Python import errors
That is a different problem: usually the wrong Python. The official troubleshooting guide is direct about it — mixing ROS 2 packages installed with apt and packages installed with conda does not work. If you use the apt binaries, make sure no conda paths are on your PATH; check ~/.bashrc for a conda initialisation block and comment it out.
If rclpy fails to import after an operating-system upgrade, the C extension may have been built for a different Python version; the guide says rebuilding the workspace may fix it.
Stop it happening again
- Put exactly one
source /opt/ros/<distro>/setup.bashline in~/.bashrc(orsetup.zshin~/.zshrc). - Install the distro that matches your Ubuntu release: Lyrical on 26.04, Jazzy on 24.04, Humble on 22.04.
- Build in one terminal, run in another, and source your workspace’s
install/setup in the second one. - In Dockerfiles, set
SHELL ["/bin/bash", "-c"]and source in the sameRUNline as the command that needs ROS. - Run
ros2 doctoronce after installing: it checks your platform, version, network and environment, and warns about likely problems.
Questions people ask
Do I have to source setup.bash every time I open a terminal?
Yes, every new shell needs it — unless the source line is in your shell startup file (~/.bashrc for bash, ~/.zshrc for zsh), which runs it for you.
What is the difference between setup.bash and local_setup.bash?
local_setup.bash adds only the packages in that workspace. setup.bash adds the workspace and the underlay it was built on top of. Sourcing /opt/ros/jazzy/setup.bash and then your workspace’s local_setup.bash gives the same result as sourcing your workspace’s setup.bash alone.
Which ROS 2 distro should I install?
The long-term-support release built for your operating system. On Ubuntu 26.04 that is Lyrical Luth (supported until May 2031); on Ubuntu 24.04, Jazzy Jalisco (until May 2029); on Ubuntu 22.04, Humble Hawksbill (until May 2027). Kilted Kaiju is a short-support release that ends in December 2026, so it is a poor choice for a new setup. The official distributions page has the current list.
Does the same fix work on WSL2?
Yes. Ubuntu under WSL2 uses the same packages, the same /opt/ros/<distro> path and the same ~/.bashrc, so the checks on this page apply unchanged.