Find us on social media
PythonVenvPip5 min read
Install Python and virtual environments
Install Python and configure virtual environments with venv to isolate dependencies per project.
Python comes pre-installed on most Linux distributions, but you may need a newer version or proper virtual environment setup for your projects.
Step 1 — Check current Python version
bash
python3 --versionStep 2 — Install Python and essential tools
bash
sudo apt update
sudo apt install python3 python3-pip python3-venv python3-dev -yStep 3 — Create a virtual environment
Virtual environments isolate project dependencies:
bash
mkdir ~/myproject && cd ~/myproject
python3 -m venv venvStep 4 — Activate the virtual environment
bash
source venv/bin/activateYour prompt changes to show (venv). All pip installs now go into this environment.
Step 5 — Install packages
bash
pip install flask gunicorn requestsSave dependencies:
bash
pip freeze > requirements.txtInstall from requirements:
bash
pip install -r requirements.txtStep 6 — Deactivate the environment
bash
deactivateInstall a newer Python version (if needed)
bash
sudo add-apt-repository ppa:deadsnakes/ppa -y
sudo apt update
sudo apt install python3.12 python3.12-venv python3.12-dev -yCreate a venv with the new version:
bash
python3.12 -m venv venv312Using pip globally (not recommended)
If you must install packages globally:
bash
pip install --break-system-packages package_nameTip
Always use virtual environments for production applications. This prevents dependency conflicts and makes deployments reproducible. Combine with systemd services for production deployment.
Was this guide helpful?