Sub topic 11
Reproducible installs in CI
This chapter assumes you can create a uv project, you know that uv add records dependencies in pyproject.toml, and you have seen uv.lock in a project directory. A CI job should install the same package versions you tested locally. The lockfile makes that possible.
Why CI needs the lockfile
pyproject.toml declares ranges such as httpx>=0.27. Without a lockfile, a CI run next month could pick a newer httpx than your laptop used, and a test could fail for reasons unrelated to your change.
uv.lock records the exact version of every direct and transitive dependency, with hashes. If CI installs from the lockfile, it gets the same set each run.
Locked and frozen installs
Two flags control how uv sync treats the lockfile.
| Command | Behavior | Use it when |
|---|---|---|
| `uv sync --locked` | Fails if `uv.lock` does not match `pyproject.toml` | You want CI to catch a stale lockfile |
| `uv sync --frozen` | Installs from `uv.lock` without checking it against `pyproject.toml` | You trust the lockfile and want no re-resolution |
| `uv sync` | Updates `uv.lock` if needed, then installs | Local development |
Prefer --locked in CI. If someone edits pyproject.toml and forgets to commit the new uv.lock, the job fails with a clear error instead of passing on a lockfile nobody reviewed.
A GitHub Actions example
This workflow installs uv, installs the project from the lockfile, and runs the tests.
name: tests
on: [push, pull_request]
jobs:
test:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- uses: astral-sh/setup-uv@v5
with:
enable-cache: true
- name: Install dependencies
run: uv sync --locked
- name: Run tests
run: uv run pytestuv run uses the project environment that uv sync created. By default, uv sync also installs the dev dependency group, so pytest is available. To install only runtime dependencies, for example in a deployment image, add --no-dev.
The Python version comes from .python-version and requires-python. If that interpreter is not on the runner, uv downloads it.
Common errors
- The lockfile is out of date.
uv sync --lockedreports that the lockfile needs to be updated. Runuv locklocally, review the diff, and commituv.lock. uv.lockis in.gitignore. CI then has no lockfile to follow. Commit it for applications and for libraries that you test in CI.- Using
--frozeneverywhere. It never notices a stale lockfile, so a missing dependency can slip through. Keep it for steps where you have already validated the lock, such as a Docker build after the tests passed. - Upgrades happen by accident. Plain
uv syncanduv lockkeep existing locked versions unless you ask otherwise. Upgrade deliberately withuv lock --upgradeoruv lock --upgrade-package httpx, then commit the new lockfile.
Exercise
Your CI job runs uv sync --frozen. A teammate adds rich to pyproject.toml but does not commit an updated uv.lock. What happens, and which command would catch the problem?
Show answer
The job does not re-resolve or compare the files, so it installs the old lockfile contents without rich, and the tests may fail later with an import error or pass without it. uv sync --locked would fail immediately because uv.lock no longer matches pyproject.toml.