185

I'm using homebrew and oh-my-zsh on a fresh OSX 10.10.1 install. I got nvm via homebrew and then attempted to run it but says - zsh: command not found: nvm

Any idea what the problem is? I was able to install and use git just fine...

enter image description here

enter image description here


UPDATED 9/20/2019

As stated by more recent answers from DarkPurple141 and Elise van Looij. nvm doesn't appear to be compatible with homebrew. This is also stated in the official nvm-sh repo located here.

Homebrew installation is not supported. If you have issues with homebrew-installed nvm, please brew uninstall it, and install it using the instructions below, before filing an issue.

Note: If you're using zsh you can easily install nvm as a zsh plugin. Install zsh-nvm and run nvm upgrade to upgrade.

The following steps should help:

  1. $ brew uninstall nvm

  2. $ brew cleanup (just for good measure)

  3. $ curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.34.0/install.sh | bash

  4. Check that nvm was installed correctly by running $ command -v nvm.

    4.a If the response is anything other than $ nvm, add the following two lines to the ~/.bash-profile file: export NVM_DIR=~/.nvm source ~/.nvm/nvm.sh

    4.b Restart your computer (you can try closing Terminal and restarting it first)

    4.c $ command -v nvm should now return nvm`

  5. Now update Node.js with $ nvm install --lts

  6. Update npm: $ nvm install-latest-npm

  7. $ npm install --global mocha. Finally, success! Well, for me and I hope for you too.

7
  • 3
    Does it persist if you restart Terminal? Commented Dec 26, 2014 at 0:56
  • Try typing rehash or opening a new shell. Sometimes zsh doesn't pick up additions.
    – Doon
    Commented Dec 26, 2014 at 0:57
  • I've closed the shell, rebooted system and nothing. Did rehash and no luck.
    – Dayan
    Commented Dec 26, 2014 at 0:59
  • How about brew link nvm does that do anything ?
    – Doon
    Commented Dec 26, 2014 at 1:04
  • Warning: Already linked: /usr/local/Cellar/nvm/0.20.0 To relink: brew unlink nvm && brew link nvm
    – Dayan
    Commented Dec 26, 2014 at 1:59

11 Answers 11

196

Did you follow the instructions listed in the caveats?

[~] brew info nvm
nvm: stable 0.20.0, HEAD
https://github.com/creationix/nvm
Not installed
From: https://github.com/Homebrew/homebrew/blob/master/Library/Formula/nvm.rb
==> Caveats
Add the following to $HOME/.bashrc, $HOME/.zshrc, or your shell's
equivalent configuration file:

  source $(brew --prefix nvm)/nvm.sh

Node installs will be lost upon upgrading nvm. Add the following above
the source line to move install location and prevent this:

  export NVM_DIR=~/.nvm

Without the extra config it doesn't look like it will find NVM by default..

7
  • 5
    Thank you for the heads up Doon, i was not aware of brew info, still new to homebrew :) Its working good now after following those steps.
    – Dayan
    Commented Dec 26, 2014 at 14:05
  • They should be shown after the install completes as well. But if you are not looking for them they can be missed.
    – Doon
    Commented Dec 26, 2014 at 14:53
  • 5
    I had to add this to by ~/.bash_profile to get it working :/
    – Janaka
    Commented May 21, 2016 at 21:47
  • 1
    hmm. I don't use bash(prefer zsh), but it looks like OSX terminal.app (I don't use terminal either, prefer Iterm2), opens each shell as a login shell so yes .bash_profile would be correct in that case.. also the info has changed. so you should refer to the latest version..
    – Doon
    Commented May 22, 2016 at 0:37
  • 1
    Thanks! I was missing this line source $(brew --prefix nvm)/nvm.sh. Added it to my .zshrh Commented Oct 17, 2017 at 18:25
72

There are TWO things you need to do. Follow the caveats shown after installing nvm via brew, and THEN you need to activate/reload the .bash_profile/.zshrc changes.

  1. Run brew install nvm
  2. Follow caveats shown in console, mine were as follows, yours MAY be different!:

Add the following to ~/.bash_profile, ~/.zshrc or your desired shell configuration file:

export NVM_DIR="$HOME/.nvm"
. "$(brew --prefix nvm)/nvm.sh"
  1. Run . ~/.bash_profile to apply the changes you made to your .bash_profile file (Adjust as required for .zshrc and similar)
3
  • 3
    This is useful for Bash users, but the OP is on zsh. I guess the same applies to .zshrc with the necessary modifications.
    – tripleee
    Commented Dec 11, 2017 at 6:28
  • Thanks. (if you're on zsh, just type command, bash and then apply the steps) Commented Apr 11, 2023 at 7:36
  • Years later I see the above comment made, and notice I am since then now also using zsh instead of bash lol. Also adjusted my answer to also include zsh, as per other comment.
    – redfox05
    Commented Feb 9 at 14:49
24

I've spent a couple of hours going round and round on this issue, but I've come to the conclusion that DarkPurple141 is right: nvm just isn't compatible with Homebrew, as they state on their Github nvm-sh/nvm Node Version Manager. Homebrew will install nvm and everything looks fine, until one tries to get npm to install a module, Mocha in my case. That threw me right back to the dreaded error:

ERR! Error: EACCES: permission denied, access '/usr/local/lib/node_modules'

NOTE: On step 3, make sure to replace version with correct release.

The solution, on MacOS 10.14 Mojave, was:

  1. $ brew uninstall nvm
  2. $ brew cleanup (just for good measure)
  3. $ curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.0/install.sh | bash
  4. Check that nvm was installed correctly by running $ command -v nvm.

    4.a If the response is anything other than $ nvm, add the following two lines to the ~/.bash-profile file: export NVM_DIR=~/.nvm source ~/.nvm/nvm.sh 4.b Restart your computer (you can try closing Terminal and restarting it first) 4.c $ command -v nvm should now returnnvm`

  5. Now update Node.js with $ nvm install --lts

  6. Update npm: $ nvm install-latest-npm
  7. $ npm install --global mocha. Finally, success! Well, for me and I hope for you too.
