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.txtMain parts
Commands
installPKG/-rFILE- Install packages / from a requirements file.
uninstallPKG- Remove a package.
freeze- Print installed packages with exact versions.
list[--outdated]- List installed (or old) packages.
showPKG- Details: version, location, requires.
downloadPKG-dDIR- Download packages without installing (offline installs).
check- Check that dependencies fit together.
cachepurge- Empty the pip cache.
Options 9
Most used
Options
Nothing matches this filter.
Examples 11
$ run as a normal user# run as root (or with sudo)
1A virtual environment for a project
python3 -m venv .venv source .venv/bin/activate pip install -r requirements.txt2Install a pinned version
pip install 'requests==2.32.3'3Save the exact versions
pip freeze > requirements.txt4Small 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"]5Install Ansible for one user
python3 -m pip install --user ansible-core6What is out of date?
pip list --outdated7Use a private mirror
pip install -i https://nexus.example.com/repository/pypi/simple -r requirements.txt8Hash-checked install (supply chain)
pip install --require-hashes -r requirements.lock9Download for an offline server, then install there
pip download -r requirements.txt -d wheels/ pip install --no-index --find-links wheels/ -r requirements.txt10Where is a package and what does it need?
pip show boto311Check for broken dependencies
pip check
Nothing matches this filter.
Watch out
- Do not use
sudo pip installon 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 pipmakes sure you use the pip of the right Python.pip-auditchecks installed packages for known CVEs.
Exit codes
0 | OK. |
1 | Error. |