Lake Build System
Lake is Lean's build system and package manager. It handles dependencies, compilation, and project configuration.
Creating a New Project
bash
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:
text
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:
lean
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 := `MainKey Takeaway
The lakefile is written in Lean itself. It defines packages (collections of code), libraries (reusable modules), and executables (runnable programs).
Basic Lake Commands
bash
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:
lean
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:
bash
1# Fetch and build dependencies2lake update3lake buildℹ
Dependencies are cached in the
.lake directory. This folder is typically gitignored.Project Targets
Libraries
lean
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
lean
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:
text
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:
bash
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
lean
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
lean
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 0bash
1# Run custom scripts2lake run test3lake run formatCommon Workflows
Starting a New Project
bash
1lake new myproject2cd myproject3lake build4lake exe myproject # If it's an executableAdding Mathlib
lean
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
bash
1# Build with optimizations2lake build -Krelease💡
Use
lake exe cache get when using Mathlib to download pre-compiled files instead of building from source—saves hours of compile time.