Python virtual environments create isolated spaces for project dependencies, preventing package conflicts between projects. The built-in venv module is the standard approach in Python 3. This guide covers creating, activating, and managing virtual environments on Ubuntu 26.04 LTS.
Tested and valid on:
- Ubuntu 26.04 LTS
Prerequisites
- Ubuntu 26.04 LTS with Python 3.14 installed
- pip installed (
sudo apt install python3-pip)
Step 1 – Install python3-venv
sudo apt install python3-venv python3.14-venv -y
Step 2 – Create a Virtual Environment
mkdir ~/myproject && cd ~/myproject
python3 -m venv .venv
Step 3 – Activate the Environment
source .venv/bin/activate
Your prompt changes to show (.venv). All pip installs will now go into this environment.
Step 4 – Install Packages
pip install requests flask sqlalchemy
pip list
Step 5 – Freeze and Recreate Requirements
pip freeze > requirements.txt
cat requirements.txt
On another machine or environment:
python3 -m venv .venv
source .venv/bin/activate
pip install -r requirements.txt
Step 6 – Deactivate the Environment
deactivate
Step 7 – Use virtualenvwrapper for Convenience
pip install virtualenvwrapper
Add to ~/.bashrc:
export WORKON_HOME=$HOME/.virtualenvs
source $(which virtualenvwrapper.sh)
source ~/.bashrc
mkvirtualenv myproject
workon myproject
Conclusion
Python virtual environments on Ubuntu 26.04 LTS allow clean dependency management per project. Always use a virtual environment for Python development to avoid dependency conflicts and keep the system Python clean.