$ run as a normal user# run as root (or with sudo)
Know it before you install
The system has its own Python that the OS tools need. Never change its packages. Make a virtual environment (venv) for each project: a private folder with its own Python packages.
| Item | Value |
|---|---|
| Programs | python3, pip (inside a venv), python3 -m venv |
| RHEL packages | python3, python3-pip; newer versions: python3.12, python3.12-pip |
| Ubuntu packages | python3, python3-pip, python3-venv, pipx |
| pip settings | ~/.config/pip/pip.conf (user), /etc/pip.conf (system) |
| A project's packages | requirements.txt (or pyproject.toml) |
| Network | pypi.org and files.pythonhosted.org (443) |
Versions: RHEL 9 has Python 3.9 as python3 plus 3.11 and 3.12 as extra packages; RHEL 10 has 3.12. Ubuntu 24.04 has 3.12.
Install on RHEL, Rocky, Alma and Fedora
dnf -y install python3 python3-pip
python3 --version
Python 3.9.21
dnf -y install python3.12 python3.12-pip
python3.12 --version
Python 3.12.9At work
Use a newer version for a project with python3.12 -m venv .venv. Do not change what python3 points to — dnf and other OS tools need the default version.
Install on Ubuntu and Debian
apt-get update
apt-get install -y python3 python3-pip python3-venv pipx
python3 --version
Python 3.12.3First configuration: one venv per project
This is the professional way. Make the venv once, turn it on, and install packages only inside it:
mkdir ~/api-tool && cd ~/api-tool
python3 -m venv .venv
source .venv/bin/activate
python -m pip install --upgrade pip
pip install requests pyyaml
pip freeze > requirements.txt
deactivateOn another machine (or in CI), make the same environment from the file:
Settings to make first:
- Add
.venv/to.gitignore— the venv is never committed, onlyrequirements.txt. - Pin versions in
requirements.txt(requests==2.32.3), so every build is the same. - Command-line tools (not libraries) go in
pipx: every tool gets its own venv, and the command is on yourPATH.
pipx ensurepath
pipx install ansible-core
pipx install pre-commit
pipx listA company package mirror or proxy is set in pip.conf:
[global]
index-url = https://nexus.example.com/repository/pypi/simple
timeout = 30Daily use: the commands and the real uses
source .venv/bin/activate
pip install -r requirements.txt
pip list --outdated
pip show requests
python script.py --help
python -m json.tool data.json
python -m http.server 8000 --bind 127.0.0.1
python -c 'import ssl; print(ssl.OPENSSL_VERSION)'Real uses at work:
- Scripts that call APIs (cloud, Jira, GitHub) with
requestsorboto3. - Automation: Ansible modules and custom tools; log parsing; reports to CSV and JSON.
- Small web services and health checks; glue code in CI pipelines.
Make it safe
Security checklist:
- Never
sudo pip install— it can break OS tools. Use a venv orpipx. - Pin versions; for strong builds use hashes:
pip install --require-hashes -r requirements.txt. - Check for known CVEs:
pipx run pip-audit -r requirements.txt. - Watch the package name: fake packages with near names (typosquatting) steal secrets.
- Keep secrets in environment variables or a secret manager, not in the script.
- Do not run scripts as root unless they really need it.
Update and remove
Fix common problems
| Problem | Cause | Fix |
|---|---|---|
error: externally-managed-environment | Ubuntu 23.04+ / Debian 12 block pip on the system Python (PEP 668). | Use a venv (python3 -m venv .venv) or pipx for tools. |
The virtual environment was not created successfully because ensurepip is not available | python3-venv is missing (Ubuntu). | apt-get install -y python3-venv. |
pip: command not found | pip is not on the PATH. | Use python3 -m pip, or activate the venv. |
ModuleNotFoundError | The package is in another venv, or the venv is not active. | source .venv/bin/activate; which python. |
SSL: CERTIFICATE_VERIFY_FAILED behind a company proxy | The proxy's CA is unknown. | pip config set global.cert /etc/pki/ca-trust/source/anchors/corp.pem (and REQUESTS_CA_BUNDLE). |
| Wrong Python version | Many versions installed. | Make the venv with the exact one: python3.12 -m venv .venv. |
Next steps
- Quick how-to: Create a Python virtual environment.
- Command page: pip — every option and example.
- Learn Python for DevOps: Python summary — Files, Web APIs, A real project.
- Practise: Lab: a full Python project.
- Python in DevOps pipelines: pip — Python packages.
1Why not sudo pip install on a server?
It changes the system Python that the OS tools need, and can break them. Use a venv or pipx.
2What goes into Git: the .venv folder or requirements.txt?
Only requirements.txt. The venv is made again from it on each machine.