Node Version Manager (NVM) is a widely-used tool that allows developers to manage multiple versions of Node.js on a single machine. Its importance cannot be overstated, as Node.js applications can sometimes be version-specific, and different projects may require different Node.js versions. The flexibility offered by NVM ensures a smooth development process, allowing developers to switch between Node versions seamlessly. On macOS, there are multiple methods to install NVM, but one of the most popular and efficient ways is using Homebrew, the renowned package manager for macOS.

Advertisement

This guide will walk you through the detailed steps of installing NVM on macOS using Homebrew, allowing you to set up a flexible Node.js development environment with ease.

Prerequisites

  • You must have macOS desktop access with administrator privileges.
  • If you don’t have Homebrew installed on your macOS, you can install it by pasting the following command in the terminal:
    /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)" 
    

    For more instruction visit Homebrew installation tutorial.

Step 1: Remove Existing Node Versions

If your system already has a node installed, uninstall it first. My system already has installed node via Homebrew. So uninstalling it first. Skip if not already installed.

brew uninstall --ignore-dependencies node 
brew uninstall --force node 

Step 2: Install NVM on macOS

  1. Before installing any packages, it’s always a good idea to update Homebrew to the latest version and update the formulae.
    brew update 
    
  2. After that, you can easily install NVM using the following command:
    brew install nvm 
    
  3. Next, create a .nvm directory in the home.
    mkdir ~/.nvm 
    
  4. Now, configure the required environment variables. Edit the following configuration file in your home directory
    vim ~/.bash_profile 
    
  5. Add the below lines to ~/.bash_profile ( or ~/.zshrc for macOS Catalina or newer versions)
    
    export NVM_DIR="$HOME/.nvm"
    [ -s "/usr/local/opt/nvm/nvm.sh" ] && \. "/usr/local/opt/nvm/nvm.sh"
    [ -s "/usr/local/opt/nvm/etc/bash_completion" ] && \. "/usr/local/opt/nvm/etc/bash_completion"
    
    

    Press ESC + :wq to save and close your file.

  6. Next, load the variable to the current shell environment. From the next login, it will automatically loaded.
    source ~/.bash_profile  # or source ~/.zshrc 
    

That’s it. The NVM has been installed on your macOS system. Go to next step to install Node.js versions with the help of nvm.

Step 3: Using NVM

First of all, see what Node versions are available to install. To see available versions, type:

nvm ls-remote 

Now, you can install any version listed in above output. You can also use aliases names like node for latest version, lts for latest LTS version, etc.

nvm install node     ## Installing Latest version 
nvm install 20       ## Installing Node.js 20.X version 

After installing you can verify what is installed with:

nvm ls 

Install NVM macOS

If you have installed multiple versions on your system, you can set any version as the default version any time. To set the node 20 as default version, simply use:

nvm use 20 

Similarly, you can install other versions like Node 12, 16, 18, and 21 versions and switch between them.

Conclusion

Having the capability to manage and switch between various Node.js versions is essential for modern web development, and NVM provides that flexibility. By leveraging Homebrew on macOS, the installation process becomes streamlined, making it easier than ever to get NVM up and running. Once installed, developers can enjoy a hassle-free experience, working on multiple Node.js projects regardless of their version requirements.

This guide has furnished you with all the knowledge needed to install NVM on macOS using Homebrew. Now, you’re ready to tackle any Node.js project, knowing you have the power to easily manage and switch between Node versions at your fingertips.

Share.

46 Comments

  1. command not found: nvm

    after doing the following:
    brew reinstall nvm

    I added:
    export NVM_DIR=”$HOME/.nvm”
    [ -s “/opt/homebrew/opt/nvm/nvm.sh” ] && \. “/opt/homebrew/opt/nvm/nvm.sh”
    [ -s “/opt/homebrew/opt/nvm/etc/bash_completion.d/nvm” ] && \. “/opt/homebrew/opt/nvm/etc/bash_completion.d/nvm”
    [[ -s $HOME/.nvm/nvm.sh ]] && . $HOME/.nvm/nvm.sh

    to the .zshrc file located in home.

    Then nvm was found!

    System: MacOS M3 Sonoma 14

  2. I just want to say you saved my life!
    This is so amazing. thank you for this wonderful exact step by step guide
    God bless you

  3. Pablo Câmara on

    Why do you “export NVM_DIR=~/.nvm” right before the “source $(brew –prefix nvm)/nvm.sh”
    if inside the “$(brew –prefix nvm)/nvm.sh” there is already this export ?

    I have used only:
    source $(brew –prefix nvm)/nvm.sh

    and it works.

    Thanks for your post!

  4. Guillaume Deschamps on

    Hello,

    i am working on macOS BigSur v11.5.2. I wanted to install nvm in order to work with the 10.x version of sage/root (a wordpress theme builder).

    :: source ~/.bash_profile
    is not working for me.

    i used
    :: source ~/.nvm/nvm.sh
    instead.

    Thank you for the tut 😉

  5. Homebrew installation is not supported according to nvm docs:
    https://github.com/nvm-sh/nvm#installing-and-updating

    This article was a bit of a waste of time to be honest.

    I ended up with command not found: nvm
    This is because there was an empty .nvm folder in my user location.
    To fix this I installed nvm manually using:
    curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash

    So you can skip over the homebrew stuff and just do that instead!

  6. Aykut Kılıç on

    Hello,

    Thanks for the clean explanation..
    I see a file named .zshenv. Is it possible to use this instead of zshrc?
    Regards.

    Aykut.

  7. for beginners like me:

    last part of point 2 should be : source ~/.zshrc
    for any recent OS (Catalina, Big Sur, Monterey) It’s kind of mentioned at the step before that but it took me a minute 🙂

  8. Peter Holdsworth on

    nvm documentation explicitly states that homebrew installation is not supported. I tried it but had problems invoking minor versions of node.

  9. When zshrc is installed, you have to add
    “`export NVM_DIR=~/.nvm
    source $(brew –prefix nvm)/nvm.sh“`
    to `~/.zshrc` file.

Leave A Reply

Exit mobile version