88

So I made the mistake of trying to get rid of all sudo dependencies. I downloaded npm as a package from the site and did a manual/global install. But it seemed as if i was always having to run with sudo...so I tried to uninstall and run with homebrew.

Now I can't get node or npm to even run...I guess I have to link with brew link them but i'm getting this error:

Could not symlink share/doc/node/gdbinit
Target /usr/local/share/doc/node/gdbinit
already exists. You may want to remove it:
  rm '/usr/local/share/doc/node/gdbinit'

I've tried removing that: And i've gotten permission denied.

I have tried running brew prune. I have tried to uninstall then reinstall using these steps:

$ brew uninstall npm
$ brew uninstall node
$ npm uninstall npm -g
$ sudo rm -rf /usr/local/lib/node_module

Error: The 'brew link' step did not complete successfully

I am running Yosemite 10.10.5. I have git version 2.6.0 installed. My homebrew is updated. A brew doctor gives me this warning:

Warning: You have unlinked kegs in your Cellar
Leaving kegs unlinked can lead to build-trouble and cause brews that depend on
those kegs to fail to run properly once built. Run `brew link` on these:
    node

Not sure where to go from here. I'm trying to lose my reliance on CodeKit and get gulp up and running.

Community
  • 1
  • 1
pwhitt
  • 1,033
  • 1
  • 10
  • 9
  • 1
    `I've tried removing that: And i've gotten permission denied.` - That's because you installed as root in the first place. Now you have to remove using `sudo rm '/usr/local/share/doc/node/gdbinit'`. Note: If you use homebrew, do not install anything with `sudo install` anymore into /usr/local. This will create a huge mess. – cel Sep 30 '15 at 06:03
  • Now I get 'could not symlink share/system/systemtap/tapset/node.stp /usr/local/share/system/tapset is not writable. I have tried doing a sudo chown -R 'username'/usr/local/share/system/tapset but that didn't work. – pwhitt Sep 30 '15 at 09:01
  • I would simply delete all colliding files. It's a bit quirky, but that's why we have awesome package managers like hombrew. Manually uninstalling is simply way too painful. – cel Sep 30 '15 at 09:11
  • As you can tell, I'm new using the command line for installation removal of files...do I have to go one by one trying to delete conflicting files? Homebrew does have a command that says it would overwrite the node files but it doesn't seem to do the trick. – pwhitt Sep 30 '15 at 19:34

3 Answers3

252

It looks like several files and directories in /usr/local are now owned by root, since you ran a couple of steps using sudo. To get rid of these, take back ownership of all of the files and directories under /usr/local:

sudo chown -R $(whoami) $(brew --prefix)/*

Once that is done, run brew doctor again.

Similar questions can be found here:

Connor
  • 4,216
  • 2
  • 29
  • 40
nwinkler
  • 52,665
  • 21
  • 154
  • 168
  • Is it ok to chown the /usr/local directory if there is more than one user? – Omtara Oct 18 '16 at 05:38
  • Good question. The most recent version of Homebrew doesn't require the `chown` anymore, from what I understand. If you still have to `chown`, make sure that all users are included in the group that owns the directory, and that the group has write permissions as well. – nwinkler Oct 18 '16 at 06:04
  • I know this is old but doing `sudo chown -R $USER /usr/local` made some things I have installed unable to be found anymore. How can I solve this? – Lucas Nogueira Jan 10 '18 at 13:55
  • 24
    In Mac High Sierra you can't `chown` and now have to use `sudo chown -R $(whoami) $(brew --prefix)/*`. – Liz Dec 21 '18 at 22:27
  • 6
    `sudo chown -R $(whoami) $(brew --prefix)/*` then `brew link --overwrite node` worked for me. – Dami Oct 09 '20 at 10:14
  • Good explanation. I prefer to be more conservative and only `chown -R` the specific dirs that cause issues instead of everything under `/usr/local/*`. Example, `chown -R myuser:admin /usr/local/share/doc/node` – wisbucky Feb 18 '22 at 04:29
  • this is incredibly dangerous if you have a multi-user system. – Akin Williams Jul 31 '23 at 14:08
24

In my case, I was continue to execute command brew link node and upon every execution, it is keep on saying to remove some files. I just followed the instructions and keep on removing them with sudo. At last, after 5 such removals, I have the linking done.

enter image description here

Rohit Mandiwal
  • 10,258
  • 5
  • 70
  • 83
  • 1
    This approach worked for me, except instead of 5 files it was 434 files. Fortunately it suggested I could list them by running `brew link --overwrite --dry-run node` – prototype Mar 01 '21 at 02:51
  • 2
    I had to delete files via `brew link --overwrite --dry-run node` and then run `sudo chown -R $(whoami) $(brew --prefix)/*` as posted by @nwinkler – osuwireless Apr 13 '21 at 20:21
11

Had something similar happen with node listed as an unlinked keg. This is what worked for me on MacOS Big Sur:

  1. sudo mkdir -p /usr/local/sbin // had issues with sbin, ignore if you don't.
  2. sudo chown -R $(whoami) /usr/local/sbin // ignore if sbin isn't an issue.
  3. brew link --overwrite node
  4. brew cleanup // wanted to be sure this alone ran fine :)
  5. brew doctor // found un brewed header files, but not concerned about those.
  6. node -v // checking node version
  7. npm -v // checking npm version

If the above doesn't work, try starting from scratch and seeing if these steps help:

  1. brew uninstall node
  2. brew update
  3. brew upgrade
  4. brew cleanup
  5. brew install node
  6. sudo chown -R $(whoami) /usr/local
  7. brew link --overwrite node
  8. brew postinstall node

You can definitely chain these commands and make the input way shorter, but small victories help ease some of the frustration, while also making it easier to ID exactly what step failed, rather than displaying a chained-command error and having the person rage (╯°□°)╯︵ ┻━┻ because they are now even more lost lol.

Jesse
  • 439
  • 4
  • 6
  • 1
    `sudo chown -R $(whoami) /usr/local` may raise `chown: /usr/local: Operation not permitted`, so you might need to `sudo chown -R $(whoami) $(brew --prefix)/*` instead. – J.Z. May 12 '22 at 23:13