Managing NodeJS Versions on Ubuntu 20.04 Using ‘n’

If you’re a Node.js developer who frequently works on multiple applications and projects, managing different Node.js versions becomes essential. Fortunately, for Linux users, there are several utilities available for this purpose. One of the most straightforward and effective tools is the n Node version manager, which simplifies the management of Node.js versions.

In this guide, we’ll walk you through how to use n to manage Node.js versions on Ubuntu 20.04.

Prerequisites

  • A server running Ubuntu 20.04.
  • A root password configured on the server.

Getting Started

First, it’s a good practice to update your system packages to their latest versions. Execute the command below to update them all:

apt-get update -y

Post updating the packages, you’ll need to install some additional dependencies on your server. Use the following command to do so:

apt-get install curl git make -y

Once the dependencies are installed, proceed to the next step.

Install n

There are multiple ways to install n on Linux, but using npm is the simplest. Run this command to install n:

npm install -g n

If npm is not installed on your system, you can install it by downloading the n script from GitHub:

curl -L https://raw.githubusercontent.com/tj/n/master/bin/n -o n
bash n lts

This command installs Node.js, npm, and n onto your system. To ensure n is accessible via your system path, add it manually:

nano ~/.bashrc

Add the following lines:

export N_PREFIX=$HOME/.n
export PATH=$N_PREFIX/bin:$PATH

Save and close the file, then activate the new path with:

source ~/.bashrc

An alternative approach is to use the n-install script. Execute the command below to download and run it:

curl -L https://git.io/n-install | bash

Activate the n system path with:

source ~/.bashrc

To remove n from your system, execute:

n-uninstall

To update n to the latest version, use the command:

n-update

Verify the version of n by running:

n --version

You should see an output similar to:

7.3.0

To review all available options with n, execute:

n --help

The output should resemble:

Usage: n [options] [COMMAND] [args]
Commands:
  n                              Display downloaded Node.js versions and install selection
  n latest                       Install the latest Node.js release (downloading if necessary)
  n lts                          Install the latest LTS Node.js release (downloading if necessary)
  n                     Install Node.js  (downloading if necessary)
  n install             Install Node.js  (downloading if necessary)
  n run  [args ...]     Execute downloaded Node.js  with [args ...]
  n which               Output path for downloaded node 
  n exec   [args...]  Execute command with modified PATH, so downloaded node  and npm first
  n rm                  Remove the given downloaded version(s)
  n prune                        Remove all downloaded versions except the installed version
  n --latest                     Output the latest Node.js version available
  n --lts                        Output the latest LTS Node.js version available
  n ls                           Output downloaded versions
  n ls-remote [version]          Output matching versions available for download
  n uninstall                    Remove the installed Node.js
Options:
  -V, --version         Output version of n
  -h, --help            Display help information
  -p, --preserve        Preserve npm and npx during install of Node.js
  -q, --quiet           Disable curl output. Disable log messages processing "auto" and "engine" labels.
  -d, --download        Download only
  -a, --arch            Override system architecture
  --all                 ls-remote displays all matches instead of last 20
  --insecure            Turn off certificate checking for https requests (may be needed from behind a proxy server)
  --use-xz/--no-use-xz  Override automatic detection of xz support and enable/disable use of xz compressed node downloads.

Manage Node.js Versions

To install the latest Node.js version, use:

n latest

Expect an output like:

  installing : node-v16.4.0
       mkdir : /root/n/n/versions/node/16.4.0
       fetch : https://nodejs.org/dist/v16.4.0/node-v16.4.0-linux-x64.tar.xz
   installed : v16.4.0 (with npm 7.18.1)

Next, verify the installed Node.js version:

node --version

The output should be:

v16.4.0

To install the latest LTS version of Node.js, use:

n lts

Expected output:

   installed : v14.17.1 (with npm 6.14.13)

For installing a specific Node.js version, such as 14.15.0, use:

n 14.15.0

You should see:

installing : node-v14.15.0
       mkdir : /root/n/n/versions/node/14.15.0
       fetch : https://nodejs.org/dist/v14.15.0/node-v14.15.0-linux-x64.tar.xz
   installed : v14.15.0 (with npm 6.14.8)

To only download a specific Node.js version, like 14.8.0, run:

n -d 14.8.0

Output:

  installing : node-v14.8.0
       mkdir : /root/n/n/versions/node/14.8.0
       fetch : https://nodejs.org/dist/v14.8.0/node-v14.8.0-linux-x64.tar.xz

To execute the downloaded Node.js version, such as 14.8.0, use:

n run 14.8.0

You should see:

Welcome to Node.js v14.8.0.
Type ".help" for more information.

Exit from the Node.js shell with:

> .exit

To list all installed Node.js versions, use the command:

n ls

The output should be:

node/14.8.0
node/14.15.0
node/14.17.1
node/16.4.0

To view all available Node.js versions, execute:

n ls-remote

You will see:

Listing remote... Displaying 20 matches (use --all to see all).
16.4.0
16.3.0
16.2.0
16.1.0
16.0.0
15.14.0
15.13.0
15.12.0
15.11.0
15.10.0
15.9.0
15.8.0
15.7.0
15.6.0
15.5.1
15.5.0
15.4.0
15.3.0
15.2.1
15.2.0

If you want to list all available Node.js versions, execute:

n ls-remote --all

To find the path of a specific Node.js version, run:

n which node/14.8.0

You should get:

/root/n/n/versions/node/14.8.0/bin/node

To switch between different Node.js versions, simply execute:

n

You’ll be prompted to select a Node.js version to switch:

    node/14.8.0
    node/14.15.0
  ο node/14.17.1
    node/16.4.0

Use up/down arrow keys to select a version, return key to install, d to delete, q to quit

Highlight your desired Node.js version and press Enter to set it as the default.

Remove Node.js Version with n

To remove a specific Node.js version, e.g., node/16.4.0, execute:

n rm node/16.4.0

Use the command below to prune all downloaded Node.js versions:

n prune

To uninstall all Node.js versions, run:

n uninstall

You’ll see a similar output:

Do you wish to delete node and npm from /root/n? Y

Uninstalling node and npm
/root/n/bin/node
/root/n/bin/npm
/root/n/bin/npx
/root/n/include/node
/root/n/lib/node_modules/npm
/root/n/share/doc/node
/root/n/share/man/man1/node.1
/root/n/share/systemtap/tapset/node.stp

Conclusion

This guide has walked you through the process of installing and managing Node.js versions using the n version manager. This tool is invaluable for developers who need to effortless switch between different projects and applications.

Frequently Asked Questions (FAQ)

What is n Node version manager?

n is a Node.js version manager that allows you to easily install, manage, and switch between different Node.js versions on your machine.

Why do I need to update my system before installing n?

Updating your system ensures that you have the latest security patches and system dependencies required for a smooth installation.

How can I install n if npm is not pre-installed?

If npm is not installed, you can still install n by directly downloading the installation script from GitHub as shown in the guide.

Can I switch back to a previous version of Node.js?

Yes, using n, you can list all installed Node.js versions and switch back to any version of your choice.

What does the `n prune` command do?

The `n prune` command removes all downloaded Node.js versions except for the one currently in use.