226

I am trying to use Homebrew as much as possible. What's the recommended way to install Node.js, nvm and npm on MacOS X?

Aryan Beezadhur
  • 4,503
  • 4
  • 21
  • 42
ohho
  • 50,879
  • 75
  • 256
  • 383
  • 15
    It should be noted that although you asked to use homebrew. Using homebrew to install nvm is not recommended From https://github.com/creationix/nvm/ "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." – Eric Aug 25 '16 at 01:53
  • 1
    2022 Solution - use `nvm` not `brew`. But, use `brew` to install `nvm`. – Janac Meena Feb 28 '22 at 15:15

10 Answers10

298
  1. Using homebrew install nvm:

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

    Add the last command to the .profile, .bashrc or .zshrc file to not run it again on every terminal start. So for example to add it to the .profile run:

    echo "source $(brew --prefix nvm)/nvm.sh" >> ~/.profile
    

    If you have trouble with installing nvm using brew you can install it manually (see here)

  2. Using nvm install node or iojs (you can install any version you want):

    nvm install 0.10
    # or
    nvm install iojs-1.2.0
    
  3. npm is shipping with node (or iojs), so it will be available after installing node (or iojs). You may want to upgrade it to the latest version:

    $ npm install -g npm@latest
    

    UPD Previous version was npm update -g npm. Thanks to @Metallica for pointing to the correct way (look at the comment bellow).

  4. Using npm install ionic:

    npm install -g ionic
    
  5. What about ngCordova: you can install it using npm or bower. I don't know what variant is more fit for you, it depends on the package manager you want to use for the client side. So I'll describe them both:

    1. Using npm: Go to your project folder and install ng-cordova in it:

      npm install --save ng-cordova
      
    2. Using bower: Install bower:

       npm install -g bower
      

      And then go to your project folder and install ngCordova in it:

       bower install --save ngCordova
      

PS

  1. Some commands may require superuser privilege
  2. Short variant of npm install some_module is npm i some_module
