Complete Guide to ROS Installation and Setup

Introduction

Robot Operating System (ROS) is a flexible framework for writing robot software. It is a collection of tools, libraries, and conventions that aim to simplify the task of creating complex and robust robot behavior across a wide variety of robotic platforms. In this guide, we’ll walk through the installation process for both ROS 1 and ROS 2, the two major versions currently in use.

ROS 1 Installation (Noetic - for Ubuntu 20.04)

Setting Up the Repository

To begin with, you need to configure your system to accept software from the ROS repositories:

sudo sh -c 'echo "deb http://packages.ros.org/ros/ubuntu $(lsb_release -sc) main" > /etc/apt/sources.list.d/ros-latest.list'

Setting Up Keys

Next, you need to set up the necessary keys:

sudo apt install curl -y
curl -s https://raw.githubusercontent.com/ros/rosdistro/master/ros.asc | sudo apt-key add -

Updating and Installing ROS

Now, update your package lists and install ROS:

sudo apt update
sudo apt install ros-noetic-desktop-full -y

If you prefer a smaller installation, you can choose from these options:

  • ros-noetic-desktop: GUI tools without simulation
  • ros-noetic-ros-base: Base packages only, no GUI tools

Environment Setup

For the changes to take effect in your current terminal and all future terminal sessions, add the following to your shell configuration:

For Bash users:

echo "source /opt/ros/noetic/setup.bash" >> ~/.bashrc
source ~/.bashrc

For Zsh users:

echo "source /opt/ros/noetic/setup.zsh" >> ~/.zshrc
source ~/.zshrc

Installing Dependencies for Building Packages

Install the necessary build tools and dependencies:

sudo apt install python3-rosdep python3-rosinstall python3-rosinstall-generator python3-wstool build-essential -y

Initializing rosdep

Initialize rosdep, which is used for installing system dependencies:

sudo rosdep init
rosdep update

ROS 2 Installation (Humble - for Ubuntu 22.04)

Locale Configuration

ROS 2 requires a UTF-8 locale. Set this up with:

locale  # Check current settings
sudo apt update && sudo apt install locales -y
sudo locale-gen en_US en_US.UTF-8
sudo update-locale LC_ALL=en_US.UTF-8 LANG=en_US.UTF-8
export LANG=en_US.UTF-8

Setting Up Sources

Add the Universe repository:

sudo apt install software-properties-common -y
sudo add-apt-repository universe

Adding the ROS 2 GPG Key

sudo apt update && sudo apt install curl -y
sudo curl -sSL https://raw.githubusercontent.com/ros/rosdistro/master/ros.key -o /usr/share/keyrings/ros-archive-keyring.gpg

Adding the Repository to Your Sources List

echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/ros-archive-keyring.gpg] http://packages.ros.org/ros2/ubuntu $(. /etc/os-release && echo $UBUNTU_CODENAME) main" | sudo tee /etc/apt/sources.list.d/ros2.list > /dev/null

Installing ROS 2

Update your package lists and install ROS 2:

sudo apt update
sudo apt install ros-humble-desktop -y

For a minimal installation (CLI tools only):

sudo apt install ros-humble-ros-base -y

Install development tools:

sudo apt install ros-dev-tools -y

Environment Setup

For Bash users:

echo "source /opt/ros/humble/setup.bash" >> ~/.bashrc
source ~/.bashrc

For Zsh users:

echo "source /opt/ros/humble/setup.zsh" >> ~/.zshrc
source ~/.zshrc

Creating a Workspace

ROS 1 Workspace

Create a catkin workspace, which is a directory where you’ll develop and build your ROS packages:

mkdir -p ~/catkin_ws/src
cd ~/catkin_ws
catkin_make
echo "source ~/catkin_ws/devel/setup.bash" >> ~/.bashrc
source ~/.bashrc

ROS 2 Workspace

Create a workspace using colcon:

mkdir -p ~/ros2_ws/src
cd ~/ros2_ws
colcon build
echo "source ~/ros2_ws/install/setup.bash" >> ~/.bashrc
source ~/.bashrc

Creating Your First Package

ROS 1 Package Creation

cd ~/catkin_ws/src
catkin_create_pkg my_package std_msgs rospy roscpp
cd ~/catkin_ws
catkin_make

ROS 2 Package Creation

For a Python package:

cd ~/ros2_ws/src
ros2 pkg create --build-type ament_python my_package

For a C++ package:

cd ~/ros2_ws/src
ros2 pkg create --build-type ament_cmake my_package

Build the package:

cd ~/ros2_ws
colcon build --packages-select my_package

Testing ROS Commands

Basic ROS 1 Commands

Start the ROS master:

roscore

In a new terminal, run a simple publisher node:

rosrun rospy_tutorials talker

In another terminal, run a subscriber node:

