Setup Node Version Manager (NVM) on macOS M1 Silicon

Eric Bishard - Nov 14 '21 - - Dev Community

As a web developer these days, we typically need to be able to switch versions of Node on the fly, for this we want to install Node Version Manager on a clean install of our machine, we don't want to start by installing Node on its own as this will give us a single version of Node (whichever we decide to install)

If you install Node first and then try to install NVM, things can get complicated, so if you have already installed Node, my suggestion is to completely remove it before installing NVM.

As well, NVM is explicitly not supported when installed via homebrew - the only correct way to install it is with the install script in NVM's Readme.

So if you have a Mac M1, these are the steps I would encourage you to try.

Navigate to your home directory

cd ~
Enter fullscreen mode Exit fullscreen mode

Create a .zshrc file (if it doesn't exist)

touch .zshrc
Enter fullscreen mode Exit fullscreen mode

Before proceeding to the next step I needed to manually install Rosetta 2 in order run apps not built for Apple silicon.

softwareupdate --install-rosetta
Enter fullscreen mode Exit fullscreen mode

Install NVM using curl (found on the NVM Readme)

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
Enter fullscreen mode Exit fullscreen mode

This last command will update your .zshrc file to look like the following:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"  # This loads nvm
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"  # This loads nvm bash_completion
Enter fullscreen mode Exit fullscreen mode

Install Node using NVM

nvm install node
Enter fullscreen mode Exit fullscreen mode

This will install the latest version of Node (v17.x at time of writing).

NOTE: Node versions before v15.x are not necessarily ARM compatible, but it seems that Node has addressed this issue, so if you do install a version prior to v15.x hopefully, you will not need to use Rosetta to run.

Install LTS version of Node

nvm install --lts
Enter fullscreen mode Exit fullscreen mode

Running this command installed the current Node LTS which at the time of this writing is v16.x. I think I will try to stick with this version or better when developing, however; that's the beauty of NVM is that if I need and older version it's easy to switch!

List the versions of Node I have installed

nvm ls
Enter fullscreen mode Exit fullscreen mode

Select an alternate version that I have installed

nvm use 16
Enter fullscreen mode Exit fullscreen mode

or

nvm use --lts
Enter fullscreen mode Exit fullscreen mode

Finally, to clear the nvm cache to reduce clutter, run:

nvm cache clear
Enter fullscreen mode Exit fullscreen mode

Setting Default

nvm alias default v10.19.0
Enter fullscreen mode Exit fullscreen mode

But it will give the following error

! WARNING: Version 'v10.19.0' does not exist. default -> 
v10.19.0 (-> N/A)
Enter fullscreen mode Exit fullscreen mode

In that case, you need to run two commands in the following order

Install the version that you would like

nvm install 10.19.0
Enter fullscreen mode Exit fullscreen mode

Set 10.19.0 (or another version) as the default

nvm alias default 10.19.0
Enter fullscreen mode Exit fullscreen mode

Use a Specific Node Version

nvm use 12
Now using node v12.22.3 (npm v6.14.13)
Enter fullscreen mode Exit fullscreen mode

Now we are using that latest version of node. As time goes on, I could keep periodically running:

nvm install 12
Enter fullscreen mode Exit fullscreen mode

And get any updates to the latest of that version, or set a more specific earlier version, and then I would have two different versions of 12 I could go back and forth if needed during development.

Uninstall a Node Version

nvm uninstall 12
nvm: Cannot uninstall currently-active node version, 
v12.22.3 (inferred from 12).
Enter fullscreen mode Exit fullscreen mode

Notice the comment about it inferring because we were not specific. But, we are on the node we are trying to delete, we need to get off of this version to delete it:

nvm use 16 && nvm uninstall 12
Now using node v16.4.2 (npm v7.18.1)
Uninstalled node v12.22.3
Enter fullscreen mode Exit fullscreen mode

This is not a topic widely talked about in one easy to find resource and the best sources I have found on the subject came from Code Fallacy on YouTube and Michael Baldwin who both have good information on this topic, so much thanks to them where all credit is due...

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .