Last active
April 28, 2025 16:35
-
-
Save sauparna/de6628ebf7c029173bae654f1294f90b to your computer and use it in GitHub Desktop.
Install LLVM/Clang on Ubuntu 24.04
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
The message to share is that, on Ubuntu 24.04, to enable LLVM/Clang, run apt install llvm clang g++-14. If you want to set up a custom toolchain using a particular version of LLVM, it will require much more work. The reasoning are in a gist, if at all it's worth the reader's time: | |
1. On Linux, using an LLVM version of one's choice is fairly straightforward. Download the appropriate tarball from https://releases.llvm.org, extract it, (the extracted directory contains the sub-directories `lib` and `bin`), place the directory somewhere, and add the path to the directory's `bin` directory to your `PATH` variable. The LLVM tarball comes with Clang. | |
2. That `bin` directory contains the tool `llvm-config` (or `llvm-19-config`, say), which when run, will tell you what flags to use to compile and link your C/C++ programs correctly. At this point, if you are using `clang++` to compile C++ program, and linking it to LLVM's C++ standard-library in this way `clang++ -stdlib=libc++ f.cpp`, it will work, but running the executable will complain about the missing shared library `libc++.so.1`. The LLVM tarball does not come with the standard C++ library binaries, so that leaves more work for you to do. You will have to fetch the libc++ source (which probably entails fetching `libcxx`, `libcxxabi`, `libunwind` if I am correct) and build them from source. An alternative is to use `apt install libc++` to obtain the C++ standard-libraries, but they will be routed to your system's standard locations and will not match the particular version you are trying to work with. | |
3. On Ubuntu 24.04, if I wish to install LLVM/Clang from the standard distribution packages, using `apt install llvm`, it installs version 18. If you want version 19, you could run `apt install llvm-19`, but that will not install `clang`. `/usr/lib/llvm-19/bin/` will have the binary `clang-19` but not `clang`. I haven't tried thinking up a work-around, but whatever you could do about that will lead to a non-standard solution, defeating the purpose of having your distribution lay out the toolchain for you. Try `sudo apt install --dry-run llvm*` to get a sense of all the LLVM versions your distribution offers. `apt install libc++` will fetch you the version 18 C++ standard-libraries. | |
4. On Ubuntu 24.04, after you have installed LLVM/Clang, and not having touched the C++ libraries, compiling a C++ program using `clang++` will complain that the use of the compiler flag `-lstdc++` fails to find the standard C++ libraries. This is because since Linux has the GCC toolchain, clang choses to use GCC's standard libraries automatically. If your run `clang -v` it will tell you that it has found GCC versions 13 and 14, and that it has chosen GCC version 14. Check for yourself that there are two directories, `13` and `14`, in `/usr/lib/gcc/x86_64-linux-gnu/`. Oddly, the GCC 14 directory does not have the C++ libraries. So you install it yourself `apt install g++-14`. This should complete setting up a the LLVM/Clang toolchain. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment