[ROS2 How-to] Install third-party Python packages using ROS2 #5

How to install ros2 depencencies using rosdep, apt-get, and pip

Written by Ruben Alves


What we are going to learn

  1. How to use rosdep
  2. How to use apt and apt-get
  3. How to create a virtual environment and install packages inside

List of resources used in this post

  1. Use this rosject: https://app.theconstructsim.com/l/4fefbbab/
  2. The Construct: https://app.theconstructsim.com/
  3. ROS2 Guide: https://docs.ros.org/en/humble/How-To-Guides/Using-Python-Packages.html
    1. https://github.com/ros/rosdistro/blob/master/rosdep/base.yaml
    2. https://github.com/ros/rosdistro/blob/master/rosdep/python.yaml
  4. ROS2 Courses –▸
    1. ROS2 Basics in 5 Days (Python): https://app.theconstructsim.com/#/Course/73
    2. ROS2 Basics in 5 Days (C++): https://app.theconstructsim.com/#/Course/61
    3. ROS2 Navigation training: https://www.theconstructsim.com/ros2-navigation-training/


Software usually builds on other software. When you are creating a ROS2 Package, for example, you have to specify the packages that the created package depends on.

In this post, we are going to talk about different methods that you can use to properly handle package dependencies in ROS2.

ROS Inside!

ROS inside

ROS inside

Before anything else, in case you want to use the logo above on your own robot or laptop, feel free to download it for free and attach it to your robot. It is really free. Find it in the link below:

ROS Inside logo


Opening the rosject

In order to learn how to install third-party Python packages using ROS2, we need to have ROS2 installed in our system, and it is also useful to have some simulations. To make your life easier, we already prepared a rosject for that: https://app.theconstructsim.com/l/4fefbbab/.

You can download the rosject on your own computer if you want to work locally, but just by copying the rosject (clicking the link), you will have a setup already prepared for you.

After the rosject has been successfully copied to your own area, you should see a Run button. Just click that button to launch the rosject (below you have a rosject example).

Learn ROS2 Parameters - Run rosject

Installing Third-Party Python packages in ROS2 – Run rosject (example of the RUN button)

After pressing the Run button, you should have the rosject loaded. Let’s now head to the next section to really get some real practice.

Installing packages via rosdep

The rosdep command helps you install external dependencies in an OS-independent manner.

In order to use rosdep, we need a terminal. Let’s open one:

Open a new Terminal

Open a new Terminal

Ok, in this rosject, we have already some packages. They were created with the following commands (you don’t need to run those commands since the package already exists):

cd ~/ros2_ws/src

ros2 pkg create --build-type ament_python my_new_ros2_numpy_pkg --dependencies rclpy std_msgs geometry_msgs python3-numpy

In the command above, we created the my_new_ros2_numpy_pkg package that depends on rclpy, std_msgs geometry_msgs, and python3-numpy.

You can see the command by running the commands below:

cd ~/ros2_ws/src



You can now see the content of the package.xml file of that package using the command below:

cat ~/ros2_ws/src/my_new_ros2_numpy_pkg/package.xml


After checking that file, let’s pay attention to the following line:


Do you remember that we specific python3-numpy as a dependency of our package when we created it?

You can also add a new dependency to an existing package by manually editing the package.xml file.

All right, now that our package is created, let’s install its dependencies using rosdep. For that, let’s run the following commands in the first terminal:

cd ~/ros2_ws/src

source ~/ros2_ws/install/setup.bash

sudo apt-get update

rosdep install --from-paths ~/ros2_ws/src --ignore-src -r -y


The last command above installs all the packages that are not yet installed in our system and are required by the packages in your workspace.

If the message says no package was installed, that is because all dependencies are already installed in the system, but you should have seen at least the package python3-websockets installed.

That python3-websockets package is a dependency of our second package called my_new_ros2_websockets_pkg in our workspace, which was created using the following commands

cd ~/ros2_ws/src

ros2 pkg create --build-type ament_python my_new_ros2_websockets_pkg --dependencies rclpy python3-websockets


Packages that can be installed with rosdep

The list of packages that can be installed using rosdep can be found in the following links:

    1. https://github.com/ros/rosdistro/blob/master/rosdep/base.yaml
    2. https://github.com/ros/rosdistro/blob/master/rosdep/python.yaml


Installing packages using a package manager (apt, apt-get)

We just learned how to install packages using rosdep. Another way of installing dependencies is by using a package manager directly. If you are using Linux Ubuntu, for example, you can use apt or apt-get, which are command-line tools that can be used to install and manage packages.

You can use the following command to list the packages installed with apt;

apt list --installed


The output would be something similar to the following:

