Linux Apps & tools

Run a Python script in a venv on Linux

Avoid pip conflicts with system packages — venv basics done right.

11 min read Beginner Updated 9 Jun 2026

Step-by-step guide

Work through each section in order. Stop when your issue is resolved — you do not need every step for every situation.

Warning

Never pip install as root into system Python on Debian/Ubuntu — it breaks apt-managed packages. Use venv for project deps; use apt install python3-foo for system-wide modules when available.

What you will achieve

Create a Python virtual environment on Ubuntu or Debian, install dependencies from requirements.txt, and run scripts in isolation from system Python — avoiding the pip-vs-apt wars.

1) Install venv support

sudo apt update
sudo apt install python3 python3-venv python3-pip

Match Python version to your project: python3.12-venv on Ubuntu 24.04 if the default is 3.12.

2) Create virtual environment

cd ~/projects/myapp
python3 -m venv .venv

.venv in the project root is conventional. Add .venv/ to .gitignore.

3) Activate the venv

source .venv/bin/activate

Prompt shows (.venv). which python should point inside .venv/bin/. Deactivate with deactivate.

4) Install requirements

pip install --upgrade pip
pip install -r requirements.txt

No sudo — everything installs into the venv. Pin versions in requirements.txt for reproducibility:

pip freeze > requirements-lock.txt

5) Run your script

python scripts/process_data.py
python -m mypackage.cli --verbose

Module form (-m) works when the package is installed editable:

pip install -e .

6) Run without activating (cron/systemd)

/home/user/projects/myapp/.venv/bin/python /home/user/projects/myapp/scripts/job.py

Use absolute paths in unit files and crontabs — activation is a shell convenience, not required.

7) Recreate from scratch

deactivate
rm -rf .venv
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt

When dependencies tangle, burning down the venv beats fighting system site-packages.

8) System packages vs pip

Some Python libs exist in apt as python3-requests etc. — fine for system tools, not for project isolation. Inside a venv, always prefer pip install. On Debian, PEP 668 may block system pip — another reason venv is mandatory for application work.

Prerequisites

python3-venv installed, project directory writable, and requirements.txt checked into version control with pinned versions for reproducible deploys.

Troubleshooting

ensurepip errors mean missing python3-venv. externally-managed-environment on Debian 12+ confirms you must use venv — do not override with --break-system-packages on production hosts.

Verify

python -c "import sys; print(sys.prefix)"
pip list
python scripts/process_data.py --help

sys.prefix must be the venv path, not /usr. Script runs without ModuleNotFoundError for listed requirements.

Related guides

linux python venv