3
  • For the people who are using zshrc instead of bash use the following command in the step number 4.a: nano ~/.zshrc Now you add the two commands, restart the terminal and you're good to go :) Commented Apr 27, 2022 at 14:10
  • Just to add, at step 4 one need to add complete info to .bash_profile returned from step 3. Mine was like : export NVM_DIR="$HOME/.nvm" [ -s "/opt/homebrew/opt/nvm/nvm.sh" ] && \. "/opt/homebrew/opt/nvm/nvm.sh" # This loads nvm [ -s "/opt/homebrew/opt/nvm/etc/bash_completion.d/nvm" ] && \. "/opt/homebrew/opt/nvm/etc/bash_completion.d/nvm" # This loads nvm bash_completion and then run . ~/.bash_profile to keep the terminal ready to use it. Commented May 10, 2022 at 4:15
  • step 4 was extremely helpful, thanks!
    – lolu
    Commented May 30, 2023 at 10:16
11

To install nvm, brew install nvm

To run nvm after installation or just once, $(brew --prefix nvm)/nvm.sh

To run nvm everytime zsh opens

  1. nano ~/.zshrc
  2. Add this line source $(brew --prefix nvm)/nvm.sh
9

While the accepted answer does technically work, it's worth noting that Homebrew installation is not officially supported by the nvm package. The recommended way to avoid issues like those raised above is to apply either of the below methods of installation.

curl -o- https://raw.githubusercontent.com/creationix/nvm/v0.34.0/install.sh | bash

# or wget:
wget -qO- https://raw.githubusercontent.com/creationix/nvm/v0.34.0/install.sh | bash

source: https://github.com/creationix/nvm

6

