Filters
How to search
  • Plain words find names, texts and examples.
  • "two words" finds the exact phrase.
  • --name or -x finds an option.
  • #word looks only inside commands.
  • @grep finds lessons and labs that use grep.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Put ! before a word to leave it out. OR between words means one of them.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search

pip

Install Python packages for scripts and services — best inside a virtual environment, with pinned versions.

Level
DevOps tool
Group
Build tools and languages
Needs root
No
Package
python3-pip
Also called
pip3

What it does

pip installs packages from PyPI (or a private index). A virtual environment (python3 -m venv .venv) keeps the packages of one project away from the system Python. requirements.txt lists the packages; with == versions the install is the same every time.

Many DevOps tools are Python (Ansible, the AWS CLI v1, many scripts), so pip is part of daily work.

How to write it

pip install [OPTIONS] PACKAGE ... | -r requirements.txt

Main parts

Commands

install PKG / -r FILE
Install packages / from a requirements file.
uninstall PKG
Remove a package.
freeze
Print installed packages with exact versions.
list [--outdated]
List installed (or old) packages.
show PKG
Details: version, location, requires.
download PKG -d DIR
Download packages without installing (offline installs).
check
Check that dependencies fit together.
cache purge
Empty the pip cache.

Options 9

Most used

Options

-r FILE
Install from a requirements file.
-U, --upgrade
Upgrade to the newest version.
--user
Install for this user only.
--no-cache-dir
Do not keep a cache (smaller Docker images).
-i, --index-url URL
Use another package index (private mirror).
--require-hashes
Install only files whose hash is in the requirements file.
-c FILE
Constraints: limit versions without installing.
-e PATH
Editable install of a local project.

Examples 11

$ run as a normal user# run as root (or with sudo)

  1. 1A virtual environment for a project

    python3 -m venv .venv
    source .venv/bin/activate
    pip install -r requirements.txt
  2. 2Install a pinned version

    pip install 'requests==2.32.3'
  3. 3Save the exact versions

    pip freeze > requirements.txt
  4. 4Small Python image

    cat Dockerfile
    FROM python:3.12-slim
    WORKDIR /app
    COPY requirements.txt .
    RUN pip install --no-cache-dir -r requirements.txt
    COPY . .
    USER 1000
    CMD ["python", "app.py"]
  5. 5Install Ansible for one user

    python3 -m pip install --user ansible-core
  6. 6What is out of date?

    pip list --outdated
  7. 7Use a private mirror

    pip install -i https://nexus.example.com/repository/pypi/simple -r requirements.txt
  8. 8Hash-checked install (supply chain)

    pip install --require-hashes -r requirements.lock
  9. 9Download for an offline server, then install there

    pip download -r requirements.txt -d wheels/
    pip install --no-index --find-links wheels/ -r requirements.txt
  10. 10Where is a package and what does it need?

    pip show boto3
  11. 11Check for broken dependencies

    pip check

Watch out

  • Do not use sudo pip install on the system Python; it can break OS tools (dnf, apt). Use a venv or --user.
  • On new distros pip refuses system installs ("externally-managed-environment") — use a venv.
  • Unpinned packages change under your feet; pin with == or a lock file.

Files

requirements.txt
The package list of a project.
pip.conf (~/.config/pip/pip.conf)
Index URL and other settings.

Tips to remember

  • python3 -m pip makes sure you use the pip of the right Python.
  • pip-audit checks installed packages for known CVEs.

Exit codes

0OK.
1Error.