xxd/jammy,now 2:8.2.3995-1ubuntu2 amd64 [installed,automatic]
xz-utils/jammy,now 5.2.5-2ubuntu1 amd64 [installed,automatic]
yaru-theme-gnome-shell/now 21.10.2 all [installed,upgradable to: 22.04.4]
yaru-theme-gtk/jammy,jammy,now 22.04.4 all [installed,automatic]
yaru-theme-icon/jammy,jammy,now 22.04.4 all [installed,automatic]
yaru-theme-sound/jammy,jammy,now 22.04.4 all [installed,automatic]
yelp-xsl/jammy,jammy,now 42.0-1 all [installed,automatic]
yelp/jammy,now 42.1-1 amd64 [installed,automatic]
zenity-common/jammy-updates,jammy-updates,now 3.42.1-0ubuntu1 all [installed,automatic]
zenity/jammy-updates,now 3.42.1-0ubuntu1 amd64 [installed,automatic]
zip/jammy,now 3.0-12build2 amd64 [installed,automatic]
zlib1g-dev/jammy,now 1:1.2.11.dfsg-2ubuntu9 amd64 [installed,automatic]
zlib1g/jammy,now 1:1.2.11.dfsg-2ubuntu9 amd64 [installed,automatic]
zstd/jammy,now 1.4.8+dfsg-3build1 amd64 [installed,automatic]


If you want to see if a given package named xml is installed, you would run the command below:

apt list --installed| grep libxml

which would should packages that contain the word xml:

libxml-parser-perl/jammy,now 2.46-3build1 amd64 [installed,automatic]
libxml-twig-perl/jammy,jammy,now 1:3.52-1 all [installed,automatic]
libxml-xpathengine-perl/jammy,jammy,now 0.14-1 all [installed,automatic]
libxml2/jammy-updates,jammy-security,now 2.9.13+dfsg-1ubuntu0.1 amd64 [installed,automatic]
libxmlb2/jammy,now 0.3.6-2build1 amd64 [installed,automatic]


Another way of finding installed packages is by calling dpkg directly.

sudo dpkg -l | grep xml


If you know the name of the package you wish to install, you can install it by using this syntax:

sudo apt-get install <package1> <package2> <package3>


Replace the <package> with your desired package name. Example:

sudo apt-get install -y python3-pandas


Sometimes, it may happen that apt or apt-get install will fail and you will get this error message:

Package 'python3-pandas' has no installation candidate

when this happens, usually, running sudo apt update before the apt install command can solve the issue:

sudo apt-get update

sudo apt-get install python3-pandas


Installing packages using pip and virtualenv

Instead of installing python dependencies system-wide, you can install them in a virtual environment, so that you can have different versions of a given package being used by different projects, yet they do not conflict. For that, we need to use virtualenv.

To check if virtualenv is installed, we can use the following command:

virtualenv --version

If you have an error like the following, it means virtualenv is not installed:

bash: virtualenv: command not found


In order to install virtualenv, we would use the following commands:

sudo apt update

sudo apt install python3-virtualenv


Once virtualenv is installed, we can create our virtual environment this way:

cd ~

mkdir -p ~/ros2_ws_venv/src

cd ~/ros2_ws_venv

virtualenv -p python3 ./venv


If everything went well, you should now have a python virtual environment in the ~/ros2_ws_venv folder.

You can now activate your environment with the following command:

source ./venv/bin/activate

After you activate the environment, your command prompt will change to show that you are in a Python virtual environment, so, instead of having user:~/ros2_ws_venv$  in your terminal, you would see something like this:

(venv) user:~/ros2_ws_venv$


Ignoring the venv folder  (virtual environment) when compiling our ros2 workspace

In order to tell ROS2 not to consider our venv folder when compiling our workspace, we just create a file named COLCON_IGNORE in the folder that we want to ignore.


touch ~/ros2_ws_venv/venv/COLCON_IGNORE


All right. In the previous section we activated our environment with:

cd ~/ros2_ws_venv

source ./venv/bin/activate

Now, we can install Python packages in this environment just like we did before. Example:

python3 -m pip install torch

Now you can build your workspace and run your python node that depends on packages installed in your virtual environment.

Let’s compile our workspace:

cd ~/ros2_ws_venv

colcon build


Now your packages can use the Python packages installed in your virtual environment, just like if they were installed system-wide.

As long as your virtual environment is activated, you can install packages into that specific environment and you will be able to import and use those packages in your application.

Bear in mind that you always need to have the virtual environment activated when you want o use it.

To exit/deactivate the virtual environment, you can run the following command:



Congratulations on reaching the end of this post. I hope it was useful for you. If you want a live version of this post, please check the video in the next section.

Youtube video

So this is the post for today. Remember that we have the live version of this post on YouTube. If you liked the content, please consider subscribing to our youtube channel. We are publishing new content ~every day.

Keep pushing your ROS Learning.

Related Courses & Training

If you want to learn more about ROS and ROS2, we recommend the following courses:

Masterclass 2023 batch2 blog banner

Check Out These Related Posts

124. ROS for Drones

124. ROS for Drones

I would like to dedicate this episode to all the ROS developers who are thinking about using ROS...

read more


Submit a Comment

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

Pin It on Pinterest

Share This