alexpods
  • 47,475
  • 10
  • 100
  • 94
  • 11
    If you update npm itself, **do NOT use the npm update command.** The upstream-recommended way to update npm is: `npm install -g npm@latest` (from `brew info node`, and [here](http://stackoverflow.com/questions/11284634/upgrade-nodejs-to-the-latest-version-on-mac-os) ) – Javad Mar 22 '15 at 23:01
  • I followed these steps on a new mac, but each time I restart the terminal, `node -v` returns `node: command not found`. So I do again `nvm install node` and it says `v0.12.2 is already installed. Now using node v0.12.2 (npm v2.7.4)`. And then `node -v` returns `v0.12.2`. Same thing with iojs. What's happening here? thanks – François Romain May 09 '15 at 05:12
  • I understand this is nvm default behavior: github.com/creationix/nvm/issues/714#issuecomment-89720100. To set a default version when a terminal window starts do: `nvm alias default node` or `nvm alias default iojs` – François Romain May 09 '15 at 06:09
  • 1
    I did all this, but for me the npm command is only available after I do: `nvm install iojs`. If I restart the terminal, it is not available anymore. And I get: `env: node: No such file or directory` – Kasper May 30 '15 at 13:43
  • 1
    Just want to mention the the ```brew install nvm``` info screen post install also indicates that you should add ```export NVM_DIR=~/.nvm``` to the shell initialization script. The description in the answer above doesn't. – Mark Edington Aug 06 '15 at 18:33
  • 19
    From the current brew nvm install: "Please note that upstream has asked us to make explicit managing nvm via Homebrew is unsupported by them ..." -> So probably the right answer is to not use brew for nvm / node.js / io.js. – s.Daniel Nov 17 '15 at 10:03
  • 7
    I was receiving the error message "nvm is not compatible with the npm config "prefix" option". according to this site (https://github.com/creationix/nvm/issues/855#issue-109279804) , NVM is not compatible with Homebrew. I "brew uninstalled nvm" and installed nvm via the CURL command and now the error message is gone. – YeeHaw1234 Dec 09 '15 at 22:23
  • 5
    I used nvm via brew for a while but got the "upstream... explicit... unsupported" message too. NVM docs explicitly say "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." So this probably shouldn't be the accepted answer anymore. I just started using n which works. – whatsthatitspat Sep 30 '16 at 19:58
  • 1
    The latest brew instructions on nvm uses '. "/usr/local/opt/nvm/nvm.sh"' instead of 'source $(brew --prefix nvm)/nvm.sh'. – Alpha Huang Apr 22 '17 at 04:39
  • 4
    Current NVM version (v0.33.2) DOES NOT support homebrew installation: See: https://github.com/creationix/nvm/tree/v0.33.2#installation – Andrea Carraro May 15 '17 at 13:32
  • obviously nvm is overcomplicated, better look at answer with `n` below, which worked just great for me – Daniel Garmoshka Aug 30 '21 at 19:37
109

Use nvm to install Node.js, not Homebrew

In most of the answers, the recommended way to install nvm is to use Homebrew.

Don't do this.

On nvm's Github Readme is clearly says:

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.

Use the following method instead

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

The script clones the nvm repository to ~/.nvm and adds the source line to your profile (~/.bash_profile, ~/.zshrc, ~/.profile, or ~/.bashrc).

And then use nvm to install node. For example to install latest LTS version do:

nvm install 16

Clean and hassle free. It will set this as your default Node.js version as well so you should be all set.

Aryan Beezadhur
  • 4,503
  • 4
  • 21
  • 42
Abhijit Mazumder
  • 8,641
  • 7
  • 36
  • 44
  • 10
    I think you could also `nvm install --lts` to get the latest LTS – skube Jan 19 '19 at 15:00
  • 2
    Thank you for bringing this up, I went down a horrible rabbit hole with nvm and homebrew and just emerged... – cssidy Jan 25 '19 at 19:55
  • 1
    Good answer! After a lot of reading (node installs can be a minefield), I just brew uninstalled node, cleaned everything out and used nvm to install node on Mojave 10.14.4: https://stackoverflow.com/q/55735701/123033 – Dave Everitt Apr 18 '19 at 12:32
  • what's the bes way to install "nvm" ? – vikramvi Mar 13 '21 at 11:05
  • 2
    The title is misleading. Yes, we should not use brew to install nvm. But that doesn't mean we should use nvm over brew to install node. Some people prefer brew over nvm if they only care about the latest version of each LTS version. – M Imam Pratama Mar 22 '22 at 01:13
14

I'm using n (Node version management)

You can install it in two ways

brew install n

or

npm install -g n

You can switch between different version of node and io. Here's an example from my current env when I call n without params:

$ n

  io/3.3.1
  node/0.12.7
  node/4.0.0
  node/5.0.0
ο node/5.10.1 
Sergiy Seletskyy
  • 16,236
  • 7
  • 69
  • 80
  • 2
    You can add a third way to install which is painless and leaves a handy uninstall utility if you need it - https://github.com/mklement0/n-install This method is suggested in the n docs at https://github.com/tj/n – whatsthatitspat Sep 30 '16 at 20:00
  • 1
    It should also be mentioned that if you're planning to run different node versions for different projects simultaneously (like in 2 different terminal windows) nvm is a better choice, because it switches just the paths to node in each session. – Nilloc Dec 15 '16 at 20:57
13

I'm super late to this but I didn't like the other answers

Installing Homebrew

For brew run

"$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Installing node & npm

You SHOULD NOT use brew to install node and npm.

I've seen a few places suggested that you should use Homebrew to install Node (like alexpods answer and in this Team Treehouse blog Post) but installing this way you're more prone to run into issues as npm and brew are both package managers and you should have a package manager manage another package manager this leads to problems, like this bug offical npm issues Error: Refusing to delete: /usr/local/bin/npm or this Can't uninstall npm module on OSX

You can read more on the topic in DanHerbert's post Fixing npm On Mac OS X for Homebrew Users, where he goes on to say

Also, using the Homebrew installation of npm will require you to use sudo when installing global packages. Since one of the core ideas behind Homebrew is that apps can be installed without giving them root access, this is a bad idea.

For Everything else

I'd use npm; but you really should just follow the install instruction for each modules following the directions on there website as they will be more aware of any issue or bug they have than anyone else

Community
  • 1
  • 1
garrettmac
  • 8,417
  • 3
  • 41
  • 60
7

If you have previously installed node using brew, then you will have a bunch of extra files that you should clean up before installing node "the right way". Plus, I had to add a few settings to my startup script to make things work smoothly.

I wrote a script to make this easy.

# filename:  install-nvm-npm-node
# author:    Lex Sheehan
# purpose:   To cleanly install NVM, NODE and NPM
# dependencies:  brew

NOW=$(date +%x\ %H:%M:%S)
CR=$'\n'
REV=$(tput rev)
OFF=$(tput sgr0)
BACKUP_DIR=$HOME/backups/nvm-npm-bower-caches/$NOW
MY_NAME=$(basename $0)
NODE_VER_TO_INSTALL=$1
if [ "$NODE_VER_TO_INSTALL" == "" ]; then
    NODE_VER_TO_INSTALL=v0.12.2
fi
if [ "`echo "$NODE_VER_TO_INSTALL" | cut -c1-1`" != "v" ]; then
    echo """$CR""Usage:   $ $MY_NAME <NODE_VERSION_TO_INSALL>"
    echo "Example: $ $MY_NAME v0.12.1"
    echo "Example: $ $MY_NAME $CR"
    exit 1
fi
echo """$CR""First, run:  $ brew update"
echo "Likely, you'll need to do what it suggests."
echo "Likely, you'll need to run: $ brew update$CR"
echo "To install latest node version, run the following command to get the latest version:  $ nvm ls-remote"
echo "... and pass the version number you want as the only param to $MY_NAME. $CR"
echo "Are you ready to install the latest version of nvm and npm and node version $NODE_VER_TO_INSTALL ?$CR"
echo "Press CTL+C to exit --or-- Enter to continue..."
read x

echo """$REV""Uninstalling nvm...$CR$OFF"
# Making backups, but in all likelyhood you'll just reinstall them (and won't need these backups)
if [ ! -d "$BACKUP_DIR" ]; then 
    echo "Creating directory to store $HOME/.nvm .npm and .bower cache backups: $BACKUP_DIR"
    mkdir -p $BACKUP_DIR
fi 
set -x
mv $HOME/.nvm   $BACKUP_DIR  2>/dev/null
mv $HOME/.npm   $BACKUP_DIR  2>/dev/null
mv $HOME/.bower $BACKUP_DIR  2>/dev/null
{ set +x; } &>/dev/null

echo "$REV""$CR""Uninstalling node...$CR$OFF"
echo "Enter your password to remove user some node-related /usr/local directories"
set -x
sudo rm -rf /usr/local/lib/node_modules
rm -rf /usr/local/lib/node
rm -rf /usr/local/include/node
rm -rf /usr/local/include/node_modules
rm /usr/local/bin/npm
rm /usr/local/lib/dtrace/node.d
rm -rf $HOME/.node
rm -rf $HOME/.node-gyp
rm /opt/local/bin/node
rm /opt/local/include/node
rm -rf /opt/local/lib/node_modules
rm -rf /usr/local/Cellar/nvm
brew uninstall node 2>/dev/null
{ set +x; } &>/dev/null

echo "$REV""$CR""Installing nvm...$CR$OFF"

echo "++brew install nvm"
brew install nvm 
echo '$(brew --prefix nvm)/nvm.sh'
source $(brew --prefix nvm)/nvm.sh

echo "$REV""$CR""Insert the following line in your startup script (ex: $HOME/.bashrc):$CR$OFF"
echo "export NVM_DIR=\"\$(brew --prefix nvm)\"; [ -s \"\$NVM_DIR/nvm.sh\" ] && . \"\$NVM_DIR/nvm.sh\"$CR"
NVM_DIR="$(brew --prefix nvm)"

echo """$CR""Using nvm install node...$CR"
echo "++ nvm install $NODE_VER_TO_INSTALL"
nvm install $NODE_VER_TO_INSTALL
NODE_BINARY_PATH="`find /usr/local/Cellar/nvm -name node -type d|head -n 1`/$NODE_VER_TO_INSTALL/bin"
echo "$REV""$CR""Insert the following line in your startup script (ex: $HOME/.bashrc) and then restart your shell:$CR$OFF"
echo "export PATH=\$PATH:$NODE_BINARY_PATH:$HOME/.node/bin"

echo """$CR""Upgrading npm...$CR"
echo '++ install -g npm@latest'
npm install -g npm@latest
{ set +x; } &>/dev/null
echo "$REV""$CR""Insert following line in your $HOME/.npmrc file:$OFF"
echo """$CR""prefix=$HOME/.node$CR"
echo "Now, all is likley well if you can run the following without errors:  npm install -g grunt-cli$CR"
echo "Other recommended global installs: bower, gulp, yo, node-inspector$CR"

I wrote a short article here that details why this is "the right way".

If you need to install iojs, do so using nvm like this:

nvm install iojs-v1.7.1

To install brew, just see its home page.

See alexpods answer for the rest.

l3x
  • 30,760
  • 1
  • 55
  • 36
  • 7
    Awesome to see that all these package managers and version managers really help to remove custom written scripts, hacks, and workarounds to get techstack xyz working. awesome. – Michahell Jun 02 '15 at 13:49
  • Worked for me even though I started with npm and brew both broken – Chris F Carroll Mar 21 '16 at 00:17
5

You should install node.js with nvm, because that way you do not have to provide superuser privileges when installing global packages (you can simply execute "npm install -g packagename" without prepending 'sudo').

Brew is fantastic for other things, however. I tend to be biased towards Bower whenever I have the option to install something with Bower.

Rajan Patel
  • 83
  • 1
  • 4
  • Installing node using nvm is now my preferred approach. For the obvious reason of being able to have multiple versions of node on my machine and also I don't have to execute npm with sudo . I wrote a blog post http://garywoodfine.com/install-nodejs-nvm-osx/ explaining how to install – Gary Woodfine Mar 06 '16 at 07:56
4

Here's what I do:

curl https://raw.githubusercontent.com/creationix/nvm/v0.20.0/install.sh | bash
cd / && . ~/.nvm/nvm.sh && nvm install 0.10.35
. ~/.nvm/nvm.sh && nvm alias default 0.10.35

No Homebrew for this one.

nvm soon will support io.js, but not at time of posting: https://github.com/creationix/nvm/issues/590

Then install everything else, per-project, with a package.json and npm install.

paulmelnikow
  • 16,895
  • 8
  • 63
  • 114
4

I agree with noa -- if you need to have multiple versions of node, io.js then brew is not the appropriate solution.

You can help beta-test io.js support in nvm: https://github.com/creationix/nvm/pull/616

If you just want io.js and are not switching versions, then you can install the binary distribution of io.js from https://iojs.org/dist/v1.0.2/iojs-v1.0.2-darwin-x64.tar.gz ; that includes npm and you will not need nvm if you are not switching versions.

Remember to update npm after installing: sudo npm install -g npm@latest

Sam Mikes
  • 10,438
  • 3
  • 42
  • 47
1

For install with zsh and Homebrew:

brew install nvm  <=== This is not recommended by NVM. They want to run their shell script instead

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

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

export NVM_DIR="$HOME/.nvm"
. "/usr/local/opt/nvm/nvm.sh"

Then install a node version and use it.

nvm install 7.10.1
nvm use 7.10.1
David Dehghan
  • 22,159
  • 10
  • 107
  • 95
  • from this answer : https://stackoverflow.com/a/41913355/773322 "You shouldn't have a package manager managing a package manager". Consider deleting your answer. – b_dubb Apr 18 '22 at 15:44
  • interesting, I didn't know that myself. I have been using brew to install NVM for the last 10 years and didn't really have an issue. But if I was going to setup a new machine today I would use their script. – David Dehghan Apr 19 '22 at 23:09
  • 1
    "nvm use" resolved my issue – Harjeevan Tanda Jul 14 '22 at 20:27
0

2021 Update

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

Troubleshooting for MAC:

Since macOS 10.15, the default shell is zsh and nvm will look for .zshrc to update, none is installed by default. Create one with touch ~/.zshrc and run the install script again.

If you use bash, the previous default shell, run touch ~/.bash_profile to create the necessary profile file if it does not exist.

You might need to restart your terminal instance or run . ~/.nvm/nvm.sh. Restarting your terminal/opening a new tab/window, or running the source command will load the command and the new configuration.

You have previously used bash, but you have zsh installed. You need to manually add these lines to ~/.zshrc and run . ~/.zshrc.

Rajeev Jayaswal
  • 1,423
  • 1
  • 20
  • 22