03 — Linux, Python & AI-Ready Raspberry Pi OS
Overview
With Raspberry Pi OS installed and the desktop configured, this tutorial covers the Linux terminal commands and Python basics you will use throughout the labs, then walks through installing the packages needed to run computer-vision and AI workloads on the Raspberry Pi 5.
1. Open a Terminal
Open a terminal in any of the following ways:
- Click the terminal icon on the Panel
- Press
Ctrl+Alt+T - Connect remotely with
ssh username@hostname.local
2. Linux Essentials
Moving Around the Filesystem
| Command | Purpose |
|---|---|
pwd |
Show the current directory |
ls |
List files |
ls -la |
List files, including hidden files |
cd folder |
Enter a directory |
cd .. |
Move up one directory |
cd ~ |
Go to your home directory |
mkdir folder |
Create a directory |
cp source destination |
Copy a file |
mv source destination |
Move or rename a file |
rm file |
Delete a file |
clear |
Clear the terminal |
history |
Show recent commands |
Linux commands such as rm normally do not move files to a recycle bin. Double-check paths before deleting files.
Permissions and sudo
sudo runs a command with administrator privileges — required for system changes such as installing software or editing configuration files:
sudo raspi-configMake a script executable:
chmod +x script.shInstalling Software with apt
sudo apt update
sudo apt install package-name -yThe first command refreshes the package list; the second installs the requested package. Remove a package with:
sudo apt remove package-nameEditing Files with nano
nano filename.txt| Shortcut | Action |
|---|---|
Ctrl+O |
Save (write out) |
Ctrl+X |
Exit |
Ctrl+K |
Cut a line |
Ctrl+U |
Paste a line |
3. Python Essentials
Check the Installed Version
Raspberry Pi OS ships with Python 3 pre-installed:
python3 --version
pip3 --versionThonny: the Bundled IDE
Thonny (Main Menu → Programming → Thonny) is a beginner-friendly Python IDE that comes preinstalled on Raspberry Pi OS. It includes a code editor, a variables viewer, and a step-through debugger — useful for early labs before moving to VS Code.
Virtual Environments
Create an isolated environment for course projects:
python3 -m venv ~/cv_env
source ~/cv_env/bin/activateRecent Raspberry Pi OS releases mark the system Python as externally managed, so a plain pip install outside a virtual environment will refuse to run. Always activate a venv first for course work.
Installing Packages with pip
With the virtual environment activated:
pip install numpy opencv-pythonRunning a Script
python3 my_script.py4. Prepare Raspberry Pi OS for AI & Computer Vision
Install OpenCV
sudo apt install -y python3-opencvOr, inside a virtual environment:
pip install opencv-pythonInstall picamera2
picamera2 is the official library for the Raspberry Pi Camera Module and usually comes preinstalled. If it is missing:
sudo apt install -y python3-picamera2Install a Lightweight ML Runtime
Full TensorFlow or PyTorch will run on a Pi 5 but are heavy for real-time inference. For course labs, prefer a lightweight runtime with a quantized model:
pip install tflite-runtime
pip install onnxruntimetflite-runtime runs TensorFlow Lite (.tflite) models; onnxruntime runs models exported in the cross-framework ONNX format.
Optional AI Accelerators
For labs that need faster on-device inference, the Pi 5 supports:
- Coral USB Accelerator (Edge TPU) — fast INT8 TensorFlow Lite inference over USB
- Raspberry Pi AI Kit (Hailo-8 / Hailo-8L) — an M.2 accelerator connected via the Pi 5's PCIe interface for high-throughput on-device inference
Quick Test
Verify the camera and OpenCV are working together:
import cv2
cap = cv2.VideoCapture(0)
ret, frame = cap.read()
print("Frame captured:", ret, frame.shape if ret else None)
cap.release()Run it:
python3 camera_test.pyA successful run prints Frame captured: True followed by the frame's height, width, and channel count.
Good Habits
- Keep the system updated regularly:
sudo apt update && sudo apt full-upgrade -y - Use a separate virtual environment per project
- Back up your SD card image before making major changes, using Raspberry Pi Imager's read/backup option
Next Steps
You are ready to start the lab exercises. Continue exploring the Visual Studio Code and NumPy tutorials for the rest of the development workflow used in this course.