Latest

Getting started with ROS 2: installing Jazzy on Ubuntu, understanding nodes and topics and moving a simulated robot in Python

ROS 2 is the system universities and companies use to program robots. Which version to install, how to set it up on Ubuntu or on Windows with WSL2, the concepts of node, topic, service and action with the turtlesim turtle, and a first Python program that makes the robot move.

By 4 min read
Getting started with ROS 2

ROS (Robot Operating System) is not an operating system but a set of libraries and tools that solve problems common to almost every robot: getting sensors, motors and algorithms to talk, recording data, visualising what the robot “sees”, simulating before trying on hardware. ROS 2 is the current version, rewritten to be more reliable and suitable for industry too. This guide takes you from installation to a first program that moves a simulated robot. For the general concepts, start with how a robot sees.

Which version to install

ROS 2 comes in named “distributions”, one each spring. Long-term support (LTS) releases last five years. To start, pick an LTS paired with its Ubuntu version:

Distribution Ubuntu Support
Humble Hawksbill (LTS) 22.04 Until May 2027
Jazzy Jalisco (LTS) 24.04 Until May 2029

This guide uses Jazzy. Before installing, check the distributions page in case a newer LTS has come out: the commands stay almost identical, only the name changes.

Where to install it

  • Ubuntu 24.04, on a dedicated PC or dual boot: the simplest route with the fewest surprises.
  • Windows 11 with WSL2: install Ubuntu 24.04 from the Microsoft Store and work inside it. Simulation windows work thanks to WSL’s graphics support.
  • A virtual machine: it works, but 3D simulation can be slow.

Installing on Ubuntu 24.04

The full procedure is in the official documentation, which explains how to add the ROS 2 repository and its key. Once the repository is added:

sudo apt update
sudo apt install ros-jazzy-desktop
echo "source /opt/ros/jazzy/setup.bash" >> ~/.bashrc
source ~/.bashrc
ros2 --help

The ros-jazzy-desktop package includes the graphical tools and examples used below.

The four basic concepts, with the turtle

ROS 2 includes turtlesim, a minimal simulator with a turtle moving around a screen. It looks like a game, but it uses exactly the same mechanisms as a real robot.

  1. In one terminal start the simulator: ros2 run turtlesim turtlesim_node
  2. In a second terminal start keyboard control: ros2 run turtlesim turtle_teleop_key and move the turtle with the arrow keys.
  3. In a third terminal see what is happening: ros2 node list, ros2 topic list, ros2 topic echo /turtle1/pose.
  4. To see the connection diagram: rqt_graph.
Concept What it is In the example
Node A program that does one thing turtlesim_node (the robot) and teleop_turtle (the keyboard)
Topic A channel where one node publishes messages and others receive them, continuously /turtle1/cmd_vel (velocity commands), /turtle1/pose (position)
Service A one-off request with a reply /spawn creates a new turtle, /clear clears the screen
Action A long task with progress feedback that can be cancelled /turtle1/rotate_absolute rotates to an angle

Try a service by hand: ros2 service call /spawn turtlesim/srv/Spawn "{x: 2.0, y: 2.0, theta: 0.0, name: 'second'}"

A first node in Python

This node publishes a velocity message to the /turtle1/cmd_vel topic ten times a second: the turtle moves forward and turns, drawing a circle.

import rclpy
from rclpy.node import Node
from geometry_msgs.msg import Twist

class Circle(Node):
    def __init__(self):
        super().__init__('circle')
        self.pub = self.create_publisher(Twist, '/turtle1/cmd_vel', 10)
        self.create_timer(0.1, self.send)

    def send(self):
        msg = Twist()
        msg.linear.x = 2.0     # forward, in m/s
        msg.angular.z = 1.0    # rotation, in rad/s
        self.pub.publish(msg)

def main():
    rclpy.init()
    rclpy.spin(Circle())

if __name__ == '__main__':
    main()

Save it as circle.py, run python3 circle.py with turtlesim running and stop it with Ctrl+C. For real projects the code goes into a package created with ros2 pkg create and built with colcon build: the official documentation has a step-by-step tutorial.

Next step: a 3D simulated robot

  • Gazebo is the physics simulator used with ROS 2: robots, sensors, gravity and obstacles. For Jazzy the paired version is Gazebo Harmonic.
  • TurtleBot and many other educational robots have ready-made simulation models: you can try mapping and autonomous navigation without buying anything.
  • RViz shows sensor data in 3D, such as lidar.
  • Nav2 is the navigation stack: given a point on the map, the robot plans a route and avoids obstacles.

Tips for not getting stuck

  • Open every new terminal with the environment loaded (that is what the source line in .bashrc is for).
  • If two PCs on the same network see each other’s nodes when they shouldn’t, set a different ROS_DOMAIN_ID on each.
  • Follow the documentation’s tutorials in order: they are written for beginners.
  • Before connecting a real robot, test everything in simulation and always provide a physical emergency stop.

Sources