In this article, we will see how to install clang tool on Ubuntu or Debian Linux. clang is a free and open source tool designed under project LLVM to mostly replace the gcc compiler. It works exactly as gcc with only difference between the two is that clang defaults to gnu99 mode while gcc defaults to gnu89 mode. clang provides fast compilation of C/C++ programs with low memory usage. It is known to be a real world, production quality compiler. It has modular library based architecture which means it separates the front-end, optimizer (LLVM IR), and back-end (target code generation), making it more flexible.
It allows better integration with IDEs, static analysis tools, and debuggers. In comparison to gcc, it provides much better and much clear diagnostic messages that helps understand developers about different errors and messages. It serves as a single unified parser for C, Objective C, C++, and Objective C++. Just like gcc, it is also easy to install in almost all the famous platforms including linux based systems. So here we will see how to install and use clang on Ubuntu or Debian based systems.
How to Install clang tool on Ubuntu or Debian Linux
Also Read: How to Install vLLM on Linux Using 4 Easy Steps
Step 1: Prerequisites
a) You should have a running Ubuntu
or Debian
Server.
b) You should have sudo
or root
access to run privileged commands.
c) You should have apt
or apt-get
utility installed in your system.
Step 2: Update Your Server
Before installing any new package, it is always advisable to update your system with latest bug fixes and feature upgrades by using sudo apt update && sudo apt upgrade
command as shown below. This will keep your system stable and secure.
Ubuntu-Server@ubuntu:~$ sudo apt update && sudo apt upgrade
Step 3: Install Clang
You can easily download and install clang from default ubuntu repo by using sudo apt install clang
command as shown below. This will install the package along with all its dependencies.
Ubuntu-Server@ubuntu:~$ sudo apt install clang [sudo] password for Ubuntu-Server: Reading package lists... Done Building dependency tree... Done Reading state information... Done The following packages were automatically installed and are no longer required: libclang1-18 liblldb-18 libnvidia-compute-550 Use 'sudo apt autoremove' to remove them. The following additional packages will be installed: clang-14 libclang-common-14-dev libclang1-14 llvm-14 llvm-14-dev llvm-14-linker-tools llvm-14-runtime llvm-14-tools Suggested packages: clang-14-doc llvm-14-doc The following NEW packages will be installed: clang clang-14 libclang-common-14-dev libclang1-14 llvm-14 llvm-14-dev llvm-14-linker-tools llvm-14-runtime llvm-14-tools 0 upgraded, 9 newly installed, 0 to remove and 86 not upgraded. Need to get 65.6 MB of archives. After this operation, 442 MB of additional disk space will be used. Do you want to continue? [Y/n] Y ....................................................
Step 4: Check Version
You can check current installed version by using clang --version
command as shown below.
Ubuntu-Server@ubuntu:~$ clang --version Ubuntu clang version 14.0.0-1ubuntu1.1 Target: x86_64-pc-linux-gnu Thread model: posix InstalledDir: /usr/bin
Step 5: Compile a sample program
Now that clang is installed, let’s look at a sample C program and understand its usage. Below C program displays a message "Hi, This is from Ubuntu-Server !!"
on the output.
Ubuntu-Server@ubuntu:~$ nano hello.c #includeint main() { printf("Hi, This is from Ubuntu-Server !!n"); return 0; }
Now let’s compile the above program and generate an executable called hello
by using clang -o hello hello.c
command(same as we used to do with gcc
command earlier) as shown below.
Ubuntu-Server@ubuntu:~$ clang -o hello hello.c
As you can see, there is no error or warning on output, we can now run this program by using ./hello
as shown below. You can see a message displaying on the output. This confirms that clang is able to compile program and generate the executable which means it is working as expected. You can check more about clang usage and examples on official page.
Ubuntu-Server@ubuntu:~$ ./hello Hi, This is from Ubuntu-Server !!
Step 6: Uninstall clang
Once you are done using clang, you can also choose to uninstall it from your system by using sudo apt remove clang
command as shown below.
Ubuntu-Server@ubuntu:~$ sudo apt remove clang Reading package lists... Done Building dependency tree... Done Reading state information... Done The following packages will be REMOVED: clang 0 upgraded, 0 newly installed, 1 to remove and 86 not upgraded. After this operation, 25.6 kB disk space will be freed. Do you want to continue? [Y/n] Y (Reading database ... 255008 files and directories currently installed.) Removing clang (1:14.0-55~exp2) ... Processing triggers for man-db (2.10.2-1) ...
Discover more from Ubuntu-Server.com
Subscribe to get the latest posts sent to your email.