How to Install Clang 16, 17 and/or 18 in Debian 12

Photo by Clément Hélardot on Unsplash

This tutorial is going to show you how to install the most recent Clang compiler in Debian Linux.

Debian includes several versions of Clang packages in system repositories, but they are old. For C, C++, and Objective-C developers need the most recent Clang compiler, it has an official repository for Debian and Ubuntu.

At the moment of writing this tutorial, the most recent versions include: Clang-16 stable, Clang-17 qualification branch, and Clang-18 that’s still in development. And, here provides 2 ways to install them in Debian and Ubuntu.

Method 1: Install Clang via its official script

There’s an official script to make the installation quite easy! It just works, though deprecated by Debian’s new policy, since it’s still using the old insecure way to install the key and source file.

First, search for and launch a terminal window from application menu or ‘Activities’ overview depends on your desktop environment.

Launch Terminal
  1. When terminal opens, run command to download the script:
    wget https://apt.llvm.org/llvm.sh

    In case you don’t have wget, install it via sudo apt install wget command.

  2. After downloading the script, add executable permission by running command:
    chmod u+x llvm.sh
  3. Finally, run the script to install Clang (change 17 to 16 or 18 depends on your need):
    sudo ./llvm.sh 17

The script will automatically add the official repository into your system (need to press Enter to confirm), update package cache, and finally install Clang compiler with apt command.

Method 2: Manually add the apt repository & install Clang

As mentioned, ‘Method 1’ so far still uses the old deprecated way for adding the repository and key. To follow the new policy, here are the more secure way to do the job.

First, if you’ve already done ‘Method 1’, then you need to first run 2 commands to remove the previously added source and key files:

sudo rm /etc/apt/sources.list.d/archive_uri-http_apt_llvm_org*.list
sudo rm /etc/apt/trusted.gpg.d/apt.llvm.org.asc

1. Setup the key

Also, launch terminal (or konsole) from application menu, then run command to download the key file:

wget https://apt.llvm.org/llvm-snapshot.gpg.key -O apt.llvm.org.asc

Run sudo apt install wget if wget command not exist.

After that, you have the new ‘apt.llvm.org.asc‘ key file stored in current terminal working directory. To install it, just de-armor and move into /etc/apt/keyrings directory

  1. To make it more secure, dearmor it, so to generate a new ‘apt.llvm.org.gpg‘ key file with un-readable garbled text.
    gpg --output apt.llvm.org.gpg --dearmor apt.llvm.org.asc
  2. You may then remove the previous key file, and move the new one to /etc/apt/keyrings directory.
    sudo mv apt.llvm.org.gpg /etc/apt/keyrings/

    Run sudo mkdir -p /etc/apt/keyrings/ first, in case the directory does not exist.

For choice, you may run the single command instead, to download the key, de-armor, and install to destination directory:

wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key | gpg --dearmor | sudo tee /etc/apt/keyrings/apt.llvm.org.gpg

2. Add the sources file

Also in a terminal window, run command to create a new sources file and edit it via nano text editor:

sudo nano /etc/apt/sources.list.d/apt.llvm.org.sources

When file opens in terminal, add following lines:

Types: deb
URIs: https://apt.llvm.org/bookworm/
Suites: llvm-toolchain-bookworm llvm-toolchain-bookworm-17 llvm-toolchain-bookworm-16
Components: main
Signed-By: /etc/apt/keyrings/apt.llvm.org.gpg

In the file, replace bookworm (after URIs & Suites) with bullseye for Debian 11, buster for Debian 10, or jammy for Ubuntu 22.04, focal for Ubuntu 20.04.

There are 3 items in Suites line. Depends on your need, you may skip any one or two of them.

Finally, press Ctrl+X, type y and hit Enter to save the file.

3. Install Clang

After setting up the key and sources file, run command to refresh system package cache:

sudo apt update

And, finally install Clang (replace number 17 depends on your need) by running command:

sudo apt install clang-17 lldb-17 lld-17 clangd-17

For choice, install all the packages by running command:

sudo apt install clang-17 lldb-17 lld-17 clangd-17 clang-tidy-17 clang-format-17 clang-tools-17 llvm-17-dev llvm-17-tools libomp-17-dev libc++-17-dev libc++abi-17-dev libclang-common-17-dev libclang-17-dev libclang-cpp17-dev libunwind-17-dev libclang-rt-17-dev libpolly-17-dev

After installation, verify by running commands:

clang-17 --version
locate clang-17

Uninstall:

To the official apt repository, just launch terminal and run command to remove that source file:

sudo rm /etc/apt/sources.list.d/apt.llvm.org.sources
sudo rm /etc/apt/sources.list.d/archive_uri-http_apt_llvm_org*.list

To remove the key file, run command:

sudo rm /etc/apt/keyrings/apt.llvm.org.gpg
sudo rm /etc/apt/trusted.gpg.d/apt.llvm.org.asc

Finally, run sudo apt update to refresh system cache.

To remove the Clang packages you installed, run command (replace 17 according which version you installed):

sudo apt remove --autoremove clang-17 lldb-17 lld-17 clangd-17

That’s all. Enjoy!

Exit mobile version