Install Node.js on mac using NVM — For beginners — MacOS Big Sur
Note: This article has been written on MacOS Big Sur operating system. If you encounter “zsh command not found” at any point of time (assuming you are using latest version of MacOS), please type source ~/.nvm/nvm.sh to resolve the issue. Thank you for my readers for pointing me the error.
As a programmer, I strongly believe in perfection whatever it may be, especially in installing any new software. I have come across many people who just simply follow the instructions online and install the applications without knowing how to uninstall them later.
Always remember, the important thing for a programmer is to know how to uninstall the applications properly rather than installing them with simple clicks and double clicks. Trust me, this knowledge will later help you in debugging the issues at the time of deployment in production servers.
Without any further ado, let’s get started.
We may use different versions of Node.js which depends on different project requirements. In order to do that, we should maintain different versions of them in our system to switch easily whenever required. That is where NVM is used for. (Not recommended in Production Servers)
NVM — Node Version Manager, pretty straight forward definition. Used to manage multiple versions of Node.js in our system so that we can easily switch based on our project requirements.
Installing NVM
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.37.2/install.sh | bash
When you run the above command, you may encounter an issue like below.
We need to install xcode command line tools and run the script again. Just accept the install option and continue. Once you are done with that, continue running the script again.
This is what you should see when you run the script.
So what generally the script does is it downloads, clones the nvm repository to .nvm and add source lines to our profile. (.zsh or .bash_profile, etc).
Now close the terminal and open a new terminal again, type
nvm --version
But, unfortunately you will encounter an issue again like above. Why because nvm generally looks for the profile ‘.zshrc’ but our default mac shell has‘.zsh’ profile. So we need to create a profile and run the script again. To create profile use below command,
touch ~/.zshrc
If everything works fine, you will should be able to see the below result.
Now to see what versions are installed, use the below command
nvm ls
The above command will result in the output just like below,
There are no Node.js installed in our system currently. If you want to see the entire list of node versions which are present remotely then use below command
nvm ls-remote
The above command will gives us a very long list of node versions.
Always prefer the versions which has long term support (LTS) which I consider as safe and also by many. Currently (at the time writing this article), the latest LTS version is v14.16.0.
In order to install the latest LTS version, follow any of the the below commands.
nvm install 14.16.0
or
nvm install —- lts
The above command will install the latest LTS version of Node.js.
You should see the output like this,
You will be getting both Node.js and node package manager (npm) which is used to install packages in your node related projects. Check the versions now using below commands.
node —- version
npm -— version
You should able to see like below
Now, let me install another version of node and switch to that version, let’s see if it works or not. Lets install version v12.21.0.
nvm install 12.21.0
The output should be,
if we check the versions, we will get something like this,
If we list the versions which we have installed so far in our system by using below command, we should see,
If you observe the green arrow which is pointing to the version 12, that means currently the shell environment is running version 12 node, but if you see the “default” , it is pointing to the latest version 14. That means that anytime you start a new shell, you will be sourced again to version 14 but not to version 12. In order to make the default node to 12, follow the below command.
nvm alias default 12.0.0
So now lets switch to our latest version by using the below command,
nvm use 14.16.0
To uninstall any versions of your node, lets say I want to uninstall version 12, follow the below command,
nvm uninstall 12.21.0
You will get the result like this,
You will be having only one version in the system. You can check by listing the node versions.
Finally, to remove your NVM completely from your system, follow the below command, (before that uninstall any of your node versions if exist by using uninstall command above)
rm -rf “$NVM_DIR”
And remove the lines from “.zshrc” below, which was created at the start of NVM installation process. (You can find at the start of this article)
So now you know how to install NVM, install versions of Node.js, switch to different versions, uninstalling Node.js, and finally uninstalling the NVM.
Thats it for now. Thanks a lot for your time. Please add comments if you have any doubts, or if I had missed something or any thing is wrong, I will rectify and publish again. I open to comments.
Thank you once again. Have a nice day.
Reference