Lake Build System
Lake is Lean's build system and package manager. It handles dependencies, compilation, and project configuration.
Creating a New Project
The lake new command scaffolds a complete project structure. Useexe for programs you want to run, or omit it for libraries.
1# Create a new library project2lake new myproject34# Create an executable project5lake new myapp exe67# Initialize in existing directory8lake init myprojectThis creates a project structure:
1myproject/2├── lakefile.lean # Build configuration3├── lean-toolchain # Lean version4├── Main.lean # Entry point (for exe)5└── Myproject/6 └── Basic.lean # Library codeThe lakefile.lean
The lakefile configures your project:
1import Lake2open Lake DSL34package myproject where5 -- Package-level options6 leanOptions := #[7 ⟨`pp.unicode.fun, true⟩8 ]910-- Library target11@[default_target]12lean_lib Myproject where13 -- Library options14 roots := #[`Myproject]1516-- Executable target17lean_exe myapp where18 root := `MainBasic Lake Commands
These commands handle everyday development tasks. Run them from the project root directory.
1# Build the project2lake build34# Build and run an executable5lake exe myapp67# Clean build artifacts8lake clean910# Update dependencies11lake update1213# Get help14lake helpAdding Dependencies
Add external packages to your lakefile:
1import Lake2open Lake DSL34package myproject where5 -- Dependencies go here67-- From GitHub8require mathlib from git9 "https://github.com/leanprover-community/mathlib4" @ "main"1011-- With specific version12require std from git13 "https://github.com/leanprover/std4" @ "v4.3.0"1415-- Local dependency16require localLib from ".." / "local-lib"1718@[default_target]19lean_lib MyprojectAfter adding dependencies:
1# Fetch and build dependencies2lake update3lake build.lake directory. This folder is typically gitignored.Understanding .lake
The .lake directory stores build artifacts, downloaded dependencies, and generated files. You can safely delete it to force a clean rebuild.
Project Targets
Libraries
A library target makes modules available for import by other code. Specify which modules belong to this library using the roots field.
1-- A library with specific root modules2lean_lib MyLib where3 roots := #[`MyLib]4 5-- Multiple root modules6lean_lib Utilities where7 roots := #[`Utils.String, `Utils.Math, `Utils.IO]89-- Exclude certain modules from build10lean_lib Core where11 roots := #[`Core]12 globs := #[.submodules `Core]Executables
An executable target produces a runnable program. The root field points to the module containing the main function.
1-- Basic executable2lean_exe myapp where3 root := `Main45-- Executable with dependencies6lean_exe cli where7 root := `CLI.Main8 -- This exe depends on the MyLib library9 -- (usually automatic)The lean-toolchain File
This file specifies the Lean version:
1leanprover/lean4:v4.3.0When you run lake, it ensures the correct Lean version is used. This guarantees reproducible builds.
Deep Dive: Lean Toolchain Management
The toolchain file works with elan, Lean's version manager:
1# Install elan (if not already)2curl https://raw.githubusercontent.com/leanprover/elan/master/elan-init.sh -sSf | sh34# List installed toolchains5elan show67# Install a specific version8elan install leanprover/lean4:v4.3.0910# Set default toolchain11elan default leanprover/lean4:stableConfiguration Options
1package myproject where2 -- Lean compiler options3 leanOptions := #[4 ⟨`pp.unicode.fun, true⟩, -- Use λ instead of fun5 ⟨`autoImplicit, false⟩ -- Disable auto-implicit6 ]7 8 -- Stricter warnings9 moreServerOptions := #[10 ⟨`warningAsError, true⟩11 ]1213lean_lib MyLib where14 -- Library-specific options15 defaultFacets := #[LeanLib.sharedLib] -- Build shared libraryScripts and Custom Commands
1-- Define a custom script2script test do3 IO.println "Running tests..."4 -- Your test logic here5 return 067script format do8 IO.println "Formatting code..."9 return 01# Run custom scripts2lake run test3lake run formatCreate a script that prints the Lean version used by your project.
1script showVersion do2 IO.println s!"Lean toolchain: {(← IO.FS.readFile "lean-toolchain").trim}"Common Workflows
Starting a New Project
1lake new myproject2cd myproject3lake build4lake exe myproject # If it's an executableAdding Mathlib
1-- In lakefile.lean2require mathlib from git3 "https://github.com/leanprover-community/mathlib4"45-- Then run:6-- lake update7-- lake exe cache get # Download pre-built .olean filesBuilding for Release
1# Build with optimizations2lake build -Kreleaselake exe cache get when using Mathlib to download pre-compiled files instead of building from source—saves hours of compile time.