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 --version

Step 2 — Install Python and essential tools

bash
sudo apt update
sudo apt install python3 python3-pip python3-venv python3-dev -y

Step 3 — Create a virtual environment

Virtual environments isolate project dependencies:

bash
mkdir ~/myproject && cd ~/myproject
python3 -m venv venv

Step 4 — Activate the virtual environment

bash
source venv/bin/activate

Your prompt changes to show (venv). All pip installs now go into this environment.

Step 5 — Install packages

bash
pip install flask gunicorn requests

Save dependencies:

bash
pip freeze > requirements.txt

Install from requirements:

bash
pip install -r requirements.txt

Step 6 — Deactivate the environment

bash
deactivate

Install 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 -y

Create a venv with the new version:

bash
python3.12 -m venv venv312

Using pip globally (not recommended)

If you must install packages globally:

bash
pip install --break-system-packages package_name

Tip

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?