Lockfiles
A lockfile records the exact result of resolving a graph: every package with its version and recipe revision. Install with the lockfile and you get the same graph tomorrow, on another machine and in CI, even if newer versions or revisions have been published since. For applications, a committed lockfile is the difference between "it built yesterday" and "it builds".
What can change without a lockfile
Even with exact versions, a graph can drift:
- Version ranges resolve to whatever is newest at the time.
- Recipe revisions: the same
fmt/12.1.0can get a new recipe revision on the remote (a packaging fix). Conan picks the latest revision by default. - Transitive requirements of your dependencies can change when their recipes get new revisions.
A Conan 2 lockfile pins all three. It does not pin package IDs or binaries; those still follow from the profile.
Creating and reading a lockfile
mkdir -p app && cd app
cat > conanfile.txt <<'TXT'
[requires]
spdlog/[>=1.15 <2]
[test_requires]
catch2/[^3.0]
TXT
conan lock create . 2>&1 | grep -E 'Resolved|: |Generated'
cat conan.lock conanfile.txt: /home/learner/app/conanfile.txt
Resolved version ranges
catch2/[^3.0]: catch2/3.16.0
cmake/[>=3.16]: cmake/4.4.4
spdlog/[>=1.15 <2]: spdlog/1.17.0
Generated lockfile: /home/learner/app/conan.lock
{
"version": "0.5",
"requires": [
"spdlog/1.17.0#bcbaaf7147bda6ad24ffbd1ac3d7142c%1767636069.964",
"fmt/12.1.0#6baf0fb8351783472b94ee6e36232391%1785754553.994",
"catch2/3.16.0#95f400ca2fba9f0c5ce5aea6e6fe0055%1788523836.458"
],
"build_requires": [
"cmake/4.4.4#fec9c7048fae91e03c31e59977cd9c12%1791276141.315"
],
"python_requires": [],
"config_requires": []
}
Each entry is name/version#recipe_revision%timestamp. The file is JSON and small, made to be committed and reviewed in pull requests. Note the sections: requires for host libraries, build_requires for tools (CMake appears because Catch2's recipe needs it), plus python_requires and config_requires.
The lockfile in action
Now the classic scenario: you lock, then a newer version appears. Here a small library, mylib, has 1.0.0 and 1.1.0 when we lock, and 1.2.0 is published afterwards:
conan remote disable conancenter > /dev/null
mkdir -p mylib app
cat > mylib/conanfile.py <<'PY'
from conan import ConanFile
class MyLib(ConanFile):
name = "mylib"
package_type = "header-library"
PY
conan export mylib --version=1.0.0 > /dev/null 2>&1
conan export mylib --version=1.1.0 > /dev/null 2>&1
cd app
printf '[requires]\nmylib/[>=1.0 <2]\n' > conanfile.txt
conan lock create . > /dev/null 2>&1
grep -o '"mylib/[0-9.]*' conan.lock
echo "--- mylib 1.2.0 is released"
conan export ../mylib --version=1.2.0 > /dev/null 2>&1
echo -n "without lockfile: "
conan graph info . --lockfile="" --filter=label 2>&1 | grep -o 'label: mylib/.*'
echo -n "with lockfile: "
conan graph info . --lockfile=conan.lock --filter=label 2>&1 | grep -o 'label: mylib/.*'"mylib/1.1.0
--- mylib 1.2.0 is released
without lockfile: label: mylib/1.2.0
with lockfile: label: mylib/1.1.0
Note: if a file named
conan.locksits next to the conanfile, Conan uses it automatically; that's why the session passes--lockfile=""to show the unlocked result. Passing--lockfile=conan.lockexplicitly makes the intent obvious in scripts.
Updating a lockfile on purpose
Locked doesn't mean frozen forever. Update deliberately, in a reviewable commit:
conan lock create . --update # re-resolve everything against the remotes
conan lock create . --lockfile=conan.lock --lockfile-out=conan.lock # extend after adding a requirement
conan lock add --requires=zlib/1.3.1 # add one pinned reference by hand
conan lock remove --requires="mylib/*" # drop one entry so it re-resolvesWhen you add a requirement that isn't in the lockfile, conan install --lockfile=conan.lock fails with Requirement ... not in lockfile. Use --lockfile-partial to allow the new one temporarily, and --lockfile-out to write the updated lockfile.
One lockfile, many configurations
A Conan 2 lockfile is shared across profiles: you can create it once for Linux and extend it for Windows (--lockfile=conan.lock --lockfile-out=conan.lock -pr windows), and conditional requirements from both are merged. Conan only uses the entries that apply to each configuration.
Lockfiles in CI
conan install . --lockfile=conan.lock --lockfile-partial=False --build=missing- Commit
conan.lockfor applications; libraries usually don't, because their consumers decide. - Let a scheduled job (or Renovate/Dependabot-style automation) refresh the lockfile and open a pull request, so updates are tested before they land.
--lockfile-outin CI also records what a build actually used, which is useful as a build artifact.
Test yourself
What does a Conan 2 lockfile pin?
- Only the top-level versions
- Versions and recipe revisions of every package in the graph, including tools
- Binary package IDs
- The compiler version
After mylib 1.2.0 was exported, which version did the locked graph use?
- 1.2.0
- 1.1.0, the version recorded when the lockfile was created
- 1.0.0
- It failed
You added a new requirement and conan install --lockfile=conan.lock fails. What's the clean fix?
- Delete the lockfile forever
- Re-create or extend the lockfile (
conan lock create/--lockfile-partial+--lockfile-out) and commit it - Use --build=missing
- Edit the timestamp