I ran into this, like many here, but all the suggested options only worked until I restarted the terminal. Here's an option that fixed it

Create NVM’s working directory.

mkdir ~/.nvm

Edit your shell config file to add the path to the NVM home directory. The shell config file would be ~/.bash_profile if you are using Bash, or ~./zshrc if you are using Zsh, the default shell for MacOS Catalina and later versions.

# open config file
nano ~/.zshrc

Add the following to the config file.

# add the nvm path
export NVM_DIR="$HOME/.nvm"
# load the nvm script every time you open the terminal
source $(brew --prefix nvm)/nvm.sh

Close your nano editor and verify the path to the home directory with the commands below.

source ~/.zshrc                   # reload config file
echo $NVM_DIR                     # echo path
nvm --version                     # check nvm version
2
  • I think installing oh-my-zsh seems to have overwritten this even when I had done it already. Thank you for writing this out so I could restore my .zshrc :) Commented Feb 2 at 11:06
  • You save my day! I followed your steps and I could disappear the error on my terminal, and yes the problem was the missing path of the nvm. Thanks Commented May 28 at 14:04
2

The reason you would need to reload your bash profiles or any other bash files might be because the command nvm may not be a program but a function that is defined and can only be used if the corresponding bash file is sourced.

On a system that I checked

which nvm 

does not work but

nvm list

does. This means that you can use the word "nvm" to invoke something. That something isn't a program. In the current case, it is a function which can be verified by

typeset -F | grep -P ' nvm$'

which outputs

declare -F nvm

which means nvm is a function, whose body can be inspected by doing

type -F nvm
3
  • 1
    Nice.. But, how is this an answer?
    – Dayan
    Commented Jan 25, 2017 at 13:00
  • 1
    Thanks for the update Dayan - edited the answer - the reason why I couldn't find it because I was relying on it being a program but it was defined as a function. Wanted to highlight that. Commented Jan 26, 2017 at 12:06
  • To check whether nvm has been installed properly use 'command -v nvm' in the Terminal. The response should be 'nvm'. If it's not, follow the instructions on github.com/nvm-sh/nvm#installation-and-update. Commented Aug 19, 2019 at 12:36
1

One possibility if brew was used is that the nvm may be unlinked, especially if it was installed by another MAC OS user.

In this case, execute:

brew link nvm
0
1

I'd like to add that nvm's location on github changed and the version bumped. The curl command now should be:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.35.0/install.sh | bash
0
1

Instructions from lukechilds/zsh-nvm#as-an-oh-my-zsh-custom-plugin

If you are using macos + zsh + oh-my-zsh:

  1. Clone zsh-nvm into your custom plugins repo

    git clone https://github.com/lukechilds/zsh-nvm ~/.oh-my-zsh/custom/plugins/zsh-nvm
    
  2. Then load nvm as a plugin in your .zshrc file

    plugins+=(zsh-nvm)
    

    Keep in mind that plugins need to be added before oh-my-zsh.sh is sourced.

    f.e.

    $ nano ~/.zshrc
    
    // edit .zshrc contents
    ...
    # Which plugins would you like to load?
    # Standard plugins can be found in $ZSH/plugins/
    # Custom plugins may be added to $ZSH_CUSTOM/plugins/
    # Example format: plugins=(rails git textmate ruby lighthouse)
    # Add wisely, as too many plugins slow down shell startup.
    # docs: f.e. https://github.com/ohmyzsh/ohmyzsh/tree/master/plugins/kubectl
    
    plugins=(zsh-nvm)
    
    source $ZSH/oh-my-zsh.sh
    ...
    
    
  3. Finally reload the configuration $ source ~/.zshrc

1
  • 1
    This was exactly what I had been looking for it worked so well. Thank you.
    – wittmason
    Commented Jun 2, 2023 at 19:41
0

if you happen to be using fish shell, consider https://github.com/jorgebucaran/nvm.fish

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.