How can I install clang-format in Ubuntu?

Viewed 105714

I am trying to use clang-tools in particular clang-format for automatic code formatting in vim but I couldn't find this tool with apt-get search.

Is there anybody experienced this problem before, do you have any suggestion?

8 Answers

Nowadays, you can directly use apt install clang-format in Debian/Ubuntu to install clang-format. However, the clang-format provided by Debian/Ubuntu is quiet old. For example, the latest version of clang-format from Ubuntu 18.04 is v7 but the latest stable version is already reached v12. You can install the latest version of clang-format by following the steps below:

Step 1: Run wget -O - https://apt.llvm.org/llvm-snapshot.gpg.key|sudo apt-key add -

Step 2: Append the following content at the end of /etc/apt/sources.list based on your operating system:

  • Buster (Debian 10):

    deb http://apt.llvm.org/buster/ llvm-toolchain-buster main
    deb-src http://apt.llvm.org/buster/ llvm-toolchain-buster main
    # 11 
    deb http://apt.llvm.org/buster/ llvm-toolchain-buster-11 main
    deb-src http://apt.llvm.org/buster/ llvm-toolchain-buster-11 main
    # 12 
    deb http://apt.llvm.org/buster/ llvm-toolchain-buster-12 main
    deb-src http://apt.llvm.org/buster/ llvm-toolchain-buster-12 main
    
  • Bullseye (Debian 11):

    deb http://apt.llvm.org/bullseye/ llvm-toolchain-bullseye main
    deb-src http://apt.llvm.org/bullseye/ llvm-toolchain-bullseye main
    # 11 
    deb http://apt.llvm.org/bullseye/ llvm-toolchain-bullseye-11 main
    deb-src http://apt.llvm.org/bullseye/ llvm-toolchain-bullseye-11 main
    # 12 
    deb http://apt.llvm.org/bullseye/ llvm-toolchain-bullseye-12 main
    deb-src http://apt.llvm.org/bullseye/ llvm-toolchain-bullseye-12 main
    
  • Xenial (Ubuntu 16.04):

    deb http://apt.llvm.org/xenial/ llvm-toolchain-xenial main
    deb-src http://apt.llvm.org/xenial/ llvm-toolchain-xenial main
    # 11
    deb http://apt.llvm.org/xenial/ llvm-toolchain-xenial-11 main
    deb-src http://apt.llvm.org/xenial/ llvm-toolchain-xenial-11 main
    # 12
    deb http://apt.llvm.org/xenial/ llvm-toolchain-xenial-12 main
    deb-src http://apt.llvm.org/xenial/ llvm-toolchain-xenial-12 main
    
  • Bionic (Ubuntu 18.04):

    deb http://apt.llvm.org/bionic/ llvm-toolchain-bionic main
    deb-src http://apt.llvm.org/bionic/ llvm-toolchain-bionic main
    # 11
    deb http://apt.llvm.org/bionic/ llvm-toolchain-bionic-11 main
    deb-src http://apt.llvm.org/bionic/ llvm-toolchain-bionic-11 main
    # 12
    deb http://apt.llvm.org/bionic/ llvm-toolchain-bionic-12 main
    deb-src http://apt.llvm.org/bionic/ llvm-toolchain-bionic-12 main
    
  • Focal (Ubuntu 20.04)

    deb http://apt.llvm.org/focal/ llvm-toolchain-focal main
    deb-src http://apt.llvm.org/focal/ llvm-toolchain-focal main
    # 14
    deb http://apt.llvm.org/focal/ llvm-toolchain-focal-14 main
    deb-src http://apt.llvm.org/focal/ llvm-toolchain-focal-14 main
    # 15
    deb http://apt.llvm.org/focal/ llvm-toolchain-focal-15 main
    deb-src http://apt.llvm.org/focal/ llvm-toolchain-focal-15 main
    
  • Jammy (Ubuntu 22.04)

    deb http://apt.llvm.org/jammy/ llvm-toolchain-jammy main
    deb-src http://apt.llvm.org/jammy/ llvm-toolchain-jammy main
    # 14
    deb http://apt.llvm.org/jammy/ llvm-toolchain-jammy-14 main
    deb-src http://apt.llvm.org/jammy/ llvm-toolchain-jammy-14 main
    # 15
    deb http://apt.llvm.org/jammy/ llvm-toolchain-jammy-15 main
    deb-src http://apt.llvm.org/jammy/ llvm-toolchain-jammy-15 main
    

You can check https://apt.llvm.org/ if your operating system is not on this list.

Step 3: Run sudo apt update

Step 4: Run apt search clang-format and check all available versions:

$ sudo apt search clang-format
Sorting... Done
Full Text Search... Done
arcanist-clang-format-linter/stable 0.git20161021-2 all
  clang-format linter for Arcanist

clang-format/unknown 1:13.0-53~20210202214848.38 amd64
  Tool to format C/C++/Obj-C code

clang-format-11/unknown 1:11.1.0~++20210203115409+1fdec59bffc1-1~exp1~20210203230038.161 amd64
  Tool to format C/C++/Obj-C code

clang-format-12/unknown 1:12.0.0~++20210312110334+ca14f0282fce-1~exp1~20210312221110.59 amd64
  Tool to format C/C++/Obj-C code

clang-format-13/unknown 1:13~++20210315063844+b868a3edad9d-1~exp1~20210315174553.2286 amd64
  Tool to format C/C++/Obj-C code

clang-format-6.0/stable 1:6.0.1-10 amd64
  Tool to format C/C++/Obj-C code

clang-format-7/stable 1:7.0.1-8+deb10u2 amd64
  Tool to format C/C++/Obj-C code

Step 5: Use apt install to install the clang-format you want. Then you can use --version to check the installed clang-format. You can install multiple versions of clang-format in a same environment.

$ sudo apt install -y clang-format-12
$ clang-format-12 --version
Ubuntu clang-format version 12.0.0-++20210312110334+ca14f0282fce-1~exp1~20210312221110.59

$ sudo apt install -y clang-format
$ clang-format --version
Ubuntu clang-format version 13.0.0-++20210315063844+b868a3edad9d-1~exp1~20210315174553.2286

This answer has been rewritten since the downvote, and is the most-up-to-date answer to this question as of Apr. 2022. It is also the only answer that explains how to get the latest version of clang-format direct from LLVM, the people who make it.

To install the latest version of clang-format and git-clang-format (runnable as git clang-format):

Summary

  1. To install clang-format in Ubuntu, you can use sudo apt install clang-format, or some variant of that (see below), but it frequently installs a really old version that is missing a ton of features. So:
  2. [Preferred] To install the latest version of clang-format in Ubuntu, you must get it from the official release pages on GitHub directly. I explain how to do this in detail below.

[Old method] To install an old version of clang-format:

Do:

sudo apt update

And then try, in this order, one-at-a-time, until one works:

sudo apt install clang-format
sudo apt install clang-format-14.0
sudo apt install clang-format-13.0
sudo apt install clang-format-12.0
sudo apt install clang-format-11.0
sudo apt install clang-format-10.0
sudo apt install clang-format-9.0
sudo apt install clang-format-8.0
sudo apt install clang-format-7.0
sudo apt install clang-format-6.0
sudo apt install clang-format-5.0
sudo apt install clang-format-4.0
sudo apt install clang-format-3.6
sudo apt install clang-format-3.4
sudo apt install clang-format-3.0

On Ubuntu 14.04, for instance, the first command above that works is sudo apt install clang-format-3.6. As of Ubuntu 16.04, I believe, sudo apt install clang-format works, but no matter what version of Ubuntu you're on, Ubuntu 20.04 included, sudo apt install clang-format installs a quite-outdated version (ex: version 6.0.0 on Ubuntu 18.04). So, to get the latest version of clang-format, keep reading.

[Preferred method] How to install the latest version of clang-format on Ubuntu

The super quick instructions to get a recent version

To get clang-format and git-clang-format from LLVM (the parent organization which makes the clang C and C++ compiler, as well as those tools) directly, follow my instructions farther below. However, that requires downloading the entire compressed clang compiler toolset release which is ~600 MB, and extracting it into a folder which is ~5 GB when fully extracted, just so you can copy out a couple megabytes of these executables. That takes some time.

So, if you're in a huge hurry, and if you want to trust executables from my personal repo, I keep a recent version (14.0.0 or later) of both clang-format and git-clang-format in the bin dir of my repo here: https://github.com/ElectricRCAircraftGuy/eRCaGuy_CodeFormatter/tree/main/bin. It is frequently not recommended to trust other peoples' executables, however, so I provide the exact method and instructions below which I used to extract those executables from the original release from LLVM (the maker of clang-format), which you can follow yourself if you like instead.

If you'd like to quickly download and install clang-format and git-clang-format from my personal repo, do this:

wget https://github.com/ElectricRCAircraftGuy/eRCaGuy_CodeFormatter/raw/main/bin/clang-format
wget https://github.com/ElectricRCAircraftGuy/eRCaGuy_CodeFormatter/raw/main/bin/git-clang-format
chmod +x clang-format git-clang-format
mkdir -p ~/bin
mv clang-format ~/bin
mv git-clang-format ~/bin

# log out of Ubuntu and log back in

# ensure it worked and you now have a later version
clang-format --version  

# Check the help menus
clang-format -h
git clang-format -h
# OR (same thing as the line just above):
git-clang-format -h

The full instructions to get the latest version from LLVM, the makers, directly:

This requires downloading the entire LLVM clang C and C++ compiler toolset, which is a compressed file ~600 MB in size, and then extracting it into a folder which is ~5 GB in size when extracted, just so you can copy out a few magabytes of executables from within it.

These instructions were originally posted in the README of my eRCaGuy_CodeFormatter repo here: https://github.com/ElectricRCAircraftGuy/eRCaGuy_CodeFormatter#installation-instructions

The full steps to get the latest clang-format and git-clang-format from the latest official LLVM release:

  1. Go to the LLVM official clang releases page: https://github.com/llvm/llvm-project/releases
  2. Find the latest binary release for your operating system. Example: for 64-bit Linux, it is currently (as of Mar. 2022): clang+llvm-14.0.0-x86_64-linux-gnu-ubuntu-18.04.tar.xz. The download link is: https://github.com/llvm/llvm-project/releases/download/llvmorg-14.0.0/clang+llvm-14.0.0-x86_64-linux-gnu-ubuntu-18.04.tar.xz
  3. Use the link you found above for the next commands:
    url="https://github.com/llvm/llvm-project/releases/download/llvmorg-14.0.0/clang+llvm-14.0.0-x86_64-linux-gnu-ubuntu-18.04.tar.xz"
    # download it; be patient: the file is ~600 MB
    wget "$url"
    # extract it; be patient: this could take several minutes, as the file is
    # about 5 GB when extracted! On my high-speed computer with SSD, it took
    # ~1 minute
    time tar xf clang+llvm*.tar.xz
    # cd into the bin dir
    cd clang+llvm*/bin
    # make a ~/bin dir if it doesn't exist yet
    mkdir -p ~/bin
    # copy out `clang-format` into your ~/bin dir
    cp clang-format ~/bin
    # copy out `git-clang-format` into your ~/bin dir
    cp git-clang-format ~/bin
    
    # Manually edit your ~/.profile file to ensure it contains the following in
    # order to ensure ~/bin is part of your executable PATH variable. This is
    # part of your default Ubuntu ~/.profile file (for which you have a backup
    # copy in /etc/skel/.profile):
    #
    #       # set PATH so it includes user's private bin if it exists
    #       if [ -d "$HOME/bin" ] ; then
    #           PATH="$HOME/bin:$PATH"
    #       fi
    
    # Now, if this is your first time creating and using the ~/bin dir, log out
    # of Ubuntu and log back in. 
    
    # check your clang-format version to ensure it shows the version you just
    # installed
    clang-format --version
    # Ensure it is found in your ~/bin dir; my output to this command is:
    # `/home/gabriel/bin/clang-format`
    which clang-format
    # Check `git-clang-format` too
    which git-clang-format
    
    # Check the help menus
    clang-format -h
    git clang-format -h
    # OR (same thing as the line just above):
    git-clang-format -h
    
    # manually delete the the extracted folder if desired, and the
    # downloaded *.tar.xz file as well, if desired
    

