Setting up a new Python virtual environment (Python 3.12) on Ubuntu/Debian

Purpose

This guide standardizes how we create a local Python virtual environment for development and ensure packaging tools (pip / wheel / setuptools) are current before installing project dependencies.

Prerequisites

  • Ubuntu/Debian-based Linux host

  • Sudo access

  • A requirements.txt file in the project directory (or a clear plan for dependency installation)

1) Install required OS packages

Install Python 3.12, pip, and the venv module for Python 3.12.

sudo apt install python3-pip python3.12-venv python3.12

2) Verify python and pip resolution (sanity check)

Confirm which executables are being used on your PATH.

which pip
which python3

python3.12 --version  # expected: 3.12.x (example: 3.12.11)

Why this matters: On multi-Python systems, python3, python, and pip may not point to the interpreter you intend. Always confirm versions before proceeding.

3) Create a new virtual environment

Create the virtual environment in your home directory (recommended for local dev).

python -m venv ~/.env_torch

Naming: use a meaningful suffix (e.g., ~/.env_<project> or ~/.env_<stack>) to make environments easy to identify.

4) Activate the virtual environment

source ~/.env_torch/bin/activate

After activation, your shell prompt typically shows the environment name. You can also confirm with:

which python
python --version

5) Upgrade core Python packaging tools

Upgrade pip, wheel, and setuptools inside the virtual environment.

python -m pip install --upgrade pip wheel setuptools

Why this matters: Many build/install issues are caused by outdated packaging tooling, especially for packages with compiled components.

6) Install project dependencies

From your project directory:

python -m pip install -r requirements.txt

Common checks and troubleshooting

  • If you see dependency installs going to system Python, your venv likely isn’t activated. Re-run the activation step and confirm which python.

  • If python3.12 exists but python -m venv ... uses a different Python, explicitly run:

    python3.12 -m venv ~/.env_torch
    
    
  • If pip behaves unexpectedly, prefer python -m pip ... (as shown above) to guarantee you’re using the venv’s pip.

Metadata

Category: Engineering Enablement → Developer Workstations → Python Tooling
Tags: python, python3.12, venv, virtualenv, pip, ubuntu, debian, dependency-management