Lean 4 projects are managed by Lake, the official build system and package manager. Adding Mathlib to your project is a matter of declaring it as a dependency in yourDocumentation 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.
lakefile.lean, pinning the correct toolchain in lean-toolchain, and then fetching the precompiled .olean files so you do not have to compile Mathlib from scratch (a process that would take hours). This guide walks through every step.
These instructions assume you have already installed
elan and Lean 4. If not, see the Installation guide first.Creating a New Project
The fastest way to start is with Lake’s built-inmath template, which scaffolds a project pre-configured to depend on Mathlib.
Create the project
Run the following command, replacing Lake creates the following structure:
myproject with your desired project name:Inspect the generated lakefile.lean
Open The
lakefile.lean. The math template generates a require statement that pulls in Mathlib from the leanprover-community GitHub organization:lakefile.lean
require line tells Lake to fetch Mathlib from the master branch. Lake records the resolved commit hash in lake-manifest.json so builds are reproducible.The
lean-toolchain file generated by the template matches Mathlib’s current toolchain. Do not change this file manually unless you are intentionally upgrading to a newer Mathlib version.Match the lean-toolchain file
Your project’s
lean-toolchain must match the Lean version that Mathlib was built with. The template handles this automatically, but if you are adding Mathlib to an existing project, copy Mathlib’s toolchain version into your own lean-toolchain:lean-toolchain
Update dependencies
Fetch and lock the dependency tree:This writes the resolved commit hash for Mathlib (and all its transitive dependencies) into
lake-manifest.json. You should commit this file to version control so all collaborators use exactly the same library revisions.Mathlib’s own lakefile.lean includes a post_update hook that automatically runs lake exe cache get after lake update, provided the environment variable MATHLIB_NO_CACHE_ON_UPDATE is not set to 1. You will see output like:Fetch precompiled build cache
Mathlib contains hundreds of thousands of lines of Lean. Compiling it locally from source takes several hours. Instead, fetch the precompiled This downloads
.olean files from Mathlib’s build cache server:.olean files corresponding to the exact Mathlib commit in your lake-manifest.json. Typical download time is a few minutes depending on your connection.Adding Mathlib to an Existing Project
If you already have a Lake project and want to add Mathlib, edit yourlakefile.lean to include the require statement:
lakefile.lean
lean-toolchain file to match Mathlib’s version and run:
Importing Mathlib in Your Source Files
Once Mathlib is a declared dependency and the cache has been fetched, you can import it in any.lean file.
Import Everything
For exploratory work or small projects, import the entire library with a single line:Selective Imports
For larger projects where compilation time matters, import only the modules you need:The post_update Hook and Cache Automation
Mathlib ships a post_update script in its own lakefile.lean. When you run lake update in a downstream project (i.e., any project that depends on Mathlib), Lake automatically invokes this hook. The hook:
- Checks that the running Lake version matches the toolchain version in
lean-toolchain. - If they match, runs
lake exe cache getautomatically. - If they do not match (e.g., you have just changed the toolchain), it prints a message asking you to run
lake exe cache getmanually.
lake update is enough — the cache is fetched for you.
Disabling Automatic Cache Fetch
If you are in an environment where the cache should not be fetched automatically (e.g., a CI job that manages caching separately), set the following environment variable before runninglake update:
post_update hook exits early and no cache download is attempted.
Minimal Working lakefile.lean
Here is a complete, minimal lakefile.lean for a downstream project that depends on Mathlib:
lakefile.lean
lean-toolchain:
lean-toolchain
Updating Mathlib
To update to the latest Mathlib revision, run:lake-manifest.json to the latest master commit, updates lean-toolchain if necessary, and (via the post_update hook) fetches fresh cache files.
Transitive Dependencies
When you depend on Mathlib, you automatically inherit its transitive dependencies. These are all managed by Lake and recorded inlake-manifest.json. The current set includes:
| Package | Purpose |
|---|---|
batteries | Extended standard library and basic tactics |
aesop | Automated proof search tactic |
Qq | Typed metaprogramming quotations |
proofwidgets | Interactive UI widgets for the infoview |
importGraph | Import graph analysis utilities |
LeanSearchClient | Integration with Lean search services |
plausible | Property-based testing for Lean |