Installation & management of multiple Node.js versions

Foreword

The following article explains how to install and manage multiple Node.js versions on your server.


Prerequisites

  • A server with Debian or Ubuntu as the operating system



Installation of nvm


The latest nvm installation script can be found on the following GitHub page of the nvm project:\nhttps://github.com/nvm-sh/nvm?tab=readme-ov-file#installing-and-updating


The following section describes the installation of Node.js version 20.16 and nvm version 0.40.1 as an example. No elevated rights are required for the entire process, but the installation is user-specific and should therefore be carried out with the system user of the web application running on the server.


First download the installation script using curl or alternatively wget and execute it:


  1. curl:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bash


  1. wget:
wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bash


It is then necessary to edit the user's bash profile so that all nvm commands are available when logging in. To do this, execute the following command to edit or create the corresponding configuration file:


nano ~/.bash_profile


Then insert the following export statement so that the required environment variables are defined and made accessible for the shell environment:


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



Management of the versions

Save the changes, log out of the server and then reconnect so that the changes take effect.


You can then install version 20.16.0 of NodeJS as follows:

nvm install 20.16.0


To ensure that the installed version is also used in the CLI, you must then execute the following command:

nvm use 20.16.0



Review of the changes


Check the changes you have made as follows:


node:

system user@sXXXXX:~$ node -v
v20.16.0 → expected output


nvm:

system user@sXXXXX:~$ nvm ls
-> v20.16.0 → expected output