The installation of Node.js from an installer is not recommended. It is because Node.js is constantly getting updates that include bug fixes and new features. So if you want to stay up-to-date with Node.js, you may want to take a look at some node version managers.
nvm or Node Version Manager is the first version manager built for Node.js. Let’s take a look at how to install and use it.
I am going to do it in steps.
Open the nvm repository. You can open it by clicking here. Now scroll down to the installation section.
Now you need to install its binary. For that, if you have curl, you can just copy-paste the following line inside your terminal and press Enter.
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.36.0/install.sh | bash
Now if you again look at the documentation now, you will see that you need to copy-paste a shell script in either your ~/.bash_profile, ~/.zshrc, ~/.profile, or ~/.bashrc files. Just for convenience, I am also adding this script below.
export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")" [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
And that’s it. You have successfully installed and configured nvm.
Now if you want to install the latest version of Node.js, you can do it by just typing the following in the terminal.
nvm install node
If you want to install a particular version, just add the following in the terminal and press Enter.
nvm install --version
If you want to run a particular version, just add the following command in your terminal.
nvm run node --version
You can take a look at its documentation to understand the working of nvm better.
I generally don’t use nvm because it slows down my terminal. The script we need to do after installation of the binary makes nvm load every time you open a new terminal. But if you don’t mind 4 or 5 seconds of delay then definitely check this one out.