5

I have used 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 MacBook (M1)

  • 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?

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131

1 Answers1

6

It seems the questioner is using an M1 Mac. /opt/homebrew/opt/llvm/bin should be the directory to be added into PATH, but not the /usr/local one.


llvm is keg-only, which means it was not symbolic linked 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 the following content into your shell init files (.zshrc for Z shell (zsh), and .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 a new shell, and type clangd --version to have a test.

Check brew info llvm for more information.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Simba
  • 23,537
  • 7
  • 64
  • 76
  • As I said, `/usr/local/opt/llvm/bin` does not exists after `brew install llvm`. And no other location inside `/usr/local` contains a `clangd` binary. – Proof-By-Sledgehammer Aug 19 '21 at 09:58
  • @Proof-By-Sledgehammer Are you using a M1 Mac? What's the output of `brew info llvm`, can you paste it in the question? – Simba Aug 19 '21 at 10:00
  • 1
    It looks like `brew info llvm` actually contains the information needed to get clangd to work. I am not sure if it is good practice to add the given directory to my path (although `brew info` tells me to do it). Maybe you can update your answer so that I can accept it? – Proof-By-Sledgehammer Aug 19 '21 at 10:09
  • 2
    I will try to remember to upvote your answer once I have enough reputation – Proof-By-Sledgehammer Aug 19 '21 at 10:21