# Should I commit Cargo.lock, and what does cargo build --locked do?
Source: https://ruststudio.org/community/t/104/should-i-commit-cargo-lock-and-what-does-cargo-build-locked-do
Author: Ruststudio Team (staff) | Published: 2026-12-05 | Updated: 2026-12-05 | Checked with: rustc 1.93.0, cargo 1.93.0, Windows 11
Category: Help and questions | Kind: FAQ | Tags: security, cargo, supply-chain, reproducibility | Views: 0

## Short answer
Commit Cargo.lock for applications and services so every build uses exactly the dependency versions you tested and audited. Use the --locked flag in CI and release builds so cargo fails instead of silently choosing newer versions. Update dependencies on purpose with cargo update and review the changes.

## Answer
## Answer
`Cargo.toml` says which versions are **acceptable** (`serde_json = "1"` means any 1.x). `Cargo.lock` records the **exact** versions that were chosen. Without a committed lock file, two builds of the same commit can use different dependency versions, and the one you audited is not the one you shipped.

The Cargo Book recommends committing `Cargo.lock` for executables and services. Libraries have traditionally left it out because their users resolve their own versions.

`--locked` makes cargo use the lock file as it is and **fail** if it would need changes:

```text
cargo build --locked
```

On a small test project with two dependencies (`regex` and `serde_json`) this build succeeded from its lock file. `cargo tree` then shows the full tree that the lock file pins, including crates you never named:

```text
demo v0.1.0
├── regex v1.13.1
│   ├── aho-corasick v1.1.5
│   │   └── memchr v2.8.3
│   ├── memchr v2.8.3
│   ├── regex-automata v0.4.19
│   │   ├── aho-corasick v1.1.5 (*)
│   │   ├── memchr v2.8.3
│   │   └── regex-syntax v0.8.12
│   └── regex-syntax v0.8.12
└── serde_json v1.0.154
    ├── itoa v1.0.19
    ├── memchr v2.8.3
    ├── serde_core v1.0.229
    └── zmij v1.0.23
```

## How to do it
1. Commit `Cargo.lock` for every application.
2. Build in CI and for releases with `cargo build --release --locked`.
3. Update on purpose: `cargo update` for everything, or `cargo update -p serde_json` for one crate. Read the diff of `Cargo.lock` in the pull request.
4. Run `cargo audit` or `cargo deny` against the lock file (see those threads).
5. `cargo tree -d` lists crates that appear in several versions, `cargo tree -i <crate>` shows why a crate is in the tree.

## Common mistakes
- Ignoring `.gitignore` templates that exclude `Cargo.lock`, for a project that is an application.
- Running `cargo update` right before a release without testing.
- Using version requirements such as `*`; cargo-deny's `wildcards` setting can forbid them.

## Related questions
### Does `--locked` check what the crates contain?
No. It fixes the versions, which makes builds repeatable. The checksums in the lock file make cargo detect a registry package that differs from the one that was locked, but they do not say that the package is trustworthy. For that, review dependencies (see the thread on reviewing a new dependency).

### Should libraries commit `Cargo.lock`?
It is not required for libraries, and consumers ignore it. Some projects commit it anyway to make their own CI repeatable.

## Sources
- The Cargo Book, "Cargo.toml vs Cargo.lock": https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
- The Cargo Book, `cargo build --locked`: https://doc.rust-lang.org/cargo/commands/cargo-build.html
- The Cargo Book, `cargo tree`: https://doc.rust-lang.org/cargo/commands/cargo-tree.html
