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, 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.12.3
If the 3.12 part of your version is different, this tutorial might not work for you. Please make sure to use the default Python in your Ubuntu.
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.12.3 (main, Jun 18 2025, 17:59:45) [GCC 13.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
in particular, if the GCC 13 is different, then this tutorial might not work for you.
As you already know, to exit the interactive 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 update
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 --break-system-packages
which will result in something similar to (might change depending on future versions)
Collecting dqrobotics
Downloading dqrobotics-25.4.0a17-cp312-cp312-manylinux2014_aarch64.whl.metadata (2.9 kB)
Requirement already satisfied: numpy in /usr/lib/python3/dist-packages (from dqrobotics) (1.26.4)
Downloading dqrobotics-25.4.0a17-cp312-cp312-manylinux2014_aarch64.whl (512 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 512.5/512.5 kB 14.0 MB/s eta 0:00:00
[...]
Installing collected packages: dqrobotics
Successfully installed dqrobotics-25.4.0a17
Removing libraries (installed with pip)#
We can remove the library we just installed with
python3 -m pip uninstall dqrobotics --break-system-packages
resulting in
Found existing installation: dqrobotics 25.4.0a7
Uninstalling dqrobotics-25.4.0a7:
Would remove:
/usr/local/lib/python3.12/dist-packages/dqrobotics-25.4.0a7.dist-info/*
/usr/local/lib/python3.12/dist-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-25.4.0a7
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.