Operating System CentOS

How to Install LLVM and Clang on AlmaLinux 9, Rocky Linux 9 and CentOS Stream 9

Install LLVM and Clang on the RHEL 9 family from AppStream, test the compiler, and get the newest upstream release from binaries or source.

This guide shows how to install the LLVM toolchain and the Clang C/C++ compiler on AlmaLinux 9, Rocky Linux 9 and CentOS Stream 9: first the supported packages from the distribution, which is all most people need, and then two ways to get the very latest upstream LLVM release when a project requires it.

Older tutorials dealt with CentOS 6, where the packaged Clang was years out of date and a newer one meant building GCC and LLVM from source. CentOS 6 has been end-of-life since November 2020, and CentOS 7 and 8 have followed. On the RHEL 9 family the situation is much better: the AppStream repository carries a recent LLVM and updates it to newer major versions over the life of the release. When this was written, AlmaLinux 9.8 shipped LLVM and Clang 21.1.8, while the newest upstream release was 23.1.5.

Requirements

  • A server running AlmaLinux 9, Rocky Linux 9, CentOS Stream 9 or RHEL 9.
  • A user with sudo rights.
  • For the build-from-source option only: plenty of free disk space and memory, and patience. A full LLVM build is one of the heavier things you can compile.

Option 1: Install LLVM and Clang from AppStream

The llvm-toolset package pulls in Clang, the LLVM tools and the LLD linker in one go:

sudo dnf install -y llvm-toolset

If you only want the compiler, sudo dnf install -y clang is enough. Either way, dnf also installs gcc-c++ and the libstdc++ headers as dependencies. Clang on Linux uses GCC's C++ standard library and runtime files by default, which on CentOS 6 had to be added by hand.

Check the versions:

clang --version
rpm -q llvm clang lld

Useful additional packages from the same repository:

PackageWhat it provides
clang-tools-extraclang-tidy, clang-format, clangd and related tools
clang-analyzerThe static analyzer front end, scan-build
lldbThe LLVM debugger
compiler-rtRuntime libraries for the sanitizers (-fsanitize=address and others)
libomp, libomp-develOpenMP runtime for -fopenmp
llvm-devel, clang-develHeaders and libraries for building software against LLVM
sudo dnf install -y clang-tools-extra clang-analyzer lldb compiler-rt libomp-devel

Compile a test program

Create a small C++ file:

cat > hello.cpp <<'EOF'
#include <iostream>
int main() {
    std::cout << "Hello from Clang " << __clang_version__ << std::endl;
    return 0;
}
EOF

Compile and run it, once with the default linker and once with LLD:

clang++ -O2 -Wall -o hello hello.cpp
./hello
clang++ -O2 -Wall -fuse-ld=lld -o hello-lld hello.cpp
./hello-lld

To build an existing project with Clang instead of GCC, set the compiler variables that most build systems honour:

export CC=clang
export CXX=clang++

With CMake the equivalent is -DCMAKE_C_COMPILER=clang -DCMAKE_CXX_COMPILER=clang++.

Option 2: Use the upstream binary release

If you need a newer LLVM than AppStream offers, the LLVM project publishes pre-built Linux archives for x86-64 and ARM64 with each release on its GitHub releases page. They unpack into a single directory and do not touch the system packages. The x86-64 archive is a download of about 2 GB:

cd /tmp
curl -LO https://github.com/llvm/llvm-project/releases/download/llvmorg-23.1.5/LLVM-23.1.5-Linux-X64.tar.xz
sudo mkdir -p /opt/llvm-23
sudo tar -xJf LLVM-23.1.5-Linux-X64.tar.xz -C /opt/llvm-23 --strip-components=1
/opt/llvm-23/bin/clang --version

Replace the version number with the current one from the releases page. If the last command prints the version, the binaries run on your system and you can put them first in your path for the current shell:

export PATH=/opt/llvm-23/bin:$PATH

If it fails with an error about a missing GLIBC or GLIBCXX version, the archive was built against newer system libraries than RHEL 9 has. In that case build from source instead. Keep the distribution packages installed as well; the upstream Clang still relies on the system's GCC installation for headers and runtime objects.

Option 3: Build the latest release from source

LLVM needs CMake 3.20 or later, Python 3.8 or later and a host compiler at least as new as GCC 7.4. The GCC 11 and the CMake in RHEL 9 meet that, so unlike on CentOS 6 you do not have to bootstrap a compiler first. Ninja comes from the CRB repository:

sudo dnf install -y dnf-plugins-core
sudo dnf config-manager --set-enabled crb
sudo dnf install -y gcc gcc-c++ cmake ninja-build python3 git

Fetch the source for one release tag, without the full history:

git clone --depth 1 --branch llvmorg-23.1.5 https://github.com/llvm/llvm-project.git
cd llvm-project

Configure an optimized build of LLVM, Clang and LLD that installs under /opt/llvm-23:

cmake -S llvm -B build -G Ninja \
  -DCMAKE_BUILD_TYPE=Release \
  -DLLVM_ENABLE_PROJECTS="clang;lld" \
  -DCMAKE_INSTALL_PREFIX=/opt/llvm-23

Build and install. Ninja uses all CPU cores by default:

cmake --build build
sudo ninja -C build install

Always pass -DCMAKE_BUILD_TYPE=Release unless you intend to debug LLVM itself; a debug build is far larger and slower. If the machine runs out of memory during linking, reduce the number of parallel jobs with cmake --build build -j 2. Other subprojects such as clang-tools-extra or lldb can be added to the LLVM_ENABLE_PROJECTS list.

Verify and switch between versions

Each installation is self-contained, so you choose one by path:

/usr/bin/clang --version
/opt/llvm-23/bin/clang --version
which clang

Leave /usr/bin/clang alone and do not overwrite it with links to a self-installed version; packages that depend on the system compiler expect the packaged one. For a single project, set CC and CXX to the full paths under /opt/llvm-23/bin instead.

Maintenance

  • The AppStream packages are updated with sudo dnf upgrade. Expect the major LLVM version to change with a new minor release of the distribution, and rebuild software that links against llvm-devel afterwards.
  • A toolchain under /opt is yours to maintain. To update it, install the new release into a new directory, switch your path, and delete the old directory when nothing uses it.
  • To remove the packaged toolchain: sudo dnf remove llvm-toolset clang llvm lld.

References: the LLVM getting started guide for build options and the LLVM releases page for current versions.

Frequently asked questions

Is there an official LLVM package repository for RHEL, AlmaLinux or Rocky Linux?

No. The LLVM project's own package repository, apt.llvm.org, serves Debian and Ubuntu only. On the RHEL family your choices are the AppStream packages, the upstream binary archive or a source build.

Do I still need GCC if I compile everything with Clang?

Yes, in the default setup. Clang on Linux links against libstdc++ and uses GCC's startup files and libgcc, which is why the clang package depends on gcc-c++. Using LLVM's own libc++ and compiler-rt instead is possible but means building those components yourself.

Can I use these instructions on CentOS 6 or 7?

Not sensibly. Both are end-of-life, receive no security fixes, and their compilers and CMake are too old to build current LLVM without first bootstrapping newer tools. Move the workload to a RHEL 9 family system, or build inside a container based on one.

Which option should I choose?

Use the AppStream packages unless you have a concrete reason not to: they are maintained, tested with the rest of the system and updated automatically. Pick the upstream release only when you need a language feature, sanitizer or tool that the packaged version lacks.

Share story:

Continue reading other guides on How VPS.