How can I install clang-format in Ubuntu?
Asked Answered
S

9

60

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?

Squint answered 24/12, 2013 at 7:43 Comment(3)
What do you see when your run sudo apt-cache search clang-format?Weighin
actually apt-get could not find it and then i added ubuntu-saucy to the my repository list and it found clang-format.Squint
I just added instructions in my answer here to download the latest binary of clang-format, straight from the clang+llvm source code repository releases page.Continuant
J
20

clang-format is not available in the ubuntu-precise 12.04 but it is available in ubuntu saucy http://packages.ubuntu.com/saucy/clang-format-3.4.

in order to find this package with the apt-cache we have to add below list into our repository list. Actually below list is generated for the singapore servers but if you want to look for your own country you can use http://repogen.simplylinux.ch/generate.php

After generating your list, you have to add them into your repository, you can learn how to do that by looking to here. https://help.ubuntu.com/community/Repositories/CommandLine

The list of packages are;

deb http://sg.archive.ubuntu.com/ubuntu/ saucy main restricted universe multiverse 

deb-src http://sg.archive.ubuntu.com/ubuntu/ saucy main restricted universe multiverse 

deb http://sg.archive.ubuntu.com/ubuntu/ saucy-security main restricted universe multiverse 

deb http://sg.archive.ubuntu.com/ubuntu/ saucy-updates main restricted universe multiverse 

deb http://sg.archive.ubuntu.com/ubuntu/ saucy-proposed main restricted universe multiverse 

deb http://sg.archive.ubuntu.com/ubuntu/ saucy-backports main restricted universe    multiverse 

deb-src http://sg.archive.ubuntu.com/ubuntu/ saucy-security main restricted universe multiverse 

deb-src http://sg.archive.ubuntu.com/ubuntu/ saucy-updates main restricted universe multiverse 

deb-src http://sg.archive.ubuntu.com/ubuntu/ saucy-proposed main restricted universe multiverse 

deb-src http://sg.archive.ubuntu.com/ubuntu/ saucy-backports main restricted universe multiverse 

Then you should search clang-format with below command first

sudo apt-cache search clang-format

Then, you can install which version you want to install such as;

sudo apt-get install clang-format-3.3

Joe answered 25/12, 2013 at 8:46 Comment(4)
sudo is not needed for apt-cache searchOrgell
sudo apt-get install clang-format-3.4 worked in Ubuntu 14.04 LTS. This was after typing: sudo apt-get install clang-3.4Gayden
I used sudo apt install clang-format-3.6 on Ubuntu 14.04Continuant
How do I get the latest version? sudo apt install clang-format installs clang-format version 6, but the documentation pages I can find online are for versions 9 and 11. Why is the Ubuntu version 5 major versions out of date?Lasala
H
62

With Ubuntu 16.04, simply do:

sudo apt install clang-format
Hewet answered 8/4, 2018 at 14:33 Comment(5)
After installing this way I get clang-format: command not found. I proceeded to remove that clang-format package.Gump
@MichaelFulton That works on ubuntu 16.04, not sure whether later version also works.Hewet
On Ubuntu 18.04, the package is used by calling clang-format-6.0 or whatever version you have.Gump
I used sudo apt install clang-format-3.6 on Ubuntu 14.04Continuant
On Ubuntu 18.04, I used: sudo apt install clang-format-5.0 after the installation you can try clang-format-5.0 -h to check if everything went well.Interruption
B
26

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 quite 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
Badmouth answered 16/3, 2021 at 11:13 Comment(4)
Would be nice to have the source for these instructions featured more clearly (apt.llvm.org) Other that that it worked great for me.Doubleminded
Note: Meanwhile it is also possible to install clang-format-13 and clang-format-14 on Ubuntu 18-04. See my answer here.Atrocity
Hi @Badmouth , I need clang-format-7, but when I follow your advice and get to "apt search clang-format" ... Every version is there except for 7. How do I get clang-format-7?Trent
when running step 1 - I get an invisible password prompt which is a bit of a nuisance the first time running the commandMastrianni
J
20

clang-format is not available in the ubuntu-precise 12.04 but it is available in ubuntu saucy http://packages.ubuntu.com/saucy/clang-format-3.4.

in order to find this package with the apt-cache we have to add below list into our repository list. Actually below list is generated for the singapore servers but if you want to look for your own country you can use http://repogen.simplylinux.ch/generate.php

After generating your list, you have to add them into your repository, you can learn how to do that by looking to here. https://help.ubuntu.com/community/Repositories/CommandLine

The list of packages are;

deb http://sg.archive.ubuntu.com/ubuntu/ saucy main restricted universe multiverse 

deb-src http://sg.archive.ubuntu.com/ubuntu/ saucy main restricted universe multiverse 

deb http://sg.archive.ubuntu.com/ubuntu/ saucy-security main restricted universe multiverse 

deb http://sg.archive.ubuntu.com/ubuntu/ saucy-updates main restricted universe multiverse 

deb http://sg.archive.ubuntu.com/ubuntu/ saucy-proposed main restricted universe multiverse 

deb http://sg.archive.ubuntu.com/ubuntu/ saucy-backports main restricted universe    multiverse 

deb-src http://sg.archive.ubuntu.com/ubuntu/ saucy-security main restricted universe multiverse 

deb-src http://sg.archive.ubuntu.com/ubuntu/ saucy-updates main restricted universe multiverse 

deb-src http://sg.archive.ubuntu.com/ubuntu/ saucy-proposed main restricted universe multiverse 

deb-src http://sg.archive.ubuntu.com/ubuntu/ saucy-backports main restricted universe multiverse 

Then you should search clang-format with below command first

sudo apt-cache search clang-format

Then, you can install which version you want to install such as;

sudo apt-get install clang-format-3.3

Joe answered 25/12, 2013 at 8:46 Comment(4)
sudo is not needed for apt-cache searchOrgell
sudo apt-get install clang-format-3.4 worked in Ubuntu 14.04 LTS. This was after typing: sudo apt-get install clang-3.4Gayden
I used sudo apt install clang-format-3.6 on Ubuntu 14.04Continuant
How do I get the latest version? sudo apt install clang-format installs clang-format version 6, but the documentation pages I can find online are for versions 9 and 11. Why is the Ubuntu version 5 major versions out of date?Lasala
C
12

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

Related

  1. See also my clang-format-based project here: eRCaGuy_CodeFormatter
Continuant answered 4/7, 2019 at 0:39 Comment(0)
V
5

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

Veteran answered 5/6, 2020 at 9:36 Comment(3)
I get "ModuleNotFoundError: No module named 'skbuild'" ? So, you need this too: "pip3 install scikit-build", and not in the same step.Binion
This is a great answer, especially in combination with the fact that the PyPi package also offers a pre-commit hook and allows selecting specific clang-format versions: pypi.org/project/clang-formatDumbfound
The state of software distribution on Linux... where pip is the best way to install clang-format 🤦 I've also found it is the best way to install the latest CMake if you need it.Responsible
P
2

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

Postbellum answered 27/8, 2020 at 8:26 Comment(0)
W
2

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.

Whomsoever answered 5/8, 2022 at 11:38 Comment(0)
S
1

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

Shrier answered 6/11, 2021 at 9:5 Comment(0)
A
0

I know the question is about another Linux distribution. But just in case, for openSUSE, to install clang-format, you have to run this:

zypper in clang-tools
Anikaanil answered 14/5, 2023 at 11:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.