# How to Build LLVM from Source: A Complete CMake Guide

> Learn how to build LLVM from source using CMake. Follow our complete guide to configure, compile, and install LLVM efficiently for your development needs.

- Repository: [LLVM/llvm-project](https://github.com/llvm/llvm-project)
- Tags: how-to-guide
- Published: 2026-09-09

---

**Building LLVM from source requires CMake (≥ 3.13), a modern C++ compiler, and Ninja, followed by configuring the build via [`llvm/CMakeLists.txt`](https://github.com/llvm/llvm-project/blob/main/llvm/CMakeLists.txt) with the `-DLLVM_ENABLE_PROJECTS` and `-DLLVM_ENABLE_RUNTIMES` flags, then compiling with `ninja` and installing with `ninja install`.**

The llvm/llvm-project repository hosts the complete LLVM compiler infrastructure, including the Clang frontend, LLD linker, MLIR framework, and runtime libraries like compiler‑rt and libc++. Compiling from source grants full control over which components to enable and which target architectures to support. This guide follows the official workflow defined in [`llvm/docs/GettingStarted.html`](https://github.com/llvm/llvm-project/blob/main/llvm/docs/GettingStarted.html) and the top‑level [`llvm/CMakeLists.txt`](https://github.com/llvm/llvm-project/blob/main/llvm/CMakeLists.txt) build system.

## Prerequisites

Before cloning the repository, install the following tools:

- **CMake** ≥ 3.13 (required to process the LLVM build scripts)
- **C++ compiler** — GCC ≥ 7 or Clang ≥ 7 (must support C++17)
- **Ninja** or GNU Make (Ninja is strongly recommended for parallel builds)
- **Python** 3 (used by the LLVM test infrastructure and utility scripts)
- **Git** (to clone the monorepo)

Exact version requirements and platform‑specific packages are documented in [`llvm/docs/GettingStarted.html`](https://github.com/llvm/llvm-project/blob/main/llvm/docs/GettingStarted.html).

## Clone the Repository

Create a working directory and clone the monorepo, which contains all LLVM sub‑projects in a single tree:

```bash
mkdir ~/llvm-workspace && cd ~/llvm-workspace
git clone https://github.com/llvm/llvm-project.git
mkdir build install

```

Keeping `build/` and `install/` separate from the source tree ensures an **out‑of‑tree build**, preventing generated files from polluting the repository and making it easy to reconfigure with different options.

## Configure the Build with CMake

LLVM uses a hierarchical CMake system. The entry point is [`llvm/CMakeLists.txt`](https://github.com/llvm/llvm-project/blob/main/llvm/CMakeLists.txt) at the root of the `llvm/` directory. From your `build/` directory, run CMake pointing to that file:

```bash
cd build
cmake -G Ninja \
  -DCMAKE_BUILD_TYPE=Release \
  -DCMAKE_INSTALL_PREFIX=../install \
  -DLLVM_ENABLE_ASSERTIONS=ON \
  -DLLVM_TARGETS_TO_BUILD=host \
  -DLLVM_ENABLE_PROJECTS="clang;lld;mlir" \
  -DLLVM_ENABLE_RUNTIMES="compiler-rt;libcxx;libcxxabi;openmp" \
  ../llvm-project/llvm

```

### Selecting Projects and Runtimes

LLVM distinguishes between **projects** (tools and libraries compiled alongside LLVM) and **runtimes** (libraries compiled with the freshly built compiler):

- **`LLVM_ENABLE_PROJECTS`** — Semicolon‑separated list including `clang`, `lld`, `mlir`, `flang`, etc. These are built using the system compiler.
- **`LLVM_ENABLE_RUNTIMES`** — List including `compiler-rt`, `libcxx`, `libcxxabi`, `openmp`, `libunwind`. These are built after LLVM is bootstrapped and use the just‑built Clang.

### Key CMake Variables

| Variable | Purpose |
|----------|---------|
| **`-DCMAKE_BUILD_TYPE`** | `Release` (optimized), `Debug` (with symbols), or `RelWithDebInfo`. |
| **`-DCMAKE_INSTALL_PREFIX`** | Final destination for `ninja install` (e.g., `/usr/local` or a local directory). |
| **`-DLLVM_TARGETS_TO_BUILD`** | Architectures to support: `host` (autodetect), `X86`, `ARM`, `AArch64`, or `all`. |
| **`-DLLVM_ENABLE_ASSERTIONS`** | Enables expensive assertions for debugging LLVM internals. |
| **`-DLLVM_PARALLEL_LINK_JOBS`** | Limits parallel link jobs to reduce memory usage (e.g., `=2`). |

## Compile LLVM

With Ninja as the generator, compilation is fully parallelized by default. From the `build/` directory:

```bash
ninja

```

If memory is constrained, limit concurrency:

```bash
ninja -j4

```

The build generates libraries, TableGen binaries, Clang, LLD, and any other enabled tools inside `build/bin/` and `build/lib/`.

## Run the Test Suite

LLVM uses the `lit` (LLVM Integrated Tester) framework. Validate the build with:

```bash
ninja check-all

```

To run tests for a specific project only:

```bash
ninja check-clang
ninja check-lld

```

Test definitions reside in each sub‑project’s `test/` directory (e.g., [`clang/test/CMakeLists.txt`](https://github.com/llvm/llvm-project/blob/main/clang/test/CMakeLists.txt)).

## Install the Toolchain

Deploy the build to the installation directory specified earlier:

```bash
ninja install

```

This copies binaries (`clang`, `lld`, `llvm-as`), headers, and libraries into `$CMAKE_INSTALL_PREFIX`. The directory layout is detailed in [`llvm/docs/InstallingFromSource.html`](https://github.com/llvm/llvm-project/blob/main/llvm/docs/InstallingFromSource.html). After installation, verify Clang works:

```bash
../install/bin/clang --version

```

## (Optional) Build Documentation

Generate Doxygen or Sphinx documentation by enabling the appropriate flags during configuration:

```bash
cmake -G Ninja -DLLVM_ENABLE_DOXYGEN=ON -DLLVM_ENABLE_SPHINX=ON ../llvm-project/llvm
ninja doxygen-llvm
ninja docs

```

Output appears in `build/docs/` or `build/doxygen/`.

## Summary

- **Install prerequisites**: CMake ≥ 3.13, C++ compiler, Ninja, and Python 3.
- **Clone**: `git clone https://github.com/llvm/llvm-project.git`.
- **Configure**: Run CMake from a separate `build/` directory pointing to [`llvm/CMakeLists.txt`](https://github.com/llvm/llvm-project/blob/main/llvm/CMakeLists.txt); use `LLVM_ENABLE_PROJECTS` and `LLVM_ENABLE_RUNTIMES` to select components.
- **Compile**: Execute `ninja` in the build directory.
- **Test**: Run `ninja check-all` to execute the lit‑based test suites.
- **Install**: Use `ninja install` to place binaries and libraries in the prefix directory.

## Frequently Asked Questions

### How long does it take to build LLVM from source?

A full build of LLVM with Clang and runtimes on a modern 8‑core machine typically takes 30–60 minutes for a Release configuration. Debug builds with assertions enabled can take significantly longer due to increased object size and link times.

### Can I build LLVM using GCC, or is Clang required?

You can compile LLVM with GCC. The build system uses whichever compiler is in your `CC` and `CXX` environment variables. However, if you include runtimes like `libcxx` in `LLVM_ENABLE_RUNTIMES`, the build will first compile LLVM/Clang, then use that freshly built Clang to compile the runtimes.

### What is the difference between LLVM_ENABLE_PROJECTS and LLVM_ENABLE_RUNTIMES?

`LLVM_ENABLE_PROJECTS` lists components like Clang, LLD, and MLIR that are built with the host system compiler alongside LLVM. `LLVM_ENABLE_RUNTIMES` lists libraries like compiler‑rt and libc++ that must be built with the newly built Clang compiler and are compiled in a second phase after the LLVM toolchain is ready.

### How do I build LLVM for specific architectures only?

Set `-DLLVM_TARGETS_TO_BUILD` to a semicolon‑separated list of target names (e.g., `X86;ARM;AArch64`) instead of `host` or `all`. This reduces compile time and binary size by excluding code generators for unused instruction sets.