How to install clangd on Mac (preferably with homebrew)

Viewed 1821

Once again a "How to install clangd on Mac" question. What I have done so far is brew install llvm, so older answers (and the clangd website) say that clangd should already be installed. But in the current version this seems to not be the case. clangd is not in my path and none of the folders I looked into have that binary.

I found these answers (none of them work):

So my questions are:

  1. As of August 2021, how do I install clangd on a macbook?
  2. Do I really have to manually build everything from sources?

Further information:

  • This is a M1 Macbook

  • I found a clangd executable at /opt//homebrew/Cellar/llvm/12.0.1/bin/clangd

  • Running brew info llvm results in a message containing

    If you need to have llvm first in your PATH, run:
    echo 'export PATH="/opt/homebrew/opt/llvm/bin:$PATH"' >> ~/.zshrc

    In that folder I found a clangd executable too, so I guess I should add this to my path instead of /usr/local/opt/llvm/bin?

1 Answers

Update: seems the questioner is using a M1 Mac. /opt/homebrew/opt/llvm/bin should be the dir to be added into PATH but not the /usr/local one.


llvm is keg-only, which means it was not symlinked into /usr/local (/opt/homebrew for M1 Mac). llvm bring its own libraries. To avoid these libraries shadowing the system one, Homebrew choose NOT to link the package, which results in binaries not linked into /usr/local/bin.

clangd is in /usr/local/opt/llvm/bin, you need to add this directory into PATH. Save following content into you shell init files (.zshrc for zsh, .bash_profile for bash)

# for x86 Mac
export PATH="/usr/local/opt/llvm/bin:$PATH"

# for M1 Mac
export PATH="/opt/homebrew/opt/llvm/bin:$PATH"

Then start and new shell, type clangd --version to have a test.

Check brew info llvm for more info.

Related