Note that git has a pretty neat feature that causes any executable in your path which begins with git- to automatically be treated as a git command. Whenever you run git some_command, git automatically searches all of your system's PATH variable for an executable named git-some_command. If it exists, git runs it. So, simply by virtue of the fact that you put an executable in your path named git-clang-format, git allows you to run it as git clang-format (withOUT the dash after git). Optionally, of course, you can also just run that same executable as git-clang-format, since that's what its filename is.

See below for recommended git clang-format usage and workflow.

Additional setup & usage info & resources:

  1. If you followed the steps just above, you now have the latest version fo both clang-format and git-clang-format. Having the latter allows you to run git clang-format as a git command to auto-format your files before you commit them in git. git-clang-format is a Python script written by LLVM, the makers of the clang C and C++ compiler and clang-format. It is located in the official LLVM GitHub repository here: https://github.com/llvm/llvm-project/blob/main/clang/tools/clang-format/git-clang-format. Place it into your PATH; ex: in a file called ~/bin/git-clang-format, and mark this file as executable (chmod +x ~/bin/git-clang-format).
    1. The recommended git workflow to call and use this git-clang-format auto-formatter would then be:
      # See what git changes you have
      git difftool  
      # OR: `git diff` if you haven't configured a difftool such as meld
      git diff
      # Add (stage) a C or C++ file to be committed
      git add my_changed_file.c 
      
      # Run `git-clang-format` to have `clang-format` check and
      # auto-format **just your changed lines**. (This runs the
      # `~/bin/git-clang-format` Python script).
      git clang-format
      # See what changes `clang-format` just made to your changed lines
      git difftool
      # OR
      git diff
      
      # Add (stage) this file again since it's been changed by
      # `git-clang-format`
      git add my_changed_file.c 
      
      # commit the changed file
      git commit                
      
  2. Other, potentially-useful info.:
    1. git-clang-format python script setup instructions: https://dx13.co.uk/articles/2015/4/3/Setting-up-git-clang-format.html
    2. git clang-format usage and workflow instructions: https://electronjs.org/docs/development/clang-format
  3. Update Apr. 2020: I just got clang-format up and running fully on a project on GitHub. I run it with ./run_clang-format.sh; here's how:
    1. I wrote some instructions here: https://github.com/AmboVent-1690-108/AmboVent#setup
    2. Here's the PR where I added everything: https://github.com/AmboVent-1690-108/AmboVent/pull/39. You can take a look to see how I did it all.
    3. I borrowed from my notes and files I have in my dotfiles project here: https://github.com/ElectricRCAircraftGuy/eRCaGuy_dotfiles/tree/master/clang-format
  4. Update Apr. 2022: I have this repo now too: https://github.com/ElectricRCAircraftGuy/eRCaGuy_CodeFormatter

pip3 install clang-format gives me the latest version 9.0 on ubuntu 18.04 with zero configuration

Just use command below:

sudo apt-get install clang-format

It works in ubuntu16.04(install Setting up clang-format-3.8 (1:3.8-2ubuntu4))

when I try

sudo apt-cache search clang-format
# it return 
clang-format - Tool to format C/C++/Obj-C code
clang-format-3.5 - Tool to format C/C++/Obj-C code
clang-format-3.6 - Tool to format C/C++/Obj-C code
clang-format-3.7 - Tool to format C/C++/Obj-C code
clang-format-3.8 - Tool to format C/C++/Obj-C code
clang-format-3.9 - Tool to format C/C++/Obj-C code
clang-format-4.0 - Tool to format C/C++/Obj-C code
clang-format-5.0 - Tool to format C/C++/Obj-C code
clang-format-6.0 - Tool to format C/C++/Obj-C code
clang-format-8 - Tool to format C/C++/Obj-C code

then try

sudo apt-get install clang-format-8

There is no command clang-format in ENV

This is an update, because I had troubles with the suggestions above on Ubuntu 21.04

sudo apt install clang-format installs v.12 and I needed anything older.

sudo apt install clang-format-9.0 won't work. ("Package wasn't found")

This worked fine: sudo apt install clang-format-9

You can do

python3 -m pip install --user clang-format

This will install a precompiled binary from https://pypi.org/project/clang-format/. It is quite practical if you don't have sudo access, or if you need clang-format for a pre-commit hook.

Related