Setting Up Python Virtual Environments on elementary OS
Developers on elementary OS often reach for Python without first separating a project’s packages from the operating system. That shortcut works for a quick script, but it becomes fragile as soon as two projects need different versions of the same library. A virtual environment gives each project its own package collection while leaving the base system and its Python installation untouched. The interpreter version also matters, because some libraries stop supporting older Python releases.
The workflow is familiar across Linux distributions: install the required tools, create an environment inside the project, activate it for terminal work, and record dependencies separately. The commands below use Bash and assume the standard elementary OS Terminal, which opens with Ctrl+Alt+T. This approach keeps experiments repeatable without changing the packages installed for the rest of the operating system.
Prepare the tools
Check the interpreter before creating anything. On a current elementary OS installation, Python 3 is normally available, but the virtual-environment package and Git may still need to be added. Run python3 --version first so you know which interpreter version the project will use.
If the version command fails, or if python3 -m venv reports that ensurepip is unavailable, install the missing components with these commands:
sudo apt update
sudo apt install python3 python3-venv python3-pip git
These packages have distinct roles. python3 is the interpreter, python3-venv supplies the module used to build environments, python3-pip installs Python packages, and Git records changes to project files. Installing them through the system package manager also makes future operating-system upgrades easier. Keep pip usage inside an environment and avoid sudo pip; system-wide pip installations can overwrite managed files and break later upgrades.
Create a clean project workspace
Choose a project directory before creating the environment. Keeping it under ~/Code makes paths short and separates development work from personal documents. A plausible Australian example is a weather dashboard using observations from the Bureau of Meteorology, although the same layout suits a web service, data script or automation tool.
Create the directory, enter it, initialise Git and build the environment:
mkdir -p "$HOME/Code/bom-weather"
cd "$HOME/Code/bom-weather"
git init
python3 -m venv .venv
The leading dot keeps .venv out of ordinary directory listings, and the conventional name makes its purpose clear to other developers. Add .venv/ and common generated files to .gitignore; the environment contains machine-specific paths and should be rebuilt on each computer rather than committed:
.venv/
__pycache__/
*.py[cod]
.pytest_cache/
.env
One project or checkout should normally have its own environment. The environment inherits the selected Python version, so it does not provide Python 3.11 when the system interpreter is 3.12; another interpreter version requires a separate tool such as pyenv or a container. Rebuilding the environment after cloning a repository is a normal part of joining a project.
Activate and verify the environment
From the project root, activate the environment with:
source .venv/bin/activate
The shell prompt normally gains a (.venv) prefix. Confirm that commands resolve inside the environment rather than to the operating system:
command -v python
python --version
python -c "import sys; print(sys.prefix)"
The first command should point to .venv/bin/python, while the final command should print a path containing .venv. These checks are valuable because forgotten activation can install a package in the wrong place even when the project commands appear to work. echo "$VIRTUAL_ENV" provides another quick confirmation.
Leave the environment with deactivate. Activation changes the current shell only; it does not modify other Terminal windows, and a newly opened shell starts without the prefix. It is not a security sandbox: credentials, network access and filesystem permissions still need normal care. For Australian services, log timestamps in UTC and convert them to Australian Eastern Standard Time or Australian Eastern Daylight Time at the display boundary, avoiding ambiguous local date formats.
Install and record dependencies
With the environment active, upgrade its bundled pip and install packages through the interpreter. Using python -m pip makes the intended interpreter explicit and is safer than relying on whichever pip appears first on the path.
python -m pip install --upgrade pip
python -m pip install requests pytest
Replace the example packages with those required by the project. Virtual environments isolate packages, but they do not freeze time: a package installed today can receive a different version during a later installation. For an application, record the resulting versions in requirements.txt:
python -m pip freeze > requirements.txt
The file will contain direct and transitive packages with version constraints, commonly using the == operator for an exact version. Review it before committing, because automated tools and operating-system packages may also appear in the list. Commit this dependency file to Git while continuing to ignore .venv/. Another developer can create the environment and reproduce the installation with:
python -m pip install -r requirements.txt
For a larger application, a pyproject.toml file with a suitable lock tool can manage dependencies more formally. For a small script or early prototype, a committed requirements file is easier to understand. Whichever format is chosen, update it whenever the dependency set changes and review version upgrades rather than regenerating it automatically every day.
Connect the environment to your editor
A terminal-only workflow is complete once the environment activates and the dependency file exists, but an editor should use the same interpreter. In Visual Studio Code, install the Python extension, open the project folder and run “Python: Select Interpreter” from the Command Palette. Choose the entry ending in .venv/bin/python rather than the system interpreter.
The same selection can be made for each project without changing the global shell. A workspace-level settings file can contain:
{
"python.defaultInterpreterPath": "${workspaceFolder}/.venv/bin/python"
}
This setting tells the Python extension which interpreter to select when the workspace opens. It does not necessarily activate the environment in every integrated terminal, so check the terminal prompt or run source .venv/bin/activate there. Programs launched from a graphical application also have their own environment; giving the executable an explicit path prevents them from silently choosing system Python.
Editors such as GNOME Text Editor, Vim, Neovim and JetBrains IDEs offer similar interpreter controls. The reliable rule is to confirm the executable path whenever an import fails, an installed module is missing or a script behaves differently from the terminal. A team working across Sydney, Melbourne and Brisbane can share the project definition through Git while each workstation keeps its own local environment.
Protect work with snapshots and Git
A virtual environment can be deleted and rebuilt, so source files need their own protection. Git is the primary record: commit the application code, .gitignore, dependency definitions and any configuration templates that contain no secrets. Keep real credentials in an ignored .env file or in a secrets manager rather than placing them in Git history.
Before experimenting with a large dependency change, a system restore point can provide a second safety net. The community guide to Timeshift snapshots explains how to schedule and restore system states on elementary OS. A snapshot is useful for recovering a desktop that has become unstable, but it is not a substitute for project history or an off-site backup.
Use Git to review exactly what changed:
git status
git add .gitignore requirements.txt
git commit -m "Set up isolated Python development"
Push important commits to a remote repository according to the team’s access and data-security rules. Australian fintech, agricultural-technology and geospatial projects may handle commercially sensitive location or customer data, so check repository visibility and retention settings before sharing code. Timeshift can assist with local recovery, while Git and appropriate remote backups preserve work that a system snapshot may not include.
Make the workflow repeatable
The setup becomes dependable when another person can follow the same steps without relying on memory. Keep three facts in the project: the required Python version, the commands that create the environment, and the file that defines dependencies. A README can state that a fresh checkout needs Python 3, python3 -m venv .venv, activation and python -m pip install -r requirements.txt.
Scripts may also call the environment’s interpreter directly, avoiding activation entirely:
#!/usr/bin/env bash
set -euo pipefail
python3 -m venv .venv
.venv/bin/python -m pip install -r requirements.txt
.venv/bin/python app.py
Save this as setup.sh, change app.py to the project’s entry point and run it with bash setup.sh. This approach works well in a development container or continuous-integration job, where each run starts with a clean filesystem. On a normal workstation, activation remains convenient for interactive commands. Whichever style is used, test the documented setup in a new terminal or fresh checkout so hidden assumptions do not survive.
The durable rule to remember is simple: one project, one environment, recorded dependencies, and version-controlled source that lets another developer rebuild the same setup.