Note

This section is optional, the ROS2 tutorial starts at ROS2 Installation.

Installing Python on Ubuntu

Warning

If you change or try to tinker with the default Python version of Ubuntu, your system will most likely BREAK COMPLETELY. Do not play around with the default Python installation, because Ubuntu depends on it to work properly (or work at all).

In Ubuntu 22.04, Python is already installed! In fact, Ubuntu would not work without it. Let’s check its version by running

python3 --version

which should output

Python 3.10.6

If the 3.10 part of your version is different (e.g. 3.9 or 3.11), get this fixed because this tutorial will not work for you.

Warning

Note that the command is python3 and not python. In fact, the result of

python

is

 Command 'python' not found, did you mean:
command 'python3' from deb python3
command 'python' from deb python-is-python3

A quick Python check

Run

python3

which should output something similar to

Python 3.10.6 (main, Mar 10 2023, 10:55:28) [GCC 11.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

in particular, if the GCC 11 is different, e.g. GCC 9 or GCC 12, then get this fixed because this tutorial will not work for you.

As you already know, to exit the interative shell you can use CTRL+D or type quit() and press ENTER.

Some Python packages must be installed through apt

Warning

Aside from these packages that you MUST install from apt, it is best to use venv and pip to install packages only for your user without using sudo.

For some Python packages to work well with the default Python in Ubuntu, they must be installed through apt. If you deviate from this, you can cause issues that might not be easy to recover from.

For the purposes of this tutorial, let us install pip and venv

sudo apt install -y python3-pip python3-venv

When you want to isolate your environment, use venv

Warning

At the time of this writing, there was no support for venv on ROS2 (More info). Until that is handled, we are not going to use venv for the ROS2 tutorials. However, we will use venv to protect our ROS2 environment from these Python preamble tutorials.

Using venv (More info) is quite straightforward.

Create a venv

cd ~
python3 -m venv ros2tutorial_venv

where the only argument, ros2tutorial_venv, is the name of the folder in which the venv will be created.

Activate a venv

Whenever we want to use a venv, it must be explicitly activated.

cd ~
source ros2tutorial_venv/bin/activate

The terminal will change to have the prefix (ros2tutorial_venv) to let us know that we are using a venv, as follows

(ros2tutorial_venv) murilo@murilos-toaster:~$

Deactivate a venv

To deactivate, run

deactivate

We’ll know that we’re no longer using the ros2tutorial_venv because the prefix will disappear back to

murilo@murilos-toaster:~$

Installing libraries

Warning

In these tutorials, we rely either on apt or pip to install packages. There are other package managers for Python and plenty of other ways to install and manage packages. They are, in general, not compatible with each other so, like cleaning products, DO NOT mix them.

Hint

Using python3 -m pip instead of calling just pip allows more control over which version of pip is being called. The need for this becomes more evident when several Python versions have to coexist in a system.

As an example, let us install the best robot modeling and control library ever conceived, DQ Robotics.

First, we activate the virtual environment

cd ~
source ros2tutorial_venv/bin/activate

then, we install

python3 -m pip install dqrobotics

which will result in something similar to (might change depending on future versions)

Collecting dqrobotics
Downloading dqrobotics-23.4.0a15-cp310-cp310-manylinux1_x86_64.whl (551 kB)
     ---------------------------------------- 551.4/551.4 KB 6.3 MB/s eta 0:00:00
Collecting numpy
  Downloading numpy-1.25.0-cp310-cp310-manylinux_2_17_x86_64.manylinux2014_x86_64.whl (17.6 MB)
     ---------------------------------------- 17.6/17.6 MB 7.4 MB/s eta 0:00:00
Installing collected packages: numpy, dqrobotics
Successfully installed dqrobotics-23.4.0a15 numpy-1.25.0

Removing libraries (installed with pip)

We can remove the library we just installed with

python3 -m pip uninstall dqrobotics

resulting in

Found existing installation: dqrobotics 23.4.0a15
Uninstalling dqrobotics-23.4.0a15:
  Would remove:
    /home/murilo/ros2tutorial_venv/lib/python3.10/site-packages/dqrobotics-23.4.0a15.dist-info/*
    /home/murilo/ros2tutorial_venv/lib/python3.10/site-packages/dqrobotics/*
Proceed (Y/n)?

Hint

If in the terminal a question is made, the option with an uppercase letter, in this case Y, will be the default. If you want the default, just press ENTER.

Then, press ENTER, which results in

Successfully uninstalled dqrobotics-23.4.0a15

When using pip, do NOT use sudo

Using sudo without knowing what one is doing is the easiest way to wreak havoc in a Ubuntu installation. Even seemingly innocuous operations such as copying files with sudo can cause irreparable damage to your Ubuntu environment.

When installing Python packages that are not available on apt, use pip.