Installing LLVM Compiler Infrastructure

LLVM
Steps for installing the LLVM Compiler Infrastructure from source, including common build errors.
Published

May 21, 2021

This post walks you through how to install the LLVM Compiler Infrastructure from scratch.

Prerequisites

  • Install git on your system:
sudo apt install git-all
  • A Linux machine (tested on Ubuntu 20 LTS).
  • Make sure you have sufficient storage (around 20 GB free).
  • While building, your CPU might freeze — close other programs before installation.

Clone the LLVM source

To save space we do a shallow clone. This also pulls LLVM subprojects such as Clang. See Getting the source code and building LLVM for the full source.

mkdir llvm
cd llvm
git clone --depth 1 https://github.com/llvm/llvm-project.git

Configure and build LLVM and Clang

cd llvm-project
mkdir build
cd build
cmake -G Ninja ../llvm

Note that -G Ninja is a build-system generator — you can choose others too. Check the official documentation if you would like to use something other than Ninja.

If you haven’t installed cmake:

sudo apt install cmake

Then run:

cmake -G Ninja ../llvm

If Ninja is missing, you’ll see errors like:

Error: CMake was unable to find a build program corresponding to "Ninja".
CMAKE_MAKE_PROGRAM is not set. You probably need to select a different build tool.
Error: CMAKE_C_COMPILER not set, after EnableLanguage
Error: CMAKE_CXX_COMPILER not set, after EnableLanguage
Error: CMAKE_ASM_COMPILER not set, after EnableLanguage
-- Configuring incomplete, errors occurred!

Install Ninja

sudo apt-get install -y ninja-build

Then generate the build files:

cmake -G Ninja ../llvm

Building LLVM and Clang

cmake --build .

This might take hours depending on your machine. It took about 1 hour on my machine (Intel Xeon, 32 GB RAM).

I also encountered a linking error similar to:

FAILED: lib/libLTO.so.13git

Do the following — see this Stack Overflow answer:

cmake -G Ninja -DCMAKE_BUILD_TYPE=Release -DLLVM_PARALLEL_LINK_JOBS=1 ../llvm
cmake --build .

This error likely happens because you are running out of memory.