CodeTheRobot

Ubuntu Linux for Robotics · Lesson 24 of 96

Installing Python tools: pip and virtual environments

python3, pip, venv, why not to pip install system-wide, and Python with ROS 2.

Estimated time · 45–60 min

Learning goals

What you will understand

  • Explain installing python tools: pip and virtual environments in your own words
  • Use it in a small workstation example
  • Verify the result instead of assuming it worked

Understand the idea first

The mental model

Ubuntu ships python3, and ROS 2 tools depend on it. Do not install packages with sudo pip: it can break system tools (recent Ubuntu blocks it with an 'externally managed environment' error). Use apt for packages that Ubuntu provides (python3-numpy) and a virtual environment for everything else.

A virtual environment (python3 -m venv name) is an isolated folder of packages. Activate it with source name/bin/activate. For ROS 2 Python nodes create it with --system-site-packages so it can still import rclpy, and install extra packages with pip inside it.

Guided lesson

Work through these steps

  1. 1.

    Open a terminal and record the current state before changing anything.

  2. 2.

    Read the installing python tools: pip and virtual environments commands and predict what each one does.

  3. 3.

    Run the commands one at a time and compare each output with your prediction.

  4. 4.

    Write the commands that worked in a setup note you can repeat later.

Worked example

Read it before you run it

sudo apt install python3-pip python3-venv
# Install pip and the venv module.
python3 -m venv --system-site-packages ~/venvs/robot
# Create an environment that can still see ROS 2's Python packages.
source ~/venvs/robot/bin/activate
# Activate it; the prompt shows (robot).
pip install numpy opencv-python
# Install packages into the environment only.
python -c "import sys; print(sys.prefix)"
# Confirm the interpreter is the one inside the environment.
deactivate
# Leave the environment.

What the example is doing

  1. 1.Lines without sudo only inspect or change files you own; sudo authorizes a system-wide change.
  2. 2.Read command output before moving to the next line. Stop if names, versions, disks, or paths differ from the lesson.
  3. 3.Comments beginning with # explain safety checks and are not executed by the shell.

Watch it run · Build and run

See what this code does

The terminal types this lesson's practice solution and runs it. The scene shows the kind of robot behaviour that code drives. It plays by itself and loops.

sourcecompilerprogramBUILD AND RUN

Demo for Installing Python tools: pip and virtual environments. Expected result: True

Practice code

Now type it yourself

Open a terminal, run the starter line by line (or save it as practice.sh and run bash practice.sh), and replace every TODO with your own command.

# First run: sudo apt install python3-venv
d=$(mktemp -d)
# TODO: create a virtual environment in $d/venv with python3 -m venv
"$d/venv/bin/python" -c "import sys; print(sys.prefix != sys.base_prefix)"

Expected output

True
Stuck? Show the full solution (try it yourself first)
# First run: sudo apt install python3-venv
d=$(mktemp -d)
python3 -m venv "$d/venv"
"$d/venv/bin/python" -c "import sys; print(sys.prefix != sys.base_prefix)"

Expected result

How you know it worked

  • Each command completes without an unexplained error.
  • Your notes contain the command, the important output, and what it proves.

Practical exercise

Apply the lesson

Create a virtual environment, install a package in it, show that the package is missing outside it, and write down why pip install with sudo is a bad idea on Ubuntu.

Before you continue

  • You recorded the starting state
  • Every command and output is understood
  • The change can be repeated from your notes

If something goes wrong

A command says permission denied.

Check ownership and permissions first. Do not add sudo unless the command changes a documented system location.

A command or package is not found.

Check spelling, Ubuntu version, enabled repositories, and whether the previous installation command succeeded.