Cloud VPS from $1.98/mo · pay with Bitcoin, Ethereum, Monero, USDT or Solana · paid in crypto
DevOps

Upgrade, pin and roll back Python packages on a server

Upgrade, pin, downgrade and roll back Python packages on a server: pip inside a venv, hashed lock files, pip-audit and versioned environments you can swap.

CloudSat teamNovember 18, 20267 min read
Abstract illustration of stacked package boxes on a timeline, with a glowing arrow looping back from the newest box to an older one
On this pageTwo files with different jobs

Installing an application's packages happens once. Changing them happens for as long as the application lives: a security advisory lands, a library drops support for your Python version, or a new release of one dependency quietly pulls three others along with it. This guide covers that second part, inside an application's own virtual environment on a Linux server: seeing what an upgrade will move before it moves, pinning and downgrading, removing packages cleanly, locking with hashes, auditing, and rolling back in seconds when a release goes wrong.

The examples assume the app lives in /srv/app and runs from /srv/app/.venv. Never point any of these commands at the distribution's own Python.

Two files with different jobs

Most dependency trouble comes from keeping one file that tries to do two jobs. Keep a short file that says what you want, and a generated file that says exactly what you get:

File Written by Contains Commit it
requirements.in or pyproject.toml You Direct dependencies, with ranges Yes
requirements.txt (compiled) pip-compile or uv pip compile Every package pinned with ==, optionally with hashes Yes
uv.lock uv lock uv's cross-platform lock for a project Yes
pylock.toml uv, or the experimental pip lock The PEP 751 standard lock format Yes
Output of pip freeze pip Whatever is installed right now Only as a record

pip freeze describes the current state, including anything someone installed by hand last spring. It carries no hashes and no record of why a package is there. Use it to compare two servers, not to build a third.

Upgrade pip inside the environment

Call pip through the environment's own interpreter. That way the right pip runs, against the right site-packages, whatever PATH says:

cd /srv/app
.venv/bin/python -m pip install --upgrade pip
.venv/bin/python -m pip --version

pip ships a release every few months (26.3 arrived on November 2, 2026), and newer releases resolve conflicts faster and understand newer lock formats. Upgrading pip inside a virtual environment touches nothing outside it.

See what an upgrade will move before it moves

Start with the list of packages that have newer releases:

.venv/bin/python -m pip list --outdated

Then ask pip what one upgrade would change, without installing anything. --report - prints a JSON report to stdout, and --quiet keeps log lines out of it:

.venv/bin/python -m pip install --upgrade --dry-run --quiet --report - requests \
  | python3 -c 'import json,sys; [print(i["metadata"]["name"], i["metadata"]["version"]) for i in json.load(sys.stdin)["install"]]'

Every line is a package that would be installed or replaced. By default pip uses the only-if-needed upgrade strategy: dependencies move only when the installed version no longer satisfies the new release. --upgrade-strategy eager upgrades every dependency as well, which is rarely what you want on a production server.

Pin, downgrade and remove

Pinning and downgrading use the same syntax. Name the exact version and pip replaces whatever is installed:

.venv/bin/python -m pip install "requests==2.32.3"
.venv/bin/python -m pip check

pip check verifies that installed packages have compatible dependencies and exits with status 1 when they do not, so run it after every manual change. A downgrade that leaves another package needing a newer version shows up there, not at the first request in production.

Removal is where environments rot. pip uninstall -y somepackage removes that package only; the dependencies it pulled in stay behind, and pip check does not report them because nothing is broken. The clean way to remove a dependency is to delete it from requirements.in, regenerate the lock, and make the environment match the lock exactly with uv pip sync requirements.txt or pip-sync, both of which uninstall anything the lock does not list.

Lock with hashes

A compiled requirements file pins the whole tree. With uv:

uv pip compile requirements.in -o requirements.txt --generate-hashes
# later: move one package forward, keep everything else where it is
uv pip compile requirements.in -o requirements.txt --generate-hashes --upgrade-package django

pip-tools (BSD-3-Clause) does the same with pip-compile --generate-hashes requirements.in and pip-compile --upgrade-package django. Projects managed by uv use uv lock --upgrade-package django, then uv sync --locked on the server, which refuses to run if uv.lock is out of date.

On the server, install in hash-checking mode:

.venv/bin/python -m pip install --require-hashes -r requirements.txt

In this mode every requirement must be pinned with == (or to a URL or path), and every dependency must carry a hash. A single --hash anywhere in the file switches the mode on globally, so a half-hashed file fails loudly instead of installing unverified packages. If a mirror or a compromised index serves a different file under the same version, the hash check rejects it.

pylock.toml: the standard lock format

PEP 751 defined a standard lock file, pylock.toml, and reached Final status in March 2025. Support is arriving step by step:

  • uv pip compile requirements.in -o pylock.toml writes it (uv infers the format from the extension), and uv export --format pylock.toml converts an existing uv.lock.
  • pip lock has existed since pip 25.1 and is still experimental; its output is only guaranteed to be valid for the current Python version and platform.
  • pip 26.1 added experimental support for pip install -r pylock.toml.

Keep a compiled requirements.txt as the deployment input for now, and try pylock.toml on a staging server first.

A cooldown for new releases

pip 26.0 added --uploaded-prior-to, which only considers packages uploaded before a given time, and pip 26.1 lets it take a duration such as P3D. Installing with --uploaded-prior-to P3D skips anything published in the last three days, which keeps a freshly hijacked release off your server while the community notices it. It only works when the index publishes upload times.

Audit what is installed

pip-audit, maintained under the Python Packaging Authority and licensed under Apache-2.0, checks requirements files and environments against the PyPI and OSV vulnerability data. Run it from a throwaway tool environment so it never mixes with the app's packages:

uvx pip-audit -r requirements.txt

It exits with a non-zero status when it finds a known vulnerability, which makes it easy to wire into CI or a weekly timer that emails you. Its --fix option upgrades vulnerable packages in place; on a server, prefer recompiling the lock with --upgrade-package and deploying it like any other change.

Roll back by swapping environments

Upgrading in place means a bad release leaves you rebuilding the old state under pressure. Build each environment in its own directory instead, and point .venv at the current one with a symlink:

APP=/srv/app
NEW="$APP/envs/$(date +%Y%m%d-%H%M)"

python3 -m venv "$NEW"
"$NEW/bin/python" -m pip install --upgrade pip
"$NEW/bin/python" -m pip install --require-hashes -r "$APP/requirements.txt"
"$NEW/bin/python" -m pip check
cp "$APP/requirements.txt" "$NEW/requirements.lock.txt"

# switch: rename over the old link in one step, then restart
ln -s "$NEW" "$APP/.venv.next"
mv -T "$APP/.venv.next" "$APP/.venv"
sudo systemctl restart app

The first time, move the existing .venv directory into envs/ and create a .venv link that points at it, because mv -T cannot replace a real directory with a link. After that, the service file keeps pointing at /srv/app/.venv/bin/python and never changes. To roll back, point the link at the previous directory and restart:

ls -1 /srv/app/envs
ln -s /srv/app/envs/20261101-0930 /srv/app/.venv.next
mv -T /srv/app/.venv.next /srv/app/.venv
sudo systemctl restart app

Keep the last two or three environments and delete older ones. Each holds a full copy of its packages, but pip's wheel cache makes rebuilding fast. If the release also changed application code, roll the code back in the same step, with the same pattern of versioned directories.

Snapshots are the coarse version of the same safety net. On a CloudSat Python server, take one before a large dependency upgrade, and a bad release costs a restore instead of an evening.

FAQ

Is pip freeze good enough as a lock file?

For a hobby project, maybe. For a server, no: it has no hashes, it records leftovers from manual installs, and it cannot tell direct dependencies from transitive ones. Compile a lock from a short input file instead, and keep pip freeze output as a snapshot for comparisons.

Should I upgrade packages in place or rebuild the environment?

Rebuild for anything beyond a single patch release. A fresh environment built from the lock file proves the lock is complete, and the old environment stays available for an instant rollback.

Can pip install from uv.lock?

No. uv.lock is uv's own format. Export it with uv export --format requirements.txt or uv export --format pylock.toml, or use uv sync --locked on the server.

I upgraded in place and have no old environment. How do I go back?

Recover the previous lock from version control, for example git show HEAD~1:requirements.txt > /tmp/requirements.txt, build a new environment from it with --require-hashes, and switch to it. This is why the lock file belongs in Git next to the code.

Run it on CloudSat. Cloud VPS on AMD EPYC and NVMe from $1.98/mo in 13 regions, delivered in under 5 minutes and paid in crypto. See plans or deploy a server.