Python Virtual Environments: venv and virtualenv
A Python virtual environment is a self-contained directory that includes its own Python interpreter and a set of installed packages, isolated from the system-wide Python installation. Using virtual environments lets each project maintain its own dependencies without affecting other projects on the same machine.
This guide explains how to create and manage virtual environments using venv (built into Python 3) and virtualenv (a popular third-party alternative) on Linux and macOS.
Quick Reference
| Task | Command |
|---|---|
| Install venv support (Ubuntu, Debian) | sudo apt install python3-venv |
| Create environment | python3 -m venv venv |
| Activate (Linux / macOS) | source venv/bin/activate |
| Deactivate | deactivate |
| Install a package | pip install package-name |
| Install specific version | pip install package==1.2.3 |
| List installed packages | pip list |
| Save dependencies | pip freeze > requirements.txt |
| Install from requirements file | pip install -r requirements.txt |
| Create with specific Python version | python3.11 -m venv venv |
| Install virtualenv | pip install virtualenv |
| Delete environment | rm -rf venv |
What Is a Python Virtual Environment?
When you install a package globally with pip install, it is available to every Python script on the system. This becomes a problem when two projects need different versions of the same library — for example, one requires requests==2.28.0 and another requires requests==2.31.0. Installing both globally is not possible.
A virtual environment solves this by giving each project its own isolated space for packages. Activating an environment prepends its bin/ directory to your PATH, so python and pip resolve to the versions inside the environment rather than the system-wide ones.
Installing venv
The venv module ships with Python 3 and requires no separate installation on most systems. On Ubuntu and Debian, the module is packaged separately:
sudo apt install python3-venvOn Fedora, RHEL, and Derivatives, venv is included with the Python package by default.
Verify your Python version before creating an environment:
python3 --versionFor more on checking your Python installation, see How to Check Python Version .
Creating a Virtual Environment
Navigate to your project directory and run:
python3 -m venv venvThe second venv is the name of the directory that will be created. The conventional names are venv or .venv. The directory contains a copy of the Python binary, pip, and the standard library.
To create the environment in a specific location rather than the current directory:
python3 -m venv ~/projects/myproject/venvActivating the Virtual Environment
Before using the environment, you need to activate it.
On Linux and macOS:
source venv/bin/activateOnce activated, your shell prompt changes to show the environment name:
(venv) user@host:~/myproject$
From this point on, python and pip refer to the versions inside the virtual environment, not the system-wide ones.
To deactivate the environment and return to the system Python:
deactivateInstalling Packages
With the environment active, install packages using pip as you normally would. If pip is not installed on your system, see How to Install Python Pip on Ubuntu
.
pip install requestsTo install a specific version:
pip install requests==2.31.0To list all packages installed in the current environment:
pip listPackages installed here are completely isolated from the system and from other virtual environments.
Managing Dependencies with requirements.txt
Sharing a project with others, or deploying it to another machine, requires a way to reproduce the same package versions. The convention is to save all dependencies to a requirements.txt file:
pip freeze > requirements.txtThe file lists every installed package and its exact version:
certifi==2024.2.2
charset-normalizer==3.3.2
idna==3.6
requests==2.31.0
urllib3==2.2.1
To install all packages from the file in a fresh environment:
pip install -r requirements.txtThis is the standard workflow for reproducing an environment on a different machine or in a CI/CD pipeline.
Using a Specific Python Version
By default, python3 -m venv uses whichever Python 3 version is the system default. If you have multiple Python versions installed, you can specify which one to use:
python3.11 -m venv venvThis creates an environment based on Python 3.11 regardless of the system default. To check which versions are available :
python3 --version
python3.11 --versionvirtualenv: An Alternative to venv
virtualenv is a third-party package that predates venv and offers a few additional features. Install it with pip:
pip install virtualenvCreating and activating an environment with virtualenv follows the same pattern:
virtualenv venv
source venv/bin/activateTo use a specific Python interpreter:
virtualenv -p python3.11 venvWhen to use virtualenv over venv:
- You need to create environments faster —
virtualenvis significantly faster on large projects. - You are working with Python 2 (legacy codebases only).
- You need features like
--copiesto copy binaries instead of symlinking.
For most modern Python 3 projects, venv is sufficient and has the advantage of requiring no installation.
Excluding the Environment from Version Control
The virtual environment directory should never be committed to version control. It is large, machine-specific, and fully reproducible from requirements.txt. Add it to your .gitignore:
echo "venv/" >> .gitignoreIf you named your environment .venv instead, add .venv/ to .gitignore.
Troubleshooting
python3 -m venv venv fails with “No module named venv”
On Ubuntu and Debian, the venv module is not included in the base Python package. Install it with sudo apt install python3-venv, then retry.
pip install installs packages globally instead of into the environment
The environment is not activated. Run source venv/bin/activate first. You can verify by checking which pip — it should point to a path inside your venv/ directory.
“command not found: python” after activating the environment
The environment uses python3 as the binary name if it was created with python3 -m venv. Use python3 explicitly, or check which python and which python3 inside the active environment.
The environment breaks after moving or renaming its directory
Virtual environments contain hardcoded absolute paths to the Python interpreter. Moving or renaming the directory invalidates those paths. Delete the directory and recreate the environment in the new location, then reinstall from requirements.txt.
FAQ
What is the difference between venv and virtualenv?venv is a standard library module included with Python 3 — no installation needed. virtualenv is a third-party package with a longer history, faster environment creation, and Python 2 support. For new Python 3 projects, venv is the recommended choice.
Should I commit the virtual environment to git?
No. Add venv/ (or .venv/) to your .gitignore and commit only requirements.txt. Anyone checking out the project can recreate the environment with pip install -r requirements.txt.
Do I need a virtual environment for every project?
It is strongly recommended. Without isolated environments, installing or upgrading a package for one project can break another. The overhead of creating a virtual environment is minimal.
What is the difference between pip freeze and pip list?pip list shows installed packages in a human-readable format. pip freeze outputs them in requirements.txt format (package==version) suitable for use with pip install -r.
Can I use a virtual environment with a different Python version than the system default?
Yes. Pass the path to the desired interpreter when creating the environment: python3.11 -m venv venv. The environment will use that interpreter for both python and pip commands.
Conclusion
Virtual environments are the standard way to manage Python project dependencies. Create one with python3 -m venv venv, activate it with source venv/bin/activate, and use pip freeze > requirements.txt to capture your dependencies. For more on managing Python installations, see How to Check Python Version
.

