Introduction

Managing multiple versions of Node.js on a single Linux server is a common requirement for developers and DevOps teams. Different applications often depend on different Node.js versions, making it essential to switch between versions without affecting existing deployments. Whether you’re maintaining legacy applications, testing new features, or supporting multiple projects on the same server, having the ability to run multiple Node.js versions can save significant time and effort.

In this guide, we’ll walk through a traditional Linux-based approach using the update-alternatives utility to install and manage multiple Node.js versions on an Ubuntu server. This method allows administrators to switch between versions easily while maintaining a clean and organized environment.

Prerequisites

Before you begin, ensure that:

  • You are using an Ubuntu Linux server.
  • You have sudo or root privileges.
  • Internet access is available to download Node.js packages.
  • Basic familiarity with Linux command-line operations.

Step 1: Install the Default Node.js Version

First, install Node.js and npm using the Ubuntu package manager.

apt-get update
apt-get install -y nodejs npm

Verify the installed version:

nodejs -v
npm -v

This installs the default version available in your Ubuntu repository.

Step 2: Create a Directory for Additional Node.js Versions

Create a dedicated directory to store additional Node.js versions.

mkdir -p /usr/local/bin/nodes
cd /usr/local/bin/nodes

Using a separate directory helps keep multiple installations organized.

Step 3: Download Another Node.js Version

Download the desired Node.js version directly from the official Node.js website.

wget https://nodejs.org/dist/v5.0.0/node-v5.0.0-linux-x64.tar.gz

Extract the package:

tar -xzf node-v5.0.0-linux-x64.tar.gz

After extraction, the Node.js binaries will be available under:

/usr/local/bin/nodes/node-v5.0.0-linux-x64/bin/

Step 4: Configure update-alternatives for Node.js

Register both Node.js versions with the Linux alternatives system.

Register the default Node.js installation:

update-alternatives --install "/usr/bin/node" "node" "/usr/bin/node" 100

Register the newly downloaded version:

update-alternatives --install "/usr/bin/node" "node" "/usr/local/bin/nodes/node-v5.0.0-linux-x64/bin/node" 50

The number at the end represents the priority. Higher values receive higher preference when automatic mode is used.

Step 5: Switch Between Node.js Versions

To view and select available Node.js versions:

update-alternatives --config node

Example output:

There are 2 choices for the alternative node.

  Selection    Path                                               Priority
------------------------------------------------------------
* 0            /usr/bin/node                                      100
  1            /usr/bin/node                                      100
  2            /usr/local/bin/nodes/node-v5.0.0-linux-x64/bin/node 50

Select the desired version by entering the corresponding number.

Verify the active version:

node -v

Step 6: Configure Multiple npm Versions

To ensure compatibility between Node.js and npm, configure npm alternatives as well.

Register the default npm version:

update-alternatives --install "/usr/bin/npm" "npm" "/usr/bin/npm" 100

Register the npm version from the downloaded Node.js package:

update-alternatives --install "/usr/bin/npm" "npm" "/usr/local/bin/nodes/node-v5.0.0-linux-x64/bin/npm" 50

Switch npm versions:

update-alternatives --config npm

Verify the active npm version:

npm -v

Best Practices

  • Keep each Node.js version in a dedicated directory.
  • Use consistent naming conventions for installation paths.
  • Document version requirements for each application.
  • Test applications after switching Node.js versions.
  • Consider using NVM (Node Version Manager) for development environments where frequent version switching is required.

Alternative Approach: Using NVM

For modern Linux environments, NVM provides a simpler and more flexible way to manage multiple Node.js versions.

Install NVM:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/master/install.sh | bash
source ~/.bashrc

Install multiple Node.js versions:

nvm install 18
nvm install 20
nvm install 22

Switch versions:

nvm use 20

Set a default version:

nvm alias default 20

NVM is generally recommended for individual users and development servers because it simplifies installation, upgrades, and version switching.

Conclusion

Running multiple Node.js versions on the same Linux server provides the flexibility needed to support different applications and development requirements. The update-alternatives method offers a reliable system-wide solution for managing multiple versions, making it suitable for shared servers and production environments. For developers and teams that frequently switch between versions, tools like NVM offer a more convenient and modern approach.

By implementing proper Node.js version management, organizations can reduce compatibility issues, simplify application maintenance, and ensure a stable development and deployment experience across their infrastructure.

Related Articles

  1. HOW TO INSTALL AND RUN THE SIMPLE WEB PAGE USING NODEJS IN WINDOWS
  2. Transform Your Workflow: A Step-by-Step Guide to JIRA REST API Automation with Nodejs and AXIOS

Leave a Reply