Mathlib4 is developed against a single, precisely pinned version of the Lean 4 compiler and runtime. That version is recorded in a plain-text file calledDocumentation Index
Fetch the complete documentation index at: https://mintlify.com/leanprover-community/mathlib4/llms.txt
Use this file to discover all available pages before exploring further.
lean-toolchain at the root of the repository. The elan version manager reads this file automatically whenever you run any Lean or Lake command inside the directory, downloads the correct compiler if it is not already cached, and routes all subsequent commands through it. Getting this toolchain management right is essential for reproducible builds and smooth collaboration — both within Mathlib itself and in any downstream project that depends on it.
The lean-toolchain File
Thelean-toolchain file contains a single line identifying the toolchain:
<GitHub organization>/<repository>:<release tag>. The leanprover/lean4 prefix identifies the official Lean 4 repository; the tag v4.32.0-rc1 pins the exact release. When elan encounters this file it:
- Checks whether the named toolchain is already installed locally.
- If not, downloads and installs it from the Lean 4 GitHub releases.
- Runs all
lean,lake, and related executables through that version for the duration of the session.
elan is installed and the lean-toolchain file is present, the correct Lean version is used automatically.
Why Pinning Matters
Lean 4 is under active development. The compiler, standard library APIs, elaboration behavior, and tactic interfaces can all change between releases — sometimes in breaking ways. Mathlib’s 8,000+ files depend on precise elaboration behavior and on specific APIs being present. A single unconstrained toolchain upgrade can cause hundreds or thousands of files to fail to compile until the necessary adaptations are made. By pinning the toolchain inlean-toolchain, Mathlib guarantees:
- Reproducibility — any contributor with
elaninstalled gets the same build. - Compatibility — CI, local development, and cached
.oleanfiles all correspond to the same compiler binary. - Safety — downstream projects that copy the
lean-toolchainfile know they are using a version that Mathlib has tested.
The fixedToolchain Setting
Thelakefile.lean records an additional lock at the Lake level:
lean-toolchain. The corresponding field appears in lake-manifest.json as well:
lake build against Mathlib with a different elan default toolchain, Lake will print an error before attempting any compilation, making the mismatch immediately visible.
How elan Uses the Toolchain File
elan is the Lean version manager, analogous to rustup for Rust. It intercepts calls to lean and lake executables and dispatches them to the appropriate versioned installation.
Clone Mathlib (or your downstream project)
lean-toolchain file is already present in the repository.elan auto-detects the toolchain
Running any Lean or Lake command in the directory automatically triggers toolchain resolution:
The post_update Hook
Mathlib’slakefile.lean defines a post_update hook that runs automatically when a downstream project updates its dependencies with lake update. The hook copies the correct lean-toolchain version to the downstream project and fetches the cached .olean files:
Lean.versionString ≠ toolchainVersion check. Lean.versionString is the version of the lake binary currently executing the script (e.g. "4.32.0-rc1"), while toolchainVersion is parsed from the lean-toolchain file (stripping the leading v). If they differ, the hook prints an informative message and exits without fetching the cache, because the cached .olean files were compiled with a different compiler and would be unusable.
The
MATHLIB_NO_CACHE_ON_UPDATE=1 environment variable suppresses the cache fetch entirely. This is useful in CI environments where the cache step is managed separately.Downstream Projects: Copying the Toolchain File
When your project depends on Mathlib, it must use the same Lean version. The recommended approach is to copy Mathlib’slean-toolchain file into your project root and commit it:
lean-toolchain is out of sync with Mathlib’s, you will encounter errors when Lean tries to elaborate Mathlib imports. The cached .olean files are also tied to the compiler version, so a mismatched toolchain forces a full recompile of Mathlib from source — which can take many hours.
Updating the Toolchain
Mathlib’s toolchain is bumped by the maintainer team following a deliberate process:A new Lean release is published
The Lean core team tags a new release (e.g.
v4.33.0) or a release candidate on GitHub.Nightly testing branch catches issues early
Mathlib maintains a
nightly-testing branch that is continuously tested against the latest nightly Lean builds. Compilation failures on nightly are labelled and tracked before the stable release arrives, reducing the adaptation effort.Adaptation PRs are merged
Contributors file PRs to fix any files broken by upstream changes. Source files that required non-trivial changes due to upstream Lean behavior include an
adaptation_note comment explaining what changed and why:The lean-toolchain file is updated
The maintainer opens a PR that changes CI validates that all 8,000+ files compile cleanly, then the PR is merged into
lean-toolchain to the new version:master.Nightly Testing
Mathlib’s nightly testing workflow runs on a dedicated branch and automatically opens issues or PRs when the latestleanprover/lean4:nightly-* build breaks Mathlib files. This early-warning system means that by the time a stable release is cut, most incompatibilities are already resolved. The lake exe nightly-testing-checklist executable (declared in lakefile.lean) reports the current state of the nightly-testing branch: