Install Clang 9 on Ubuntu

2 minute read

Here, I will introduce you to a nice and easy way to install the latest version of Clang with the latest libc++ implementation. The procedure will take Ubuntu 16.04 x86_64 system as an example, but it should similarly work for 14.04 and 18.04. A quick shout to Solarian Programmer, this post is inspired by ( and many parts simply copied from ) his clang8 installation on Linux post. Check out his posts!

So open your favorite terminal and lets first sure our system is up to date.

sudo apt-get update
sudo apt-get upgrade

A few prerequisites are necessary to run Clang, namely:

sudo apt-get install build-essential xz-utils curl

Next up, we download necessary binaries from clang releases archive and extract them.

curl -SL http://releases.llvm.org/9.0.0/clang%2bllvm-9.0.0-x86_64-linux-gnu-ubuntu-16.04.tar.xz | tar -xJC .

# For Ubuntu 18.04: 
# curl -SL http://releases.llvm.org/9.0.0/clang%2bllvm-9.0.0-x86_64-linux-gnu-ubuntu-18.04.tar.xz | tar -xJC . 
# For Ubuntu 14.04: 
# curl -SL http://releases.llvm.org/9.0.0/clang%2bllvm-9.0.0-x86_64-linux-gnu-ubuntu-14.04.tar.xz | tar -xJC .

Visit the links for more information on -S and -L, cURL flags might be a bit overwhelming sometimes…

Next up: renaming the download folder as its name is a bit too long and moving the binaries to a more fitting directory.

mv clang+llvm-9.0.0-x86_64-linux-gnu-ubuntu-16.04 clang_9.0.0
sudo mv clang_9.0.0 /usr/local

# For Ubuntu 18.04: 
mv clang+llvm-9.0.0-x86_64-linux-gnu-ubuntu-18.04 clang_9.0.0
sudo mv clang_9.0.0 /usr/local

# For Ubuntu 14.04: 
mv clang+llvm-9.0.0-x86_64-linux-gnu-ubuntu-14.04 clang_9.0.0
sudo mv clang_9.0.0 /usr/local

So now the binaries are now in /usr/local. Of course if you feel like it, you could move it to some other place!

Now lets tell our system where clang-9 is, so when we call the binaries or try to link our programs, it will find its way.

To do that, you can add the following at the end of your .bashrc or .zshrc or any other cool shells’ rc:

export PATH=/usr/local/clang_9.0.0/bin:$PATH
export LD_LIBRARY_PATH=/usr/local/clang_9.0.0/lib:$LD_LIBRARY_PATH

And that should be it! The cool new C++20 features that clang 9 offers are now at your disposal. Lets try them.

Taking a quick look at clang implementation status for C++2a, a new C++20 feature called “Pack expansion in lambda init-capture” drew my attention. So a small and pointless demonstration is as follows:

#include <utility>
#include <string>
#include <iostream>

template<class... T>
auto pack(T... t)
{
    // Now legal in  C++20
    return [...t=std::move(t)](auto&& f){f(t...);};
};

// Forgive me Kate Gregory
void f(int age, const std::string& name )
{
    std::cout <<"I am " << name <<" and am " <<age <<" years old\n";
}
int main(int argc,char** argv )
{
    if ( argc != 3)
    {
        std::cerr << " Provide my age then my name\n";
        return -1;
    }

    int age = std::stoi(argv[1]);
    std::string name =  argv[2];
    auto func = pack(age,name);
    func(f);
    return 0;
}

As the name suggests, it lets us capture a parameter pack inside lambda init, essentially letting delayed evaluation of functions in a rather generic way. As this post is not really about this feature, i wont try to go deeper into it.( not that I can go much deeper. ) For those interested, take a look at the proposal.

All right, now that we saved the above code, we are ready to try out our new compiler!

We compile the trial.cpp with necessarily -std=c++2a to enable the feature:

clang++ -stdlib=libc++ -std=c++2a -Wall trial.cpp -o trial

And hopefully your experience with the above code will be something like this:

~ $ clang++ -stdlib=libc++ -std=c++2a -Wall trial.cpp -o trial
~ $ ./trial 25 oguz
I am oguz and am 25 years old
~ $

Hope this short installation guide for clang-9 was helpful, here is another implemented C++20 features that you might be interested:

Categories:

Updated: