Stage 03 · Lesson 06/14

Linux & Dev Environment · Lesson 06

Package Management

Est. 35–45 min

Why

Why you never hunt for .exe files on Linux

A robotics laptop needs compilers, Git, Python tools, camera utilities and later ROS 2. Installing them from trusted repositories (instead of downloading random files) keeps the machine secure, consistent and easy to rebuild.

Concept

The big idea: repositories, packages and dependencies

  • Package: a bundle of program + files + metadata (version, what it depends on). On Ubuntu they are .deb files.
  • Repository: a signed online store of packages. Your list of repositories lives under /etc/apt/.
  • apt: the friendly front-end. dpkg: the lower-level tool that actually unpacks .deb files.
  • Dependencies: packages a package needs. apt works them out and installs them too.
  • Two worlds: apt for system tools (git, compilers, python3-venv), pip inside a venv for your project’s Python libraries (Lesson 11).
Everyday apt commands
CommandMeaningNeeds sudo?
sudo apt updateDownload the latest package lists (rate list)Yes
sudo apt upgradeUpgrade installed packagesYes
sudo apt install gitInstall a package + dependenciesYes
sudo apt remove pkg / purgeRemove (purge also removes its config)Yes
sudo apt autoremoveRemove dependencies nobody needs any moreYes
apt search serialSearch names and descriptionsNo
apt show python3-venvRead the package labelNo
dpkg -L gitWhich files did this package install?No
dpkg -S /usr/bin/gitWhich package owns this file?No

How it works

How it actually works: what `apt install` does

  1. Read the local list

    apt checks the package lists downloaded by your last apt update. Old list → it may not find new packages (“Unable to locate package”).

  2. Solve dependencies

    It works out every extra package needed and shows you the plan: “The following NEW packages will be installed…”.

  3. Download and verify

    Packages are downloaded and their signatures are checked against the repository keys — this is why repositories are “trusted”.

  4. Unpack and configure

    dpkg places files: programs into /usr/bin, libraries into /usr/lib, settings into /etc.

  5. Ready to use

    The new command is now found through $PATH (Lesson 7). Check with command -v git.

Visual

Visualise it: where installed programs land

apt places programs like git and python3 in /usr/bin. Pick a command and see which folder on PATH provides it — then toggle the venv to see why project Python should not come from apt.

Interactive · PATH lookup lab (which program really runs?)

When you type a command, the shell walks the folders in $PATH from left to right and runs the first match — like a dabbawala stopping at the first pickup point that has your tiffin.

  1. 1. /home/ashish/.local/bin

    your personal tools

    ✗ no python3 here — keep walking

  2. 2. /usr/local/bin

    tools you installed by hand

    ✗ no python3 here — keep walking

  3. 3. /usr/bin

    apt packages live here

    ✔ FOUND → runs this one

  4. 4. /bin

    on Ubuntu this is a link to /usr/bin

    not checked (search already stopped)

$ echo $PATH
/home/ashish/.local/bin:/usr/local/bin:/usr/bin:/bin
$ command -v python3
/usr/bin/python3

# runs: Python 3.12 (system — do not pip install here)

Try: pick robotctl and move /usr/bin above ~/.local/bin. Then pick python3 and toggle the venv.

Worked example

Worked example: apt or pip?

You need…UseWhy
git, build-essential, curlaptSystem tools shared by every project
python3-venvaptNeeded to create virtual environments on Ubuntu
numpy, pyserial, opencv-python for one projectpip inside a venvEach project can pin its own versions
ROS 2 Jazzyapt (after adding the official ROS 2 repository)Follow the official install guide for your Ubuntu version
A Python command-line tool for yourselfpipxIsolated, but available everywhere

Code lab

Code lab: check, then install

First, a script that only checks which tools exist. Safe to run anywhere.

check_tools.sh · Shell
#!/usr/bin/env bash
# check_tools.sh — which robotics tools are already installed? (Lesson 6)
# Only checks; installs nothing.
for tool in git python3 gcc make curl ros2; do
  if command -v "$tool" >/dev/null 2>&1; then
    echo "OK       $tool  ->  $(command -v "$tool")"
  else
    echo "MISSING  $tool"
  fi
done

Output on our test machine (yours will differ — that is the point)

OK       git  ->  /usr/bin/git
OK       python3  ->  /usr/bin/python3
OK       gcc  ->  /usr/bin/gcc
MISSING  make
OK       curl  ->  /usr/bin/curl
MISSING  ros2

Then, on a real Ubuntu laptop, install the missing basics. Read each line before you press Enter; apt will show a plan and ask Y/n.

apt_setup.sh · Shell
# apt_setup.sh — fresh Ubuntu robotics laptop (run on a REAL Ubuntu machine)
sudo apt update                      # 1. download the latest package list
sudo apt upgrade -y                  # 2. update what is already installed
sudo apt install -y git build-essential python3-pip python3-venv curl
apt show python3-venv | head -n 3    # 3. read the "label" of a package
apt list --installed | grep python3-venv

Run check_tools.sh again — make and gcc (from build-essential) should now say OK. ros2 stays MISSING until Stage 10.

Real world

Where package skills show up in robotics

  • Jetson / Pi bring-up usually starts with sudo apt update && sudo apt install git python3-venv.
  • ROS 2 is installed with apt from the official ROS 2 repository.
  • v4l-utils gives v4l2-ctl --list-devices for checking USB cameras.
  • Keeping an apt_setup.sh in your repo lets a teammate rebuild your laptop setup in minutes.

