Sub topic 7
Verifying packages with hashes
This chapter assumes you can use pip and a virtual environment, and that you have seen a lock file with pinned versions. Version pins say which release you want. Hashes say which exact bytes you accept. The chapter covers what a hash proves, how pip enforces it, and the errors you will meet in ML projects.
Why version pins are not enough
A pin such as numpy==1.26.4 names a release. It does not identify the file you download. Three things can make the same pin produce different bytes:
- A package index serves several files for one release: a source distribution and one wheel per platform and Python version. You get a different file on Linux x86-64 than on macOS arm64.
- A maintainer can delete a file and upload a replacement under the same name on a private or mirrored index.
- A compromised index, mirror, or proxy can serve altered content under a legitimate name and version.
A hash closes these gaps. If the SHA-256 digest of the downloaded file differs from the digest in your lock file, the install stops.
What a hash is
A hash function turns a file into a short fixed-length digest. Changing a single byte of the file produces a completely different digest. SHA-256 is the algorithm used in Python lock files, and its digests are 64 hexadecimal characters.
You can compute one yourself:
sha256sum numpy-1.26.4-cp311-cp311-manylinux_2_17_x86_64.manylinux2014_x86_64.whlPip uses the same algorithm when it checks a download. A hash proves integrity: the file is the one you recorded. It does not prove the file is safe. If you generated the lock file from a malicious package, the lock file records the malicious hash faithfully. Hashes protect you from changes after the moment you locked.
Generating hashes
The pip-compile command from pip-tools writes hashes when you pass --generate-hashes. The input file lists only direct dependencies.
# requirements.in lists only the direct dependencies, for example:
# numpy
# scikit-learn
# torch
pip install pip-tools
pip-compile --generate-hashes -o requirements.lock requirements.in
pip install --require-hashes -r requirements.lockThe output has one entry per package, with one --hash line per acceptable file:
numpy==1.26.4 \
--hash=sha256:0000000000000000000000000000000000000000000000000000000000000000 \
--hash=sha256:1111111111111111111111111111111111111111111111111111111111111111
# via
# -r requirements.in
# scikit-learnThe digests above are placeholders. Real ones come from your own run.
Notice that a single package lists several hashes. pip-compile records the hash of every file the index offers for that version, so the same lock file works on different platforms. At install time pip accepts a download if it matches any of the listed hashes.
With uv, the lock file already contains hashes. You do not need an extra flag.
uv lock
uv sync --lockeduv lock reads pyproject.toml and writes uv.lock, which records a hash for each file. uv sync --locked installs from that file and fails if uv.lock is out of date with pyproject.toml.
How pip enforces hashes
Pip has two related behaviours.
- If any requirement in the file has a
--hashoption, pip switches to hash-checking mode automatically. - If you pass
--require-hashes, pip enters hash-checking mode whether or not hashes are present, and it rejects anything that does not meet the rules.
In hash-checking mode pip applies these rules:
- Every requirement must be pinned to an exact version with
==. - Every requirement must have at least one hash.
- Pip does not resolve dependencies. If a package needs something that is not in the file, the install fails instead of fetching it.
- Each download must match one of the listed hashes.
The third rule is what makes the mode strict. The lock file must contain the complete tree, and nothing is added silently. This is why you generate the file with a resolver and never write it by hand.
The diagram below shows the install decision for each package.
Hashes in a Dockerfile
The Dockerfile from the container chapters uses the same flag. A failed hash check fails the build, so a changed package never reaches the image.
# In a real project, pin the base image by digest: python:3.11-slim@sha256:<digest>
FROM python:3.11-slim
WORKDIR /app
# Copy only the lock file first so this layer is reused until dependencies change
COPY requirements.lock .
RUN pip install --no-cache-dir --require-hashes -r requirements.lock
# Application code changes often, so copy it last
COPY src/ ./src/
CMD ["python", "-m", "src.train"]Common errors
Hash mismatch
Pip prints the expected and the actual digest:
ERROR: THESE PACKAGES DO NOT MATCH THE HASHES FROM THE REQUIREMENTS FILE.
numpy==1.26.4 from https://...whl:
Expected sha256 0000...
Got aaaa...Causes, from most to least common:
- The lock file was generated on one platform and the index offers a file for your platform that was not recorded, for example a new wheel uploaded later.
- A corporate proxy or mirror rewrites files.
- A download was truncated. Retry once before investigating.
- The file was tampered with.
Do not remove the hash to make the error go away. Regenerate the lock file, review the diff, and find out why the digest changed.
Requirements must be pinned with ==
Pip reports that a requirement is not pinned. This usually means someone edited the lock file by hand, or installed requirements.in instead of requirements.lock.
Missing transitive dependency
Pip reports that a package is not in the requirements file. The lock file is incomplete, often because it was produced with --no-deps or was edited. Regenerate it.
Direct URL and local packages
Hash checking supports packages from an index and archives referenced by URL with a hash. It does not work well with editable installs (-e .) or local directories, which have no stable file to hash. Install your own project separately, after the locked dependencies:
pip install --require-hashes -r requirements.lock
pip install --no-deps -e .ML-specific considerations
PyTorch and CUDA wheels
PyTorch publishes CUDA builds on its own index, not on PyPI. Pass the extra index when you compile, so the hashes come from the files you will actually install:
pip-compile --generate-hashes \
--extra-index-url https://download.pytorch.org/whl/cu121 \
-o requirements.lock requirements.inInstall with the same index, or pip will not find the build with the +cu121 local version tag. Check that the locked version string includes the tag you expect.
Large wheels
CUDA-enabled wheels are hundreds of megabytes. The hash check adds little time next to the download. The cost that matters is a download that fails and retries, so use a build cache or an internal mirror in CI.
Platform differences
A lock file generated on a laptop with macOS may omit Linux-only packages, such as the NVIDIA runtime wheels that PyTorch pulls in on Linux. Generate the lock file on the platform you deploy to, or use a tool that locks for several platforms, as uv lock does. Then verify by installing in the target container.
Trade-offs
| Choice | Benefit | Cost |
|---|---|---|
| Hashes for every package | Detects altered or replaced files | Lock file is longer; diffs are noisy |
| Hashes only for some packages | Smaller file | Pip's hash mode requires hashes on all, so this is not available |
| Single-platform lock file | Smaller, simple | Fails on other platforms |
| Multi-platform lock file | One file for all targets | Slower to generate, more hashes |
What hashes do not cover
- The Python interpreter, system libraries, and CUDA runtime. The next chapter covers these.
- The first lock. If a package was already malicious when you locked it, the hash preserves it.
- Pip itself and build tools. Pin the version of pip or uv you use.
- Source distributions that execute build code during install. Prefer wheels with
--only-binary :all:in the install command when you can, so no build scripts run:
pip install --require-hashes --only-binary :all: -r requirements.lockExercise
Your CI build installs with pip install --require-hashes -r requirements.lock and fails with "Hashes are required in --require-hashes mode, but they are missing from some requirements" for one package. A teammate suggests removing --require-hashes. What is the better fix, and why?
Show answer
The lock file has an entry without a hash, probably because it was edited by hand or generated without --generate-hashes. Regenerate it with pip-compile --generate-hashes -o requirements.lock requirements.in, review the diff, and commit it. Removing --require-hashes would let unverified files into the image, which defeats the purpose of the lock file.
Exercise
A lock file generated on a macOS laptop installs correctly there, but the Linux CI job fails with a hash error for a wheel. Give one likely cause and one way to prevent it.
Show answer
The lock file did not record the hash of the Linux wheel, because it was generated on a different platform, or the platform needs different packages. Generate the lock file on the target platform (for example inside the build container), or use a tool that locks for multiple platforms, such as uv lock.