Installing LLVM Compiler Infrastructure
This post walks you through how to install the LLVM Compiler Infrastructure from scratch.
Prerequisites
- Install
giton 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.gitConfigure and build LLVM and Clang
cd llvm-project
mkdir build
cd build
cmake -G Ninja ../llvmNote that
-G Ninjais 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 cmakeThen run:
cmake -G Ninja ../llvmIf 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-buildThen generate the build files:
cmake -G Ninja ../llvmBuilding 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.