Mistakes

Common mistakes and debugging

Hardware & safety

Hardware lab and safety notes

Packages are how a Raspberry Pi or Jetson gets its robot tools. Install carefully — a robot that half-updated is a robot that does not boot.

  • On a Pi, run sudo apt update and then apt list --upgradable before upgrading anything.
  • Keep the Pi on its proper power supply during upgrades; power loss mid-upgrade can corrupt the SD card.
  • Python libraries such as pyserial go in a venv (Lesson 11), not system-wide with sudo pip.
Raspberry Pi 4 Model B single-board computer, top view
On a Raspberry Pi, apt installs system tools; a venv holds your Python libraries.Photo: Laserlicht · CC BY-SA 4.0 · Source: Wikimedia Commons · resized, converted to WebP

Debugging

Debugging: when it doesn’t work

Symptom → likely cause → fix
SymptomLikely causeFix
Unable to locate packagePackage list out of date or wrong namesudo apt update, then apt search name
error: externally-managed-environmentpip outside a venv on Ubuntu 23.04+ / Debian 12+ (PEP 668)Create a venv (Lesson 11) or use the python3-... apt package
Could not get lock /var/lib/dpkg/lockAnother apt (or auto-update) is runningWait a few minutes; do not delete the lock file
command not found right after installTool installed under a different namedpkg -L package | grep bin lists its programs
NoteRead the last line of an error first — it usually names the problem. Change one thing at a time and re-run.

Think

Think about it

Think

Why not `pip install` every library globally as root?

Experiment

Try it in the lab: plan a fresh robotics laptop

Pretend a new Ubuntu laptop has just arrived in the lab.

Experiment

Plan a fresh robotics laptop

You will need

  • PATH lookup lab
  • Notebook
  • Optional: Ubuntu machine

Steps

  1. Write 8 tools you would want on day one (git, python3-venv, curl, htop, …).
  2. Mark each one: apt, pip-in-venv, or later (ROS 2).
  3. In the PATH lookup lab, pick `git` — which folder provides it? That is where apt installed it.
  4. Pick `pip` with the venv OFF, then ON. Explain the difference in one line.
  5. On Ubuntu, run `check_tools.sh`, install the missing ones, and run it again.

Interactive · PATH lookup lab (which program really runs?)

When you type a command, the shell walks the folders in $PATH from left to right and runs the first match — like a dabbawala stopping at the first pickup point that has your tiffin.

  1. 1. /home/ashish/.local/bin

    your personal tools

    ✗ no python3 here — keep walking

  2. 2. /usr/local/bin

    tools you installed by hand

    ✗ no python3 here — keep walking

  3. 3. /usr/bin

    apt packages live here

    ✔ FOUND → runs this one

  4. 4. /bin

    on Ubuntu this is a link to /usr/bin

    not checked (search already stopped)

$ echo $PATH
/home/ashish/.local/bin:/usr/local/bin:/usr/bin:/bin
$ command -v python3
/usr/bin/python3

# runs: Python 3.12 (system — do not pip install here)

Try: pick robotctl and move /usr/bin above ~/.local/bin. Then pick python3 and toggle the venv.

apt-installed tools live in /usr/bin. Your project’s pip and python should come from .venv/bin once activated.

Knowledge check

Check your understanding

Knowledge check

1. On Ubuntu, `apt` is mainly for…

2. Python project dependencies are usually better in…

3. This browser course…

4. `sudo apt update` …

5. You see “externally-managed-environment” from pip. Best fix?

Mini assignment

Hands-on assignment

Mini assignment

apt shortlist

Create `apt_setup.sh` for your own robotics laptop.

Deliverables

  • At least 6 apt packages, each with a one-line reason
  • A separate list of Python libraries that will go into the venv instead
  • Before/after output of `check_tools.sh`

Do this, step by step

  1. Run check_tools.sh and save the output as “before”.
  2. Write your apt_setup.sh with at least 6 packages and a reason for each.
  3. Run it on an Ubuntu machine (or explain each line if you cannot).
  4. Run check_tools.sh again and save “after”.

Stuck? Hints, one at a time

NoteTry the task first. Read one hint, try again, and only then read the next one.
  1. Hint 1: Good starting packages: git, build-essential, python3-venv, python3-pip, curl, tmux.
  2. Hint 2: -y only answers “yes”; it does not make an install safer.
  3. Hint 3: pyserial, numpy and opencv-python belong in the venv list, not in apt.

Challenges (pick by level)

  • 🟢 Easy: Use apt show tmux to read a package’s description and size.
  • 🟡 Medium: Find which package owns /usr/bin/git with dpkg -S /usr/bin/git.
  • 🟠 Hard: Make apt_setup.sh skip packages already installed and print a summary.

Next step

Look back: Lesson 5: Processes & System Monitoring. Next: Lesson 7: Environment Variables. Related: Stage 2 — Modules, Packages & Environments.

Sources

Self-attested for now — keep artifacts in your lab notebook.

Complete

Today's goal

0 of 1 done today

0-day streak
Daily goal:lessons, quizzes or assignments a day

Mark this lesson complete when you have worked the experiment, knowledge check, and assignment (even roughly).

Stuck somewhere?

Ask a doubt about this lesson

Saved with this lesson's id and your email so we can reply. Privacy · or email contact@aihubstation.in