How to Manage NodeJS Versions with n in Ubuntu 20.04

If you are a Node.js developer who frequently switches between various applications and projects. Then you may need to find a tool that allows you to toggle between Node.js versions. In Linux, there are numerous utilities for managing several Node.js versions. Among these, the n Node version manager is a simple and useful tool for managing Node.js versions.

In this tutorial, we'll show you how to use n to manage Node.js versions on Ubuntu 20.04.

Prerequisites

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

Getting Started

First, it is recommended to update your system packages to the latest version. You can update all of them with the following command:

apt-get update -y

After updating all the packages, you may also need to install some required dependencies to your server. You can install all of them with the following command:

apt-get install curl git make -y

Once all the dependencies are installed, you can proceed to the next step.

Install n

There are several ways to install n on Linux. The simple and easiest way to install n is using npm.

Run the following command to install n:

npm install -g n

If npm is not installed in 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 will install Node.js, npm, and n to your system. By default, the above script does not install n in the system path. So you will need to add it to your user's path.

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 path with the following command:

source ~/.bashrc

You can also install n using the n-install script. You can download and run the n-install script as shown below:

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

Next, activate the n system path using the following command:

source ~/.bashrc

If you want to uninstall n from your system, run the following command:

n-uninstall

To update n to the latest version, run the following command:

n-update

To check the version of n, run the following command:

n --version

You should get the following output:

7.3.0

To check all options available with n, run the following command:

n --help

You should get the following output:

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 run  [args ...]     Execute downloaded node  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, run the following command:

n latest

You should get the following output:

  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, check the Node.js version using the following command:

node --version

You should get the following output:

v16.4.0

To install the Node.js lts version, run the following command:

n lts

You should get the following output:

   installed : v14.17.1 (with npm 6.14.13)

To install the specific Node.js version, run the following command:

n 14.15.0

You should get the following output:

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 download the specific Node.js version, run the following command:

n -d 14.8.0

You should get the following 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 use the downloaded Node.js version, run the following command:

n run 14.8.0

You should see the following output:

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

Next, exit from the Node.js shell with the following command:

> .exit

To list all installed versions of Node.js, run the following command:

n ls

You should get the following output:

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

To list all available Node.js versions, run the following command:

n ls-remote

You should get the following output:

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

The above command will list only 20 available versions. If you want to list all available Node.js versions, run the following command:

n ls-remote --all

To find the location of a specific Node.js version, run the following command:

n which node/14.8.0

You should get the following output:

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

To switch between different Node.js versions, run the following command:

n

You will be asked to select the Node.js version that you want 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

Select your desired Node.js version and hit Enter to set it as a default version.

Remove Node.js Version with n

To remove a specific Node.js version, run the following command:

n rm node/16.4.0

To remove all downloaded Node.js versions, run the following command:

n prune

To remove all installed Node.js versions, run the following command:

n uninstall

You should see the following 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

In the above guide, you learned how to install and manage Node.js versions using n. I hope this will helps you to easily switch between different projects and applications.

Share this page:

0 Comment(s)