Brew Your Own Compiler: Building LLVM/Clang from Source on Any OS!



This content originally appeared on DEV Community and was authored by Prajwal KP

Building LLVM/Clang from Source: Ubuntu, Fedora, Arch, Windows, macOS
Why build LLVM/Clang from source? Because pre-built binaries are like instant coffee functional, but you miss the aroma of roasting your own beans! Let’s brew the freshest compiler toolchain for your system, whether it’s Linux, Windows, or macOS.

👋 Hey there! I’m Prajwal from Bengaluru a relentless explorer in computing.

  • Started with crafting sleek, user-focused web apps, then dove deep into infrastructure automation and DevOps.
  • Now passionate about systems programming, compiler engineering, and the Rust ecosystem.
  • I thrive where software meets hardware: building safe, fast, and efficient code.

✨ Turning caffeine into code (and occasional bugs) since 2023
🔭 Interests: Systems programming • Compilers • The Rust ecosystem🦀 Advocate for: Safe, fast, and reliable software craftsmanship
🤝 Open to collaborate on: Rust, low-level projects, compiler toolchains
💡 Still hands-on with: TypeScript, Go, DevOps, Web Development but my heart beats in cargo run

🧑‍💻 Tech I use: Rust | Go | C/C++ | Java | TypeScript | React/Next.js | Linux | Docker | Kubernetes | Bash | Git

⚙ What drives me:

  • Systems Programming: kernel dev, OS work, drivers, performance tuning
  • Compiler Construction: language design, parsing, LLVM, optimizations
  • DevOps: modern infra, automation, CI/CD workflows
  • The Rust & Go Philosophy: safety, concurrency, and zero-cost abstractions

Prerequisites by OS
Ubuntu / Debian

sudo apt update
sudo apt install -y build-essential git cmake ninja-build \
    python3-dev libtinfo-dev libxml2-dev zlib1g-dev \
    libedit-dev swig curl

Installs GCC/g++, CMake, Ninja, Python headers, and necessary dev libraries.

Fedora

sudo dnf install -y @development-tools git cmake ninja-build \
    python3-devel ncurses-devel libxml2-devel zlib-devel \
    libedit-devel swig curl

Uses Fedora’s package groups and development headers.

Arch Linux

sudo pacman -Syu --needed base-devel git cmake ninja \
    python ncurses libxml2 zlib libedit swig curl

Uses pacman and Arch-specific base-devel group.

Windows
Requirements:

  1. Visual Studio 2019 or newer (Community Edition OK)
  2. Latest CMake (comes with recent Visual Studio)
  3. Git for Windows
  4. Python 3.x (from https://python.org or Microsoft Store)
  5. Optional: Ninja build system

Steps:

  1. Open x64 Native Tools Command Prompt for VS.
  2. Install Git for Windows and ensure git is in your PATH.
  3. Clone the repository:git clone –depth 1 https://github.com/llvm/llvm-project.git
  4. cd llvm-project

Create a build directory:mkdir build
cd build

Configure with CMake and Visual Studio:

cmake -G "Visual Studio 17 2022" -A x64 ../llvm ^
  -DCMAKE_BUILD_TYPE=Release ^
  -DLLVM_ENABLE_PROJECTS="clang;lld" ^
  -DLLVM_ENABLE_ASSERTIONS=ON

For Ninja (if installed): Use -G “Ninja” instead.

Build LLVM (all targets in Release mode):
cmake --build . --config Release -- /m

Optionally run tests:
ctest -C Release

Install:
cmake --install . --config Release

For full instructions, see LLVM + Visual Studio Setup.

macOS
Requirements:

  1. Xcode (install from App Store)
  2. Homebrew (https://brew.sh)
  3. Install tools:brew install cmake ninja git python3 swig curl

Some systems may need:
brew install bzip2 xz zlib lz4 zstd coreutils make

Steps:

  1. Clone LLVM:git clone –depth 1 https://github.com/llvm/llvm-project.git
  2. cd llvm-project

Create a build directory:
mkdir build && cd build

Configure the build:

cmake -G Ninja ../llvm \
  -DCMAKE_BUILD_TYPE=Release \
  -DCMAKE_INSTALL_PREFIX=/usr/local \
  -DLLVM_ENABLE_PROJECTS="clang;lld" \
  -DLLVM_ENABLE_ASSERTIONS=ON

Build:ninja
Run tests:
ninja check-llvm

Install (may require sudo):
sudo ninja install

macOS builds use Clang and Xcode-provided SDKs by default.

General Build and Project Structure

These steps are the same across all platforms (use platform-specific package install and generator):

  1. Clone Repository: git clone –depth 1 https://github.com/llvm/llvm-project.git
  2. cd llvm-project

Out-of-Source Build:
mkdir build && cd build

CMake Configuration:cmake -G Ninja ../llvm \
  -DCMAKE_BUILD_TYPE=Release \
  -DCMAKE_INSTALL_PREFIX=/usr/local \
  -DLLVM_ENABLE_PROJECTS="clang;lld" \
  -DLLVM_TARGETS_TO_BUILD="X86" \
  -DLLVM_ENABLE_ASSERTIONS=ON

Use -G “Visual Studio 17 2022” and -A x64 on Windows.

Build:

Linux/macOS: ninja

Windows:
cmake --build . --config Release -- /m

Run Tests (Optional):

Linux/macOS: ninja check-llvm
Windows: ctest -C Release

Install:

Linux/macOS: sudo ninja install
Windows: cmake --install . --config Release

Platform-Specific Notes

  • Windows: Only Visual Studio builds are officially supported. If forced to use MinGW, expect compatibility issues.
  • macOS: Must have Xcode’s command-line tools. Some library locations may differ (Homebrew paths).
  • Linux: Most distros already provide LLVM as packages; building source is for custom/bleeding-edge needs.

Management: Uninstallation and Upgrades

Linux/macOS

Use install_manifest.txt after install to track files:cd build
sudo xargs rm -vf < install_manifest.txt

Or, prefer a custom install prefix and simply delete the install directory.

Windows

If you used the default CMake install, files were put under the chosen prefix (e.g., C:\Program Files\LLVM).
Use the Windows installer or simply remove the install folder.

Checking Installation

Run:

clang --version
llc --version

If these return your custom version info, installation succeeded!
Additional Resources

Congratulations, you’ve just compiled LLVM/Clang like a coding wizard! Now go forth and optimize the world or at least your next project. And if your compiler crashes, don’t blame me; I’m just the guide, not the gremlin in your machine!


This content originally appeared on DEV Community and was authored by Prajwal KP