Ubuntu

Install LLVM on Ubuntu 22.04

LLVM is a cross-platform (available on Linux, Windows, and Mac) C/C++ compiler toolset like GCC. LLVM can compile codes written in C, C++, and Objective-C. Clang, provided by the LLVM toolset, can compile C and C++ codes faster than GCC. The LLVM debugger LLDB is more memory efficient and faster at loading symbols compared to GCC. LLVM support C++11, C++14 and C++17 through libc++ and libc++ ABI projects. LLVM also has partial support for the latest C++20 and C++2b standards.

For more information on LLVM, check out the official website of LLVM.

In this article, I will show you how to install the LLVM toolset on Ubuntu 22.04 LTS.

Table of Contents:

  1. Updating APT Package Repository Cache
  2. Installing LLVM on Ubuntu 22.04 LTS
  3. Checking if LLVM Toolset was Installed Correctly
  4. Compiling a C Program with Clang
  5. Compiling a C++ Program with Clang++
  6. Conclusion

Updating APT Package Repository Cache:

First, update the APT package repository cache with the following command:

$ sudo apt update

The APT package repository cache should be updated.

Installing LLVM on Ubuntu 22.04 LTS:

LLVM toolset is available in the official package repository of Ubuntu 22.04 LTS. So, you can easily install it with the APT package manager.

Run the following command to install the LLVM toolset on Ubuntu 22.04 LTS from its official package repository:

$ sudo apt install clang lldb lld

Press Y and then press <Enter> to confirm the installation.

The APT package manager will download all the required packages from the internet. It will take a while to complete.

Once downloaded, the required packages will be installed one by one. It will take a while to complete.

The LLVM toolset should be installed.

Checking if LLVM Toolset was Installed Correctly:

clang and clang++ programs are part of the LLVM toolset. clang is used to compile C programs and clang++ is used to compile C++ programs.

You can check whether the clang program is working correctly with the following code:

$ clang --version

You can check whether the clang++ program is working correctly with the following code:

$ clang++ --version


s

Compiling a C Program with Clang:

In this section, I am going to show you how to compile a simple C program with the clang.

First, create a new C source file hello.c with the nano text editor as follows:

$ nano hello.c

Type in the following lines in the hello.c file. This C program will print the text Hello World on the console once compiled and ran.

Once you’re done, press <Ctrl> + X followed by Y and <Enter> to save the hello.c file.

You can compile a C source file <source-file> and create a binary executable program <output-file> with Clang as follows:

$ clang <source-file> -o <output-file>

You can compile the C source file hello.c into an executable program hello_c as follows:

$ clang hello.c -o hello_c

If there are any errors in the C source file hello.c, it will be reported during the compilation process.

You will see an executable file hello_c in your current working directory as marked in the screenshot below if the C source file hello.c was successfully compiled.

$ ls -lh

You can run the compiled executable file hello_c as follows:

$ ./hello_c

If the hello_c program runs successfully, you should see the text Hello World on the terminal.

Compiling a C++ Program with Clang++:

In this section, I am going to show you how to compile a simple C++ program with the clang++.

First, create a new C++ source file hello.cpp with the nano text editor as follows:

$ nano hello.cpp

Type in the following lines in the hello.cpp file. This C++ program will print the text Hello World on the console once compiled and ran.

Once you’re done, press <Ctrl> + X followed by Y and <Enter> to save the hello.cpp file.

You can compile a C++ source file <source-file> and create a binary executable program <output-file> with Clang++ as follows:

$ clang++ <source-file> -o <output-file>

You can compile the C++ source file hello.cpp into an executable program hello_cpp as follows:

$ clang++ hello.cpp -o hello_cpp

If there are any errors in the C++ source file hello.cpp, it will be reported during the compilation process.

You will see an executable file hello_cpp in your current working directory as marked in the screenshot below if the C++ source file hello.cpp was successfully compiled.

$ ls -lh

You can run the compiled executable file hello_cpp as follows:

$ ./hello_cpp

If the hello_cpp program runs successfully, you should see the text Hello World on the terminal.

Conclusion:

In this article, I have shown you how to install the LLVM toolset on Ubuntu 22.04 LTS. I have also shown you how to compile a simple C and C++ program using Clang and Clang++ (part of the LLVM toolset) respectively and run them.

About the author

Shahriar Shovon

Freelancer & Linux System Administrator. Also loves Web API development with Node.js and JavaScript. I was born in Bangladesh. I am currently studying Electronics and Communication Engineering at Khulna University of Engineering & Technology (KUET), one of the demanding public engineering universities of Bangladesh.