rosrun rospy_tutorials listener

Check the list of running nodes:

rosnode list

Check the list of active topics:

rostopic list

Basic ROS 2 Commands

Run a publisher node (no separate master needed):

ros2 run demo_nodes_cpp talker

In a new terminal, run a subscriber node:

ros2 run demo_nodes_cpp listener

List active nodes:

ros2 node list

List active topics:

ros2 topic list

Useful Tools

RViz (Visualization Tool)

ROS 1:

rosrun rviz rviz

ROS 2:

ros2 run rviz2 rviz2

rqt (GUI Tools Collection)

ROS 1:

rqt

ROS 2:

ros2 run rqt rqt

Troubleshooting Common Issues

ROS 1 Common Issues

  1. ‘roscore not found’ error:
    source /opt/ros/noetic/setup.bash
    
  2. Package not found errors:
    source ~/catkin_ws/devel/setup.bash
    
  3. rosdep errors:
    sudo rosdep init
    rosdep update
    

ROS 2 Common Issues

  1. Environment issues:
    source /opt/ros/humble/setup.bash
    
  2. colcon build errors:
    sudo apt install python3-colcon-common-extensions
    
  3. Package dependency errors:
    rosdep install --from-paths src --ignore-src -r -y
    

Managing Multiple ROS Versions

If you have multiple ROS versions installed on the same system, you can create switching scripts:

# ros1.sh
#!/bin/bash
source /opt/ros/noetic/setup.bash
source ~/catkin_ws/devel/setup.bash
export ROS_MASTER_URI=http://localhost:11311
export ROS_VERSION=1
echo "ROS 1 Activated"
# ros2.sh
#!/bin/bash
source /opt/ros/humble/setup.bash
source ~/ros2_ws/install/setup.bash
export ROS_VERSION=2
echo "ROS 2 Activated"

Make the scripts executable:

chmod +x ros1.sh ros2.sh

Use them by sourcing:

source ~/ros1.sh  # Activate ROS 1
source ~/ros2.sh  # Activate ROS 2

Additional Resources

One-line Installation Commands for Quick Setup

For those who want a quick setup, here are one-line commands that combine multiple installation steps:

ROS 1 (Noetic) Complete Setup

sudo sh -c 'echo "deb http://packages.ros.org/ros/ubuntu $(lsb_release -sc) main" > /etc/apt/sources.list.d/ros-latest.list' && sudo apt install curl -y && curl -s https://raw.githubusercontent.com/ros/rosdistro/master/ros.asc | sudo apt-key add - && sudo apt update && sudo apt install ros-noetic-desktop-full -y && echo "source /opt/ros/noetic/setup.bash" >> ~/.bashrc && source ~/.bashrc && sudo apt install python3-rosdep python3-rosinstall python3-rosinstall-generator python3-wstool build-essential -y && sudo rosdep init && rosdep update && mkdir -p ~/catkin_ws/src && cd ~/catkin_ws && catkin_make && echo "source ~/catkin_ws/devel/setup.bash" >> ~/.bashrc && source ~/.bashrc

ROS 2 (Humble) Complete Setup

sudo apt update && sudo apt install locales -y && sudo locale-gen en_US en_US.UTF-8 && sudo update-locale LC_ALL=en_US.UTF-8 LANG=en_US.UTF-8 && export LANG=en_US.UTF-8 && sudo apt install software-properties-common -y && sudo add-apt-repository universe && sudo apt update && sudo apt install curl -y && sudo curl -sSL https://raw.githubusercontent.com/ros/rosdistro/master/ros.key -o /usr/share/keyrings/ros-archive-keyring.gpg && echo "deb [arch=$(dpkg --print-architecture) signed-by=/usr/share/keyrings/ros-archive-keyring.gpg] http://packages.ros.org/ros2/ubuntu $(. /etc/os-release && echo $UBUNTU_CODENAME) main" | sudo tee /etc/apt/sources.list.d/ros2.list > /dev/null && sudo apt update && sudo apt install ros-humble-desktop -y && sudo apt install ros-dev-tools -y && echo "source /opt/ros/humble/setup.bash" >> ~/.bashrc && source ~/.bashrc && mkdir -p ~/ros2_ws/src && cd ~/ros2_ws && colcon build && echo "source ~/ros2_ws/install/setup.bash" >> ~/.bashrc && source ~/.bashrc

These commands are particularly useful for scripting or setting up new development environments quickly.




Enjoy Reading This Article?

Here are some more articles you might like to read next:

  • ROS Installation and Setup Guide for NVIDIA Jetson Nano
  • Complete Guide to LangChain Installation and Setup
  • Docker Ubuntu Setup Guide
  • Isaac Sim Installation Guide for Ubuntu
  • Isaac Gym Installation Guide for Ubuntu