This directory contains the implementation of Mathlib's build cache system (lake exe cache), which downloads pre-built .olean files to avoid recompiling the entire library.
Note: A new
lake cachecommand is currently being designed and implemented in Lake itself. This will eventually replace the Mathlib-specificlake exe cacheand work for all repositories. Until then, this cache system remains the primary way to get pre-built artifacts for Mathlib.
# Download and unpack cache for all of Mathlib
lake exe cache get
# Force re-download everything
lake exe cache get!
# Download cache for specific files only (and their dependencies)
lake exe cache get Mathlib/Algebra/Group/Basic.lean
lake exe cache get Mathlib.Algebra.Group.Basic| Command | Description |
|---|---|
get [ARGS] |
Download linked files missing on the local cache and decompress |
get! [ARGS] |
Download all linked files and decompress (force re-download) |
get- [ARGS] |
Download linked files missing to local cache, but do not decompress |
pack |
Compress non-compressed build files into the local cache |
pack! |
Compress build files into the local cache (no skipping) |
unpack |
Decompress linked already downloaded files |
unpack! |
Decompress linked already downloaded files (no skipping) |
clean |
Delete non-linked files |
clean! |
Delete everything on the local cache |
lookup [ARGS] |
Show information about cache files for the given Lean files |
| Command | Description |
|---|---|
put |
Run pack then upload linked files missing on the server |
put! |
Run pack then upload all linked files |
put-unpacked |
put only files not already packed; intended for CI use |
commit |
Write a commit on the server |
commit! |
Overwrite a commit on the server |
The get, get!, get-, and lookup commands accept:
- Module names:
Mathlib.Algebra.Group.Basic - File paths:
Mathlib/Algebra/Group/Basic.lean - Folder names:
Mathlib/Data/(finds all Lean files inside) - Glob patterns:
Mathlib/**/Order/*.lean(via shell expansion)
When arguments are provided, only the specified files and their transitive imports are downloaded.
| Option | Description |
|---|---|
--repo=OWNER/REPO |
Override the repository to fetch cache from (e.g., --repo=leanprover-community/mathlib4) |
| Variable | Description | Default |
|---|---|---|
MATHLIB_CACHE_DIR |
Directory for cached .ltar files |
$XDG_CACHE_HOME/mathlib or ~/.cache/mathlib |
| Variable | Description | Default |
|---|---|---|
MATHLIB_CACHE_USE_CLOUDFLARE |
Set to 1 or true to use Cloudflare R2 instead of Azure |
Azure cache |
These allow overriding the cache endpoints, useful for mirrors or custom deployments:
| Variable | Description | Default |
|---|---|---|
MATHLIB_CACHE_GET_URL |
URL for downloading cache files | Azure or Cloudflare URL based on MATHLIB_CACHE_USE_CLOUDFLARE |
MATHLIB_CACHE_PUT_URL |
URL for uploading cache files | Azure or Cloudflare URL based on MATHLIB_CACHE_USE_CLOUDFLARE |
| Variable | Description |
|---|---|
MATHLIB_CACHE_SAS |
Azure SAS token (when using Azure backend) |
MATHLIB_CACHE_S3_TOKEN |
S3 credentials (when using Cloudflare backend) |
Each Lean file's cache is identified by a hash computed from:
-
Root hash: A combination of:
lakefile.leancontentlean-toolchaincontentlake-manifest.jsoncontent- The Lean compiler's git hash
- A generation counter (bumped to invalidate all caches)
-
File hash: Mixing:
- The root hash
- The file's path relative to its package
- The file's content hash
- Hashes of all imported files
This ensures that any change to toolchain, dependencies, or source files produces a different cache key.
Cache files use the .ltar format (Lean tar), handled by leantar. Each .ltar contains:
.oleanfiles (compiled Lean).ileanfiles (interface info).tracefiles (build traces).cfiles (generated C code)- Associated
.hashfiles
The cache covers these packages:
MathlibBatteriesAesopCliImportGraphLeanSearchClientPlausibleQqProofWidgetsArchiveCounterexamples
- Download URL:
https://lakecache.blob.core.windows.net/mathlib4 - Used by default for downloads and uploads
- Download URL:
https://mathlib4.lean-cache.cloud - Upload URL:
https://a09a7664adc082e00f294ac190827820.r2.cloudflarestorage.com/mathlib4 - Enable with
MATHLIB_CACHE_USE_CLOUDFLARE=1
You can host your own cache mirror or private cache using any S3-compatible storage or HTTP server.
Your endpoint must support:
-
GET requests for downloading files at:
/f/{repo}/{hash}.ltar- for fork caches/f/{hash}.ltar- for main mathlib cache (Azure only)/c/{commit_hash}- for commit manifests
-
PUT requests for uploading (if you need upload capability)
# Download from a custom mirror
export MATHLIB_CACHE_GET_URL="https://my-mirror.example.com/mathlib4"
lake exe cache get
# Upload to a custom endpoint
export MATHLIB_CACHE_PUT_URL="https://my-upload.example.com/mathlib4"
export MATHLIB_CACHE_SAS="your-auth-token" # or MATHLIB_CACHE_S3_TOKEN for S3
lake exe cache putFor S3-compatible storage (MinIO, Cloudflare R2, AWS S3, etc.):
- Create a bucket (e.g.,
mathlib-cache) - Configure public read access for downloads (or use signed URLs)
- Set up authentication for uploads
- Set the environment variables:
export MATHLIB_CACHE_GET_URL="https://your-bucket.s3.region.amazonaws.com/mathlib-cache"
export MATHLIB_CACHE_PUT_URL="https://your-bucket.s3.region.amazonaws.com/mathlib-cache"
export MATHLIB_CACHE_USE_CLOUDFLARE=1 # Use S3-style auth
export MATHLIB_CACHE_S3_TOKEN="ACCESS_KEY:SECRET_KEY"For a read-only mirror using nginx or any static file server:
- Periodically sync files from the official cache
- Serve them at a public URL
- Point users to your mirror:
export MATHLIB_CACHE_GET_URL="https://mathlib-mirror.myorg.com"
lake exe cache getThe cache uses this URL pattern:
{BASE_URL}/f/{repo}/{filename}.ltar # Fork/branch caches
{BASE_URL}/f/{filename}.ltar # Main mathlib cache (Azure)
{BASE_URL}/c/{commit_hash} # Commit manifests
Where:
{repo}is likeleanprover-community/mathlib4orusername/mathlib4{filename}is a hash like1234567890abcdef{commit_hash}is a git commit SHA
The cache system automatically downloads and manages:
- curl (>=7.70, preferably >=7.81) - for HTTP transfers
- leantar - for
.ltarcompression/decompression
If your system curl is too old, a static binary is downloaded automatically on Linux.
| Path | Description |
|---|---|
~/.cache/mathlib/ |
Default cache directory |
~/.cache/mathlib/*.ltar |
Cached build artifacts |
~/.cache/mathlib/curl.cfg |
Temporary curl configuration |
.lake/build/lib/lean/ |
Unpacked .olean files |
.lake/build/ir/ |
Unpacked .c files |