67

I just upgraded my MacBook Air to Mavericks. The process appears to have removed almost all of the symlinks that homebrew put in /usr/local/bin. As I notice things missing then I relink them via brew unlink <keg> && brew link <keg> (although the symlinks are gone, homebrew still thinks they are there so I need to unlink first). This feels somewhat inefficient.

I could just loop through everything in /usr/local/Cellar but not every keg is automatically linked and I don't want to link those. So my next guess is to list every linked keg and relink them (using the above).

Is there some magic command that will find all the kegs that homebrew thinks are linked so that I can relink them?

1
  • I used Migration Assistant to move "everything" from an old Mac to a new one, and I also lost all my homebrew symlinks. Annoying.
    – Grav
    Mar 30, 2014 at 13:31

3 Answers 3

42

/usr/local/Library/LinkedKegs seems to contain a list of, well, linked kegs, so this should do the trick:

ls -1 /usr/local/Library/LinkedKegs | while read line; do
    echo $line
    brew unlink $line
    brew link --force $line
done

I'd recommend ensuring you have write permissions to /usr/local/lib again before trying this.

4
  • Pretty sure that this just restored my homebrew into working order. Thanks! Jul 15, 2014 at 6:43
  • Amazing, thank you :) just made a gist out of it if you don't mind, I'm pretty sure that we are not the only one who faced this problem.. goo.gl/YTPMjh Jul 3, 2016 at 5:45
  • 1
    No, don't do --force! brew link should be sufficient, and some kegs really shouldn't be linked (as they can conflict with system libraries), whereas --force will force their linkage. Mar 21, 2017 at 20:38
  • Huh. Looks like I also deleted /usr/local/Library. May 31, 2019 at 21:10
72

This bash command (taken from there) will unlink all installed formulas and then link them again:

brew list -1 | while read line; do brew unlink $line; brew link $line; done

I guess all formulas need to be linked, otherwise, whichever tool depends on them, won't be able to find them..

4
  • I'm not sure that all formulae do need to be linked. Some are keg-only but I don't know which, since some were installed as dependencies of others. Mar 11, 2014 at 21:31
  • 4
    It seems you just get some warnings like "Warning: extempore is keg-only and must be linked with --force"
    – Grav
    Mar 30, 2014 at 13:29
  • 4
    Here's an equivalent version using xargs: brew list -1 | xargs -I formula sh -c "brew unlink formula && brew link formula" Apr 19, 2014 at 23:54
  • great to use. I am using 10.12.6 mac os
    – Larry Lo
    Aug 20, 2017 at 15:34
9

In addition to the other answers to this question, you may also need to relink formula with multiple versions installed (brew link won't do this for you).

First, relink all the formulas you can:

brew list -1 | while read line; do brew unlink $line; brew link --force $line; done

Then run brew doctor, which should complain and give you two lists:

  • Unlinked formulae with multiple versions
  • Keg-only formulae which have been incorrectly link by the original relinking process.

For each formula with multiple versions run the following, replacing "python" with the formula name:

brew info python  

This will show you, among other information, all installed versions. Choose which version you want to link (for me its 2.7.6), and use brew switch

brew switch python 2.7.6

You will also want to run brew unlink on the list (if any) of keg-only formulae that have been linked.

0

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .