I’m using PM2 for managing all the server processes. So, installing node and nvm (node version manager) is now one of the first things I do during an application server setup.

node and nvm, just like ruby and rvm (ruby version manager) are always installed under the non-root user account.

install nvm

nvm can be installed by using wget or curl

using wget

$> wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash

using curl

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

Running either of the above commands downloads a script and runs it. The script clones the nvm repository to ~/.nvm, and attempts to add the source lines from the snippet below to the correct profile file.

~/.bash_profile~/.zshrc~/.profile, or ~/.bashrc

The script that gets added is

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" # This loads nvm

After this, we either need to restart the current shell or by

$> source ~/.bashrc

install node

To list the node versions available for install

nvm ls-remote

To install a specific version

$> nvm install 16.10.0

To see the current list of installed node versions

$> nvm ls

And to use a specific version

$> nvm use 16.10

set a node version as default

When nvm is installed, it generates a .nvmrc file in the home directory. The default version of node to be used can be entered there.

$> cat ~/.nvmrc
16.10

Reference: github.com/nvm-sh/nvm