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: https://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.4 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.


The following commands download the installation script and execute it directly:


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


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


The script stores the necessary entries in the .bashrc, so that these changes must then be loaded as follows:

source ~/.bashrc


You can then test the execution of the nvm command as follows:

nvm --version


If the command cannot be found, you must make the following adjustment manually:


If the file .bashrc does not exist in your home directory, the following adjustments can be made in the .bash_profile.

nano ~/.bashrc


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