brewedbits.dev

Personal blog with guides and news on selfhosting, Linux and AI, from the Netherlands

nextDash nextdash --aboutYour bookmarks. Your terminal. checks your links knows your Docker host one container, no cloudLive demo →nextdash.cc ↗

Tip: pip require-hashes refuses a download that doesn't match

Pip
~/tips/tip-pip-require-hashes

python3 -m pip download requests==X.Y.Z --no-deps -d ./pkgs

Pin each package to a file hash so a swapped download fails.

A version number says which release you want. A hash says which exact file, and pip will refuse anything else.

Download a package and ask for its hash:

python3 -m pip download requests==X.Y.Z --no-deps -d ./pkgs
python3 -m pip hash ./pkgs/*.whl

Put the result in your requirements file, on the same logical line as the pinned version:

requests==X.Y.Z \
    --hash=sha256:<paste the value here>

Then install in strict mode:

python3 -m pip install --require-hashes -r requirements.txt

If the file differs, pip stops with a message that the packages do not match the hashes, and shows the expected and received values. I like that failure: it is loud and nothing gets installed.

The strictness covers everything. Every dependency, direct or not, has to be pinned with == and carry a hash, or the install is refused. Writing that by hand gets old quickly, so a tool such as pip-tools, which can generate the hashes, is worth a look. Also list the hashes for each platform you deploy to, because the wheel for Linux and the one for macOS can be different files.

Further reading: pip secure installs, pip hash

More Pip tips urllib3<2 python3 -m pip download -r requirements.txt -d ./wheels python3 -m pip config set global.timeout 60 python3 -m pip index versions requests python3 -m pip install --only-binary=:all: numpy All Pip tips (9) All topics

Link