Knowledgebase

Find frequently asked questions and setup guides by searching or by browsing the categories.

Create and Activate a Virtual Environment

Inside the project folder, run:

cd /srv/myapp
python3 -m venv .venv
source .venv/bin/activate

The second command creates a folder named .venv with a Python interpreter, pip, and an empty package directory. The third activates it in the current shell: the prompt starts with (.venv), and python and pip now point into that folder. The name .venv is a convention; any folder name works.

Why Use One

  • Packages per project: one application can need an older version of a library than another. Each environment keeps its own set.
  • The system stays intact: tools of the operating system depend on the Python packages installed with apt or dnf. Installing over them with pip can break those tools.
  • pip is blocked outside one: Debian 12 and later and Ubuntu 23.04 and later mark the system Python as externally managed, so a system-wide pip install stops with an error. The background is in error: externally-managed-environment.

Install the venv Module

On Debian and Ubuntu the module ships in a separate package. If python3 -m venv fails with a message about ensurepip, install it:

sudo apt update
sudo apt install python3-venv

On AlmaLinux and Rocky Linux the module comes with the python3 package. Ubuntu 24.04 ships Python 3.12 and Ubuntu 26.04 ships Python 3.14; for other versions see how to install Python on Ubuntu.

Install Packages and Record Them

With the environment active, pip installs into .venv only:

pip install --upgrade pip
pip install requests flask
pip freeze > requirements.txt

requirements.txt lists the exact versions. Commit it to Git and rebuild the environment on any server with:

python3 -m venv .venv
.venv/bin/pip install -r requirements.txt

How pip itself is set up is covered in how to install pip on Ubuntu.

Deactivate It

deactivate

This restores your PATH and prompt. Activation lasts only for the current shell: a new terminal starts without it.

Use It Without Activating

Activation mainly changes PATH. A program started with the environment's own interpreter uses its packages directly, which is what services and scheduled jobs need. In a systemd unit:

[Service]
User=deploy
WorkingDirectory=/srv/myapp
ExecStart=/srv/myapp/.venv/bin/python /srv/myapp/app.py

In a crontab:

*/15 * * * * /srv/myapp/.venv/bin/python /srv/myapp/job.py

Units are explained in systemd unit files, schedules in how to create a cron job.

Do Not Move or Copy an Environment

The scripts in .venv/bin contain the absolute path of the folder they were created in. After a move or a copy to another path or server, pip and other commands point to a location that no longer exists. Delete the folder and create it again from requirements.txt:

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

Do the same after the system Python moves to a new minor version, for example after a release upgrade.

Keep It Out of Git

The environment is large, tied to one machine, and rebuilt from requirements.txt. Add one line to .gitignore:

.venv/

Patterns are covered in how to use .gitignore.

pipx for Command-Line Tools

For a Python program that you run as a command, rather than a library for your own code, pipx creates one hidden environment per tool and puts the command on your PATH:

sudo apt install pipx
pipx ensurepath
pipx install httpie

Open a new shell after pipx ensurepath so the changed PATH takes effect. pipx upgrade-all updates every tool installed this way.

Was this answer helpful?
Loading...