Whether you are using Ubuntu 22.04, 20.04, or any other and want to install Clang, an open-source C, C++, and Objective-C compiler then this article will help you. Clang is a drop-in replacement for GNU Compiler Collection, that is better, much faster, and uses less memory than GCC. Therefore, become a robust and efficient compiler for developers for optimizing their code and building high-performance applications.
What do we need?
To follow this tutorial on your system, ofcourse you must have a Debian bases Linux such as Ubuntu along with that Administrative (sudo) privileges and an active internet connection for smooth installation.
Using the APT Package Manager:
Go to your Ubuntu Terminal which can be accessed using the keyboard shortcut i.e. Ctrl+Alt+T. There execute the given command first, which updates the system packages and refreshes the APT packages list.
sudo apt update && sudo apt upgrade
#1st method for default version available through Ubuntu
Install Clang on Ubuntu linux
Like most of the popular developer tools, Clang is also present to download and install through the default package repositories of Ubuntu. Therefore, to install it just use the APT command given below in your terminal.
sudo apt install clang

Check Version
Although successful execution of the previous command will show that you have the Clang on your system without any issues, yet to confirm here is the command to check its version and verify the same.
clang --version
#2nd method- for the latest or older versions of Clang
Use LLVM repository script for Clang
Get the latest or any other version
Well, the previous step’s command will provide you with version 14 of Clang which was available through the Ubuntu 22.04 repo by default while performing this tutorial. However, those who want the latest version or even some older one can run the given commands:
wget https://apt.llvm.org/llvm.sh
chmod +x llvm.sh
Now, if you want some specific version, you can use the given command to add the repository corresponding to that
sudo ./llvm.sh <version number>
For example, I want Clang-16 to be on my system, so the above command will be:
sudo ./llvm.sh 16
Set Clang Version as the default compiler
Now, by default system will not take the latest version of Clang as the system’s global version to compile your codes. So, use the given commands to set your preferred version as the default one:
For example, while writing this article the latest stable version of Clang was 16, so to set it as the default one we used the Update-alternatives command:
sudo update-alternatives --install /usr/bin/clang++ clang++ /usr/bin/clang++-16 100
sudo update-alternatives --install /usr/bin/clang clang /usr/bin/clang-16 100
Note: Change version 16 with whatever you have installed on your system in the above commands. Whereas, the 100 in the above command is the priority we have set for Clang.
[optional] Also, those who want to configure ‘make‘ utility that uses the command cc to compile, use Clang instead gcc, can run:
sudo update-alternatives --config cc
Type the Selection number of Clang and press the Enter key.
Check version:
Now, you can again check the default version of Clang, to confirm the version you want is on your system.
clang --version

Example: Create the C program
Let’s see how to use Clang to compile your C or C++ codes. Here is the common and simplest- hello-world program example.
Open a text editor (e.g., Nano or Vim) and create a new file named hello.c with the following content:
nano hello.c
add this code
#include <stdio.h>
int main() {
printf("Hello, World!\n");
return 0;
}
Save the file and exit the text editor by pressing Ctlr+X after that, type Y and hit the Enter key.
Compile the C program using Clang
Open a terminal and navigate to the directory where the hello.c file is located, however, you already would be in the same directory where you have created it. Therefore, to compile the program with Clang, use the following command:
clang hello.c -o hello
This command tells Clang to compile the hello.c source file and create an executable named hello.
Run the compiled program
After successfully compiling the program, you can run it by executing the following command in the terminal:
You should see the output:
Hello, World!
So, this was the quick tutorial on how to install the latest version of Clang and then use it to compile and run a C program on Ubuntu. The simplicity and its ability to support C, C++, and Objective-C, as well as the OpenMP, OpenCL, RenderScript, CUDA, SYCL, and HIP frameworks provide a seamless development experience to programmers.
Thank you! That was very helpful.