Steps To Install Clang LLVM on Ubuntu 22.04

This guide intends to teach you to Install the Clang LLVM Compiler on Ubuntu 22.04. Clang is a compiler for the C, C++, and Objective-C programming languages. It is a part of the Low-Level Virtual Machine (LLVM) project. Clang will process the source code and LLVM will generate the machine code. The Clang LLVM project is a good option because of the amazing features it has including performance, compatibility, extensibility, and open-source.

A Complete Guide To Install Clang LLVM Compiler on Ubuntu 22.04

In this guide, we will show you how to install Clang from the APT package repository and use the LLVM script to get the latest Clang on Ubuntu 22.04. Set the clang default version and create a sample project with it.

What are the requirements for installing Clang?

You must log in to your Ubuntu server as a non-root user with sudo privileges. To do this, you can check the Ubuntu 22.04 Initial Server Setup guide.

Also, ensure you have enough disk space and an internet connection. Then, proceed to the following steps to complete this guide.

Step 1 – Installing Clang From Ubuntu 22.04 APT Repository

You can easily get Clang from the APT package manager on Ubuntu. To do this, run the system update and install Clang with the following commands:

# sudo apt update
# sudo apt install clang -y

When your installation is completed, verify it by checking the Clang version:

clang --version
Check Clang default version Ubuntu 22

As you can see, the default version installed on Ubuntu 22.04 is Clang 14. If you want to get the latest version, you can proceed to the next step and use the LLVM script.

Step 2 – Installing Clang with LLVM Script on Ubuntu 22.04

At this point, you can use the LLVM apt repository script to get the latest Clang version. To do this, use the following wget command to download the script:

sudo wget https://apt.llvm.org/llvm.sh
Download LLVM Script to get latest Clang

Once your download is completed, make your file executable with the following command:

sudo chmod +x llvm.sh

Then, you can use the following command to install your specific version of Clang:

sudo ./llvm.sh <clang version number>

At the current time, the latest Clang version is 17. To install it, you can run:

sudo ./llvm.sh 17

Press enter to start your installation process. This will install all required packages and dependencies.

When your installation is completed, verify your Clang version. You will see that your Clang version is still the default Clang 14. To set the default version to the latest, proceed to the next step.

Step 3 – Set the Latest Clang Version as the Default Compiler

At this point, you can easily set your latest Clang version as your default compiler on Ubuntu 22.04. To do this, you can run the following commands:

# sudo update-alternatives --install /usr/bin/clang++ clang++ /usr/bin/clang++-17 100
# sudo update-alternatives --install /usr/bin/clang clang /usr/bin/clang-17 100

Note: In the above commands, you can replace the version number with your specific version. The number 100 is the priority we have set for Clang.

If you want to use Clang instead gcc, you can run the command below:

sudo update-alternatives --config cc

Now if you check your Clang version, you should see that it is set to your specific version:

clang --version
Set the Latest Clang Version as the Default Compiler

Step 4 – Create a Test Program with Clang LLVM Compiler

At this point, you can use Clang to compile your C and C++ codes. To see if it is working correctly, we created a sample Hello World project. Open a new C file with your desired text editor like Vi editor or Nano editor:

sudo vi hello.c

Add the following sample code to the file:

#include <stdio.h>

int main() {
printf("Hello, World!\n");
return 0;
}

When you are done, save and close the file.

Use Clang to Compile the C Program

At this point, you can easily use the Clang compiler to compile your program on Ubuntu 22.04. To do this, run the command below:

sudo clang hello.c -o hello

This will compile your code and make an executable file named hello. To run the program, you can simply run:

sudo ./hello

In your output, you will see the Hello World message.

Example Output
Hello, World!

That’s it, you are done. To get more information, you can visit the official documentation page.

Conclusion

Clang LLVM is a popular compiler among developers because of its performance, user-friendly, and new compiler technology. At this point, you have learned to install Clang with APT package manager, Use LLVM to get the latest version, Set the default version, create a test program, and compile it with Clang on Ubuntu 22.04.

Hope you enjoy using it. Also, you may interested in these articles:

Install Rust Programming Language on Ubuntu 22.04

Install Scala in Ubuntu Terminal Command Line

5 Best Free Alternatives To Python

Install Python 3.12 on Ubuntu and Debian Server

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Latest Articles

POPULAR TAGS

Most Popular