Sub topic 11
Reproducible installs in CI
This chapter assumes you can create a uv project, add dependencies with uv add, and read a pyproject.toml. It also assumes you know that uv.lock records resolved versions. The goal is a CI job that installs the same packages on every run and fails loudly when the project files disagree with each other.
Why CI needs more than a lockfile
A lockfile only helps if CI uses it. Three things commonly make CI runs drift:
- CI resolves dependencies again instead of reading
uv.lock, so a new release on the package index changes the result. - A developer edits
pyproject.tomland forgets to commit the updateduv.lock. CI then installs something different from what the developer tested. - The CI runner uses a different Python version than the one the project pins.
uv gives you flags that turn each of these into an error instead of a silent change.
The three sync modes
uv sync makes the project environment match the lockfile. By default it can also update the lockfile if it is stale. That is convenient on a laptop and unsafe in CI. Two flags change this behavior.
| Command | Checks that `uv.lock` matches `pyproject.toml` | May rewrite `uv.lock` | Typical use |
|---|---|---|---|
| `uv sync` | Yes, and re-locks if needed | Yes | Local development |
| `uv sync --locked` | Yes | No, it fails with an error | CI verification |
| `uv sync --frozen` | No | No | Fast installs when the lockfile is trusted |
--locked asserts that the lockfile is up to date. If pyproject.toml has a dependency that uv.lock does not account for, the command exits with a non-zero status. This catches the forgotten-commit case.
--frozen skips that check. uv installs whatever the lockfile says, even if pyproject.toml has changed. Use it when another step already validated the lockfile, or when you need to install from a lockfile you deliberately do not want touched. It is the riskier option, because a stale lockfile passes without complaint.
Checking the lockfile separately
You can verify the lockfile without installing anything:
uv lock --checkThis resolves nothing new and exits non-zero if uv.lock would change. It is useful as a fast first CI step, before the slower install. In recent uv versions uv lock --locked is an equivalent spelling. Use whichever your installed version documents in uv lock --help.
Which groups to install
CI jobs rarely need the same packages. A test job needs the dev group, while a deployment build may need only runtime dependencies. By default uv sync installs the project's runtime dependencies and the dev group. You can control this:
# Runtime dependencies only, no development group
uv sync --locked --no-dev
# Only runtime dependencies, skipping every group
uv sync --locked --no-default-groups
# Add a specific group on top
uv sync --locked --group docsInstalling fewer packages makes the job faster and keeps test tooling out of production images.
Running code without re-syncing
After a sync, uv run would normally check the environment again and might update it. In CI you can pass the same flags to uv run so that command also refuses to change anything:
uv sync --locked
uv run --locked pytestuv run --frozen pytest skips the lockfile check and just runs. If you have already done uv sync --locked, either form works. Using --locked on uv run is a cheap second guard.
If you want to run a command in the existing environment with no sync at all, use uv run --no-sync. The environment must already be correct, since uv will not fix it.
Pinning the Python version
Reproducibility includes the interpreter. Two files control it:
.python-version, created byuv python pin, names the version uv should use for the project.requires-pythoninpyproject.tomlstates which versions the project supports. The lockfile is resolved against this range.
In CI, uv reads .python-version and downloads a matching interpreter if none is installed. That means the CI Python matches what developers use, with no separate setup step. To test several versions, override the pin per job:
uv sync --locked --python 3.13
uv run --python 3.13 pytestThe lockfile stays the same across these runs because it covers the whole requires-python range. Only the interpreter changes.
A complete GitHub Actions workflow
The workflow below installs uv, checks the lockfile, installs dependencies, and runs the tests. It uses the astral-sh/setup-uv action, which can also cache uv's download cache between runs.
name: tests
on:
push:
pull_request:
jobs:
test:
runs-on: ubuntu-latest
strategy:
matrix:
python-version: ["3.12", "3.13"]
steps:
- uses: actions/checkout@v4
- name: Install uv
uses: astral-sh/setup-uv@v5
with:
enable-cache: true
- name: Check that uv.lock is up to date
run: uv lock --check
- name: Install dependencies
run: uv sync --locked --python ${{ matrix.python-version }}
- name: Run tests
run: uv run --locked pytestPin the action to a specific version tag, as shown, so the workflow itself does not drift. For stricter setups, you can also pin the uv version the action installs through its version input.
How the pieces fit
Edge cases and common errors
The lockfile is stale. The error says the lockfile needs to be updated. The fix is to run uv lock locally and commit uv.lock. Do not "fix" it in CI by dropping --locked.
Platform differences. uv.lock is universal: it holds resolutions for multiple platforms and Python versions in one file. A macOS developer and a Linux runner therefore install from the same lockfile, though the exact wheels differ by platform. Dependencies with markers, such as pywin32; sys_platform == "win32", appear in the lockfile and are installed only where they apply.
Private indexes. If a dependency comes from a private index, the lockfile records the source. CI needs credentials at install time, usually through environment variables such as UV_INDEX_<NAME>_USERNAME and UV_INDEX_<NAME>_PASSWORD that match the index name in [tool.uv.sources]. Never write tokens into pyproject.toml.
Changing a dependency on a package index. A locked install pins versions and file hashes. If a release is yanked or removed from the index, the install can fail. That failure is a signal to relock, not a reason to avoid locking.
Intentional upgrades. Reproducibility does not mean never upgrading. Run uv lock --upgrade or uv lock --upgrade-package httpx on a schedule or in a dedicated pull request, review the uv.lock diff, and let CI verify it. Upgrades then become visible changes in version control rather than side effects of a CI run.
Docker builds. The same flags apply inside images. A common pattern copies pyproject.toml and uv.lock first, then runs uv sync --locked --no-install-project --no-dev so the dependency layer is cached, and finally copies the source and syncs again.
Choosing a mode
- Use
uv lock --checkoruv sync --lockedas the default in CI. They fail on drift. - Use
--frozenonly when speed matters and the lockfile has been verified elsewhere. - Use plain
uv synconly on developer machines. - Add
--no-devor--no-default-groupsto production builds.
Exercise
A developer adds rich to pyproject.toml by hand and pushes without running uv lock. Your CI step is uv sync --frozen, then uv run pytest. The tests import rich. What happens, and which change would make CI report the real problem?
Show answer
With --frozen, uv skips the check that the lockfile matches pyproject.toml, so it installs the old lockfile contents without rich. The tests then fail with an ImportError, which hides the real cause (a stale lockfile). Changing the step to uv sync --locked, or adding uv lock --check first, makes CI fail immediately with a message that uv.lock needs updating. The developer then runs uv lock and commits the result.
Exercise
Write the commands for a CI job that installs only runtime dependencies, fails if the lockfile is stale, and uses Python 3.13.
Show answer
uv sync --locked --no-dev --python 3.13. You can add uv lock --check before it as a separate fast check, though --locked already enforces the same condition.