Environment Setup
This guide shows how to set up a ROS 2 development environment on Ubuntu.
Recommended Version
ROS 2 currently maintains two stable releases:
- Humble
- Jazzy
- Ubuntu 22.04
- Ubuntu 24.04
💡 Recommendation: Beginners should choose Humble (mature ecosystem); choose Jazzy if you want to learn the latest features.
System Environment
Make sure the system Locale supports UTF-8 to avoid encoding issues during installation.
- Check Locale
- Set Locale
localeOn a Chinese system, the output looks like zh_CN.UTF-8.
If the system Locale does not support UTF-8, set it manually.
sudo apt update && sudo apt install locales -ysudo locale-gen en_US.UTF-8sudo update-locale LC_ALL=en_US.UTF-8 LANG=en_US.UTF-8export LANG=en_US.UTF-8Enable Software Sources
Enable the Universe Repository
software-properties-common provides the add-apt-repository tool; the universe repository contains community-maintained open-source software that some ROS 2 dependencies rely on.
sudo apt update && sudo apt install software-properties-common -ysudo add-apt-repository universeAdd the ROS 2 Source
It is recommended to use the official ROS GPG key and apt source directly (the current official approach). This does not depend on downloading a deb from GitHub Releases, making it more reliable on restricted networks or ARM platforms.
sudo apt update && sudo apt install curl gnupg lsb-release -ysudo curl -sSL https://raw.githubusercontent.com/ros/rosdistro/master/ros.key -o /tmp/ros.keysudo install -m 0755 -d /etc/apt/keyringssudo mv /tmp/ros.key /etc/apt/keyrings/ros-archive-keyring.gpgsudo chmod a+r /etc/apt/keyrings/ros-archive-keyring.gpgecho "deb [arch=$(dpkg --print-architecture) signed-by=/etc/apt/keyrings/ros-archive-keyring.gpg] http://packages.ros.org/ros2/ubuntu $(. /etc/os-release && echo ${UBUNTU_CODENAME:-${VERSION_CODENAME}}) main" | sudo tee /etc/apt/sources.list.d/ros2.list > /dev/nullsudo apt update⚠️ Alternative (ros2-apt-source deb): If your network can access GitHub Releases normally, you may use the older official method of downloading the
ros2-apt-sourcedeb to configure the source automatically. Note that when extracting the version, do not use theawk -F'"'style (it can fail under nested quoting); use the more robustgrep -oPinstead:sudo apt update && sudo apt install curl -yexport ROS_APT_SOURCE_VERSION=$(curl -s https://api.github.com/repos/ros-infrastructure/ros-apt-source/releases/latest | grep -oP '"tag_name":\s*"\K[^"]+')curl -L -o /tmp/ros2-apt-source.deb "https://github.com/ros-infrastructure/ros-apt-source/releases/download/${ROS_APT_SOURCE_VERSION}/ros2-apt-source_${ROS_APT_SOURCE_VERSION}.$(. /etc/os-release && echo ${UBUNTU_CODENAME:-${VERSION_CODENAME}})_all.deb"sudo dpkg -i /tmp/ros2-apt-source.debsudo apt updateIf
ROS_APT_SOURCE_VERSIONis empty (GitHub API rate limit or no network), visit ROS Apt Source Releases to get a version and set it explicitly, e.g.export ROS_APT_SOURCE_VERSION="1.2.0".
Install Development Tools
ros-dev-tools includes essential tools such as colcon, rosdep, and catkin.
sudo apt install ros-dev-tools -yInstall ROS 2
Install the Base Version
Includes communication libraries (rclpy, rclcpp), message packages, and CLI tools, without GUI. Suitable for servers / headless scenarios.
- Humble
- Jazzy
sudo apt install ros-humble-ros-base -ysudo apt install ros-jazzy-ros-base -yInstall the Desktop Version
Includes the ROS core + RViz visualization + demos + tutorials. Suitable for desktop / development users.
- Humble
- Jazzy
sudo apt install ros-humble-desktop -ysudo apt install ros-jazzy-desktop -yConfigure Environment Variables
Load the ROS 2 workspace into the current shell environment — setting key variables such as ROS_DISTRO, ROS_PACKAGE_PATH, PATH, and LD_LIBRARY_PATH.
- Humble
- Jazzy
source /opt/ros/humble/setup.bashsource /opt/ros/jazzy/setup.bash💡 If you don't want to
sourcemanually in every new terminal, add it to~/.bashrc(pick the line matching your distro):echo "source /opt/ros/jazzy/setup.bash" >> ~/.bashrcOr make it apply to all users (root required):
echo "source /opt/ros/jazzy/setup.bash" | sudo tee /etc/profile.d/ros2.sh > /dev/null
Verify the ROS 2 Environment
📦
ros-baseonly includes the communication libraries and CLI tools — it does not ship the demo nodesdemo_nodes_cpp/demo_nodes_py. Install the demo packages before running the talker/listener verification (pick the line matching your distro):
- Humble
- Jazzy
sudo apt install ros-humble-demo-nodes-cpp ros-humble-demo-nodes-py -ysudo apt install ros-jazzy-demo-nodes-cpp ros-jazzy-demo-nodes-py -yMethod 1: Two-Terminal Verification (recommended for understanding)
Open two terminals and run:
- Terminal 1 (talker, C++ publisher)
- Humble
- Jazzy
source /opt/ros/humble/setup.bashros2 run demo_nodes_cpp talkersource /opt/ros/jazzy/setup.bashros2 run demo_nodes_cpp talker- Terminal 2 (listener, Python subscriber)
- Humble
- Jazzy
source /opt/ros/humble/setup.bashros2 run demo_nodes_py listenersource /opt/ros/jazzy/setup.bashros2 run demo_nodes_py listenerMethod 2: Single-Terminal One-Shot Verification (great for scripts/containers)
No need for two terminals — start talker and listener in one command:
- Humble
- Jazzy
source /opt/ros/humble/setup.bashros2 run demo_nodes_cpp talker & sleep 2; ros2 run demo_nodes_py listener & sleep 4; kill %1 %2source /opt/ros/jazzy/setup.bashros2 run demo_nodes_cpp talker & sleep 2; ros2 run demo_nodes_py listener & sleep 4; kill %1 %2If the listener keeps printing I heard: [Hello World: N], it means ROS 2's publish/subscribe, topic discovery, and the C++/Python client libraries are all working properly.