LinuxCommandLibrary

uv-lock

Generate a lock file for Python dependencies

TLDR

Create or update the project's lockfile

$ uv lock
copy

Check if the lockfile is up-to-date without updating it
$ uv lock --check
copy

Assert that a lockfile exists without checking if it's current
$ uv lock --check-exists
copy

Preview what would be locked without writing the lockfile
$ uv lock --dry-run
copy

Lock a specific Python script instead of the current project
$ uv lock --script [path/to/script.py]
copy

Upgrade all packages to their latest compatible versions
$ uv lock --upgrade
copy

Upgrade only specific packages
$ uv lock --upgrade-package [package1] --upgrade-package [package2]
copy

SYNOPSIS

uv lock [OPTIONS]

PARAMETERS

--output <PATH>
    Specifies the path to write the generated lock file. The default is uv.lock.

--output-format <FORMAT>
    Sets the format for the lock file. Common values include uv (for uv.lock) or pip (for requirements.txt).

--python <VERSION_OR_PATH>
    Specifies the target Python interpreter version (e.g., 3.9) or path (e.g., /usr/bin/python3.10) for dependency resolution.

--dev
    Includes development dependencies, typically defined in the [project.optional-dependencies] section of pyproject.toml with a 'dev' extra.

--all-extras
    Includes all optional dependencies (extras) defined for the project in pyproject.toml during resolution.

--strict
    Fails the operation if any dependency cannot be resolved or if there are conflicting requirements.

--refresh
    Ignores the uv package cache and re-fetches all package metadata and wheels from the network.

DESCRIPTION

uv lock is a subcommand of the uv Python package manager, designed to create a deterministic lock file for project dependencies. It efficiently resolves all direct and transitive dependencies specified in pyproject.toml or requirements.txt files, pinning them to exact versions and hashes. This process ensures reproducible builds across different environments and over time, preventing unexpected dependency updates.

uv lock is exceptionally fast due to its Rust implementation and parallel network requests. It generates a uv.lock file by default, or can output to other formats like requirements.txt, making it compatible with various deployment workflows. It supports specifying target Python versions and including development dependencies, providing a robust solution for managing Python project environments.

CAVEATS

uv lock requires a pyproject.toml file with a [project] section or a requirements.txt file in the current directory to resolve dependencies. While exceptionally fast, initial runs might take longer as uv builds a local cache of package metadata and wheels.

The default uv.lock format is specific to uv and is not directly consumable by pip without using the --output-format pip option.

DETERMINISM AND REPRODUCIBILITY

uv lock creates a fully deterministic lock file, meaning every dependency (direct and transitive) is pinned to an exact version and, optionally, a cryptographic hash. This guarantees that installing from the lock file will always result in the exact same environment, which is crucial for CI/CD pipelines, team collaboration, and long-term project stability.

INTEGRATION WITH PYPROJECT.TOML

While it can process requirements.txt files, uv lock is designed to seamlessly integrate with pyproject.toml and PEP 621 metadata. It automatically detects project dependencies, optional groups (extras), and development dependencies, simplifying modern Python project setup and management.

HISTORY

uv was developed by Astral and officially announced in February 2024, aiming to be a faster, next-generation alternative to existing Python package management tools like pip and pip-tools. uv lock is a core component of uv's strategy for reproducible environments, providing a highly optimized and deterministic dependency resolution and locking mechanism from its inception. Its development focuses on speed and correctness, built in Rust.

SEE ALSO

uv(1), uv sync(1), uv install(1), pip(1), pip-tools(1)

Copied to clipboard