Skip to main content

How to Install the Exact Locked Versions with uv sync --locked

· 6 min read
Jagdish Kumawat
Founder @ Dewiride

A project's uv.lock file records the exact version of every package it uses. uv sync --locked installs exactly those versions, and refuses to run if the lock file no longer matches pyproject.toml. That's how a second computer, or a build server, gets the same environment you have. This guide shows both with uv 0.13.10 on Python 3.15.2.

Syntax​

Syntax
uv sync --locked
PartWhat it means
uv syncMakes the project's .venv match the lock file, creating it if needed
--lockedUses uv.lock as it is, and stops with an error if it's out of date with pyproject.toml

uv's locking and syncing guide says: "If the lockfile is not up-to-date, uv will raise an error instead of updating the lockfile."

The Example​

A small project called price-tags prints a price with thousands separators, using one package, humanize. Its uv.lock pins humanize 4.17.0. The guide rebuilds its environment from the lock file, then changes pyproject.toml by hand to see what --locked does.

Prerequisites​

  • uv, with a project that has a uv.lock file. This guide uses uv 0.13.10 with Python 3.15.2.

Step 1: See the Project Run​

The project was made with uv init --python 3.15 price-tags and uv add humanize, and its command prints a price:

Terminal
uv run price-tags
Output
Price: 1,234,567

The terminal after uv run price-tags: Price: 1,234,567.

Step 2: Rebuild the Environment from the Lock File​

Delete the environment, as if you had just cloned the project onto another computer:

Terminal
rm -rf .venv

Then rebuild it from uv.lock:

Terminal
uv sync --locked
Output
Using CPython 3.15.2 interpreter at: /Library/Frameworks/Python.framework/Versions/3.15/bin/python3.15
Creating virtual environment at: .venv
Resolved 2 packages in 4ms
Installed 2 packages in 7ms
+ humanize==4.17.0
+ price-tags==0.1.0 (from file:///Users/you/source/price-tags)

The terminal after uv sync --locked: uv created .venv and installed humanize 4.17.0 and the project itself.

uv created a new .venv and installed exactly the versions in the lock file: humanize==4.17.0, even if a newer humanize has come out since. The command works unchanged in PowerShell.

Step 3: See --locked Refuse a Lock File That's Out of Date​

Add a second package to pyproject.toml by hand, without running uv add, so the lock file no longer matches:

pyproject.toml (dependencies)
dependencies = [
"humanize>=4.17.0",
"tabulate>=0.9",
]

The dependencies list of pyproject.toml in a macOS-style code window, with humanize>=4.17.0 and the newly added tabulate>=0.9.

Sync again:

Terminal
uv sync --locked
Output
Resolved 3 packages in 362ms
error: The lockfile at `uv.lock` needs to be updated, but `--locked` was provided.
cause: Requirements for `price-tags==0.1.0` differ; added: tabulate>=0.9

hint: To update the lockfile, run `uv lock`.

The terminal after uv sync --locked with the stale lock: error, the lockfile at uv.lock needs to be updated, with the cause added: tabulate>=0.9 and the hint to run uv lock.

uv noticed the difference, named it in the cause line, and stopped without installing anything. Without --locked, uv sync would have updated the lock file and carried on. uv's guide says "Locking and syncing are automatic in uv", and --locked is how you turn the automatic locking off.

Step 4: Update the Lock File​

Terminal
uv lock
Output
Resolved 3 packages in 5ms
Added tabulate v0.10.0

uv lock resolved the new requirement and pinned tabulate 0.10.0. Now uv sync --locked succeeds:

Output
Resolved 3 packages in 4ms
Prepared 1 package in 262ms
Installed 1 package in 2ms
+ tabulate==0.10.0

The terminal after uv lock, which added tabulate v0.10.0, and uv sync --locked, which installed tabulate 0.10.0.

Commit uv.lock with pyproject.toml: uv's project layout guide says it "should be checked into version control, allowing for consistent and reproducible installations".

Common Mistakes​

MistakeWhat happensDo this instead
Editing pyproject.toml by hand and forgetting the lockuv sync --locked stops: The lockfile at uv.lock needs to be updatedRun uv lock, or use uv add, which updates both files
Leaving uv.lock out of GitEvery clone resolves its own versionsCommit it

Troubleshooting​

SymptomLikely causeFix
needs to be updated, but --locked was providedpyproject.toml changed after the last uv lockRun uv lock, check the change, and commit both files

FAQ​

What's the difference between --locked and --frozen?​

--frozen uses the lock file without checking it. With the same out-of-date lock, uv sync --frozen finished without an error and didn't install tabulate at all, because the lock file didn't list it. --locked stops instead, which is safer.

How do I only check the lock file, without installing?​

With uv lock --check. On the out-of-date lock it gave the same error, ending with but --check was provided.

Does uv sync remove packages that aren't in the lock?​

Yes. The guide says "uv sync performs "exact" syncing by default, which means it will remove any packages that are not selected for installation."

Conclusion​

uv sync --locked rebuilt .venv with exactly humanize 4.17.0 from uv.lock, and refused to run once pyproject.toml no longer matched the lock. Run uv lock after changing dependencies, commit both files, and use --locked wherever an environment must match.

Additional Resources​