Safeguard
Developer Tools•

How do I pin pip requirements with hashes, step by step?

Step by step guide to pip hash-checking mode: generate pinned hashes, install with require-hashes, handle platforms, and know what hashes do not prove.

Safeguard Security Team
3 min read

Pin each requirement with a double equals version and one or more sha256 hashes, then install with hash checking on. pip then refuses any file whose hash is not in the list, which defeats a swapped package or a tampered download. Hashes prove identity, not trustworthiness.

How hash-checking mode works

The pip documentation describes the rules:

  • Hashes are attached to each requirement with a hash option in the requirements file.
  • The mode is all or nothing. Adding a hash to any requirement turns it on for all of them.
  • Every requirement and every dependency needs a hash and must be pinned with an exact version, a URL or a file path.
  • The sha256 algorithm is recommended. Weaker ones are excluded.

Steps

  1. Start from a working environment with the versions you have tested.
  2. Generate a fully pinned file with hashes. A lock tool such as pip-tools can do this. We did not fetch its documentation, so follow its own instructions. The output looks like this:
requests==2.32.3 \
    --hash=sha256:<hash-of-wheel> \
    --hash=sha256:<hash-of-sdist>
  1. List several hashes per package when you need to support more than one platform, or both wheel and source builds. The documentation allows multiple hashes for this reason.
  2. Force the mode on in deploy scripts so a missing hash fails loudly:
pip install --require-hashes -r requirements.txt
  1. Add wheels only to avoid building from source. See does pip run code from packages.
  2. Regenerate on purpose. Updating a dependency is a reviewed change to the file, not something that happens silently.

A trap with the PyPI link fragment

The download link on PyPI includes a sha256 fragment. The pip documentation says this only checks against corruption and does not count as protection against tampering, and it does not satisfy the hash requirement. The hash has to come from your requirements file.

What hashes do not prove

A hash proves that the file is the same one you recorded. If you recorded a malicious file, hash checking installs it faithfully. So pair hashes with review of what you add. Use how to check a package before install for the habits that carry across ecosystems, and keep private names on one controlled index. See why extra-index-url is unsafe.

Keep it maintainable

Hash files get long and rarely get read. Review the diff when they change: new package names matter more than new hash lines. Automate the first pass.

Click through it at your own pace, or open it full screen. Look at how direct and transitive packages are listed with versions.

Quick check

Do I need hashes for transitive dependencies?

Yes. The documentation says every requirement and every dependency needs a hash once the mode is active.

Do hashes replace version pinning?

No. The mode requires exact pins as well as hashes.

Safeguard does SBOM and dependency scanning for Python, and a free scan lists every pinned package.

Sources

Last reviewed: 19 October 2026.

Never miss an update

Weekly insights on software supply chain security, delivered to your inbox.

Self-healing security runs on Safeguard.

Your first fix PR is minutes away.

No sales call required, even your agent can complete the purchase over MCP.