Mathlib4 uses Lake — Lean 4’s official build system and package manager — for all build, test, and tooling tasks. Because Mathlib is a large library with hundreds of thousands of lines of Lean code, building it from scratch takes several hours on modern hardware. The precompiledDocumentation 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.
.olean cache makes iterative development practical by letting you download the artifacts already built by CI. This page is a complete reference for every build and test command you will need as a contributor.
Prerequisites
- Lean 4 and
elaninstalled via the community getting-started guide.elanmanages Lean toolchain versions; it automatically activates the correct version when youcdinto themathlib4directory. lake— ships with everyelan-managed Lean installation. No separate installation is needed.- Git — to clone the repository and manage branches.
- GitHub CLI
gh(optional) — needed only for theautolabeltool.
Run
lean --version and lake --version inside the mathlib4 directory to confirm you are using the pinned toolchain. If the versions do not match the lean-toolchain file, run elan toolchain install leanprover/lean4:$(cat lean-toolchain | cut -d: -f2).Standard Build Workflow
Always run this first. Skipping it means the next build step will recompile everything from scratch, which takes hours:
This downloads
.olean and .ilean files produced by Mathlib’s CI pipeline. The cache is keyed to the exact commit hash of your working tree, so you may need to re-run it after rebasing or after pulling upstream changes.With the cache in place, a full build should complete in a few minutes as Lake only needs to compile files not covered by the cache:
lake build Mathlib.Algebra.Group.Defs
lake build Mathlib.Data.List.Basic
lake build Mathlib.Topology.Basic
This is the fastest way to iterate on a single file during development — you do not need to wait for the full library to build.
This runs all tests defined under
MathlibTest/ using the MathlibTest test driver declared in lakefile.lean. A clean pass is required before opening a PR.This executes both the Python-based text linters (from
scripts/lint-style.py) and the Lean-based style linters. All reported issues must be resolved before requesting review. See the Style Guide for the full list of rules being checked.If you added a new
.lean source file to the library, regenerate the top-level Mathlib.lean import file so the new module is included in the build:Command Reference
Fetching and Managing the Cache
| Command | Description |
|---|---|
lake exe cache get | Download precompiled .olean files for the current commit |
lake exe cache get! | Force re-download even if local files are present |
lake exe cache | Print the full cache help menu |
Building
| Command | Description |
|---|---|
lake build | Build all of Mathlib (fast with cache, slow without) |
lake build Mathlib.X.Y.Z | Build a specific module and its dependencies |
lake build Archive | Build the Archive library |
lake build Counterexamples | Build the Counterexamples library |
lake clean | Remove all build artifacts under .lake/build |
.lake directory for a fully clean slate:
Testing and Linting
| Command | Description |
|---|---|
lake test | Run the full Mathlib test suite |
lake exe lint-style | Run text-based and Lean-based style linters |
lake exe check-yaml | Verify all declarations referenced in docs/*.yaml exist |
lake exe mk_all | Regenerate Mathlib.lean to include all source files |
lake exe autolabel <PR> | Auto-assign topic labels to a GitHub PR |
Checking PR Titles
Thecheck_title_labels executable validates that a PR title obeys Mathlib’s formatting requirements:
Cleaning Build Artifacts
Two levels of cleaning are available depending on how thoroughly you want to reset:Building HTML Documentation Locally
The mathlib4_docs repository is responsible for generating and publishing the official HTML documentation at leanprover-community.github.io/mathlib4_docs. To build the docs locally, clone that repository alongside yourmathlib4 checkout:
The final
lake build Mathlib:docs step can take 20 minutes or more even with the cache. The generated HTML files are placed in .lake/build/doc/ and can be viewed in any browser.Continuous Integration
Mathlib’s CI is powered by GitHub Actions. The primary workflow is defined in.github/workflows/build.yml. Every push to a PR branch triggers:
- A full
lake buildacross Linux runners. - The test suite (
lake test). - The style linter (
lake exe lint-style). - The YAML checker (
lake exe check-yaml).
Bors Merge Queue
Mathlib does not allow direct pushes tomaster. All merges go through Bors, an automated merge bot. When a reviewer approves a PR, they post:
master is always in a passing state.
Downstream Testing
TheDownstreamTest/ directory contains tests that verify Mathlib’s compatibility with downstream projects. These are run periodically rather than on every PR, but failures there can block releases.
Dependency Management
Mathlib’s upstream dependencies (Batteries, Aesop, Qq, ProofWidgets, importGraph, LeanSearchClient, plausible) are declared inlakefile.lean. To update them:
lake-manifest.json. Commit that file and open a PR to land the dependency update.
Gitpod and GitHub Codespaces
If you prefer a browser-based development environment with Lean and Lake pre-installed:- GitHub Codespaces: click the “Open in GitHub Codespaces” badge on the repository homepage.
- Gitpod: click the “Open in Gitpod” badge, or navigate to
gitpod.io/#https://github.com/leanprover-community/mathlib4.
lake exe cache get automatically on startup so you can begin editing immediately.