This document explains how to get started with Lean and mathlib.
If you get stuck, please come to the chat room to ask for assistance.
If you'd prefer to watch a short video tutorial, try:
We'll need to set up Lean, an editor that knows about Lean, and mathlib (the standard library).
Rather than installing Lean directly, we'll install a small program called elan which
automatically provides the correct version of Lean on a per-project basis. This is recommended for
all users.
- We'll need a terminal, along with some basic prerequisites.
- Ubuntu:
sudo apt install git curlif you don't already have these. - macOS: Install homebrew, then run
brew install gmp coreutilsin a terminal (gmpis required bylean,coreutilsbyleanpkg). - Windows 10:
- Either (recommended): install Git for Windows, after which you can open a terminal by typing "git bash" in the Windows search bar.
- Or: install Msys2, after which you can open a terminal by
typing "msys2" in the Windows search bar. You'll also need to run
pacman -S unzip gitin an msys2 terminal.
-
(This step can be skipped: VS Code will prompt you to install
elanif it can't find a usable copy of Lean.)At a terminal, run the command
curl https://raw.githubusercontent.com/Kha/elan/master/elan-init.sh -sSf | shand hit enter when a question is asked.
On Linux and on macOS, this automatically appends a line to your
$HOME/.profilewhich prepends$HOME/.elan/binto your$PATH.On Windows, this doesn't happen automatically.
- With Git for Windows you'll need to run
echo 'PATH="$HOME/.elan/bin:$PATH"' >> $HOME/.profilein the terminal. - With MSYS2 you'll need to run
echo 'PATH="/c/Users/$USERNAME/.elan/bin:$PATH"' >> $HOME/.bashrc.
It is recommended that you re-login, so that your environment knows about
elan.(Alternatively, type
source $HOME/.elan/envto update the current terminal.) - With Git for Windows you'll need to run
There are two Lean-aware editors, VS Code and emacs. This document describes using VS Code (for emacs, look at https://github.com/leanprover/lean-mode).
- Install VS Code.
- Launch VS Code.
- Click on the extension icon in the view bar at the left
and search for
lean. - Click "install", and then "reload" to restart VS Code.
- If you're running Windows, you've got one more step:
- If you're using
git bash, pressctrl-shift-pto open the command palette, and typeSelect Default Shell, then selectgit bashfrom the menu. - If you're using
msys2, pressctrl-commaagain to open the settings, and add two settings:
"terminal.integrated.shell.windows": "C:\\msys64\\usr\\bin\\bash.exe",
"terminal.integrated.shellArgs.windows": ["--login", "-i"]
- Verify Lean is working, for example by saving a file
test.leanand entering#eval 1+1. A green line should appear under this, and hovering the mouse over it you should see2displayed.
When you installed elan, it downloaded the latest stable release of Lean.
That may be too recent or too old for mathlib, and you really want mathlib.
- Decide on a name for your package. We will use
my_playground. - Run
leanpkg +nightly new my_playground. This will create amy_playgrounddirectory with a Lean project layout. - Run
cd my_playground. - Run
leanpkg add leanprover/mathlib. This will download mathlib and put it insidemy_playground/_target/deps/mathlib/.
That's it.
At this point you can already create some Lean file in my_playground/src:
say test.lean.
- Now launch VS Code.
- On Windows: Click "File -> Open folder" (Ctrl-K Ctrl-O) and open
my_playground. On macOS: Click "File -> Open..." and openmy_playground. It is essential that you open the folder, rather than a file inside it. - Open the file
test.lean, from the 'Explorer' view on the left edge of VS Code. - Type Ctrl-Shift-Enter (Cmd-Shift-Enter on macOS) to open the Lean message window.
- Type, say
#check 1to see the result.
You can also use import group_theory.subgroup and then #check is_subgroup.
This will use the uncompiled version of mathlib, which is very inefficient.
But if you run leanpkg build from inside my_playground,
then it will compile only those files that are dependencies of
mathlib group_theory/subgroup.lean.
If you want to play more, it's better to compile all of mathlib
once and for all.
You can do this by going into my_playground
and running lean --make _target/deps/mathlib. (Don't actually go into the mathlib directory and
run lean --make there!)
Now go and get some coffee.
Suppose you want to work on an existing project. As example, we will take https://github.com/kbuzzard/lean-stacks-project.
- Go the the directory where you would like this package to live.
- Run
git clone https://github.com/kbuzzard/lean-stacks-project. - This gives you a directory named
lean-stacks-project. Enter it. - Inside this directory is a file named
leanpkg.toml.elanuses this file to determine which version of Lean you need. You don't have to worry about this! - Run
leanpkg buildto compile everything. - Now is a good time to get yourself some tea and relax.
Once the compile is over, you can start working.
- Now launch VS Code.
(If you did not re-login after installing
elan, then you may have to launch VS Code from a terminal that has sourced$HOME/.elan/env.) - On Windowds: Click "File -> Open folder" (Ctrl-K Ctrl-O) and open
lean-stacks-project. On macOS: Check "File -> Open..." and openlean-stacks-project. - Type Ctrl-Shift-Enter (Cmd-Shift-Enter on macOS) to open the Lean message window.