Home Nodejs How To Manage Nodejs Versions With n In Linux

How To Manage Nodejs Versions With n In Linux

By sk

This guide gives you a brief introduction to "n", an unofficial node version management tool and then explains how to easily manage nodejs versions with n in Linux operating systems.

What is n, the node version manager?

NodeJS requires no introduction. Nodejs is a runtime environment for JavaScript built on top of Google's V8 engine. Nodejs comes with a built-in package manager called npm to install and manage node versions and there are also many third party tools exists to manage node versions. n is one one of the third-party node version management tool. Yes, it is just a single letter - n.

The n is an interactive node version manager to manage a single or multiple NodeJS versions easily! No subshells, no profile setup, no convoluted API, no learning curve, it is a just simple tool. It is just shell script and its code is freely available in GitHub.

1. Install n in Linux

The easiest way to install n is through npm. As stated already, Npm is the built-in package manager for Nodejs and comes pre-installed with Nodejs.

I assume that you already have the latest Nodejs on your Linux machine. If you haven't installed NodeJS yet, refer the following guide:

Next, run the following command to install n in Linux:

$ npm install -g n

If npm is not available or you don't want to install n with npm, you can directly grab the n script from GitHub and install it like below:

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

This will install both node lts version and npm.

1.2. Create home directory for node installation

By default, n installs node under n/versions sub-directory of /usr/local/ directory, which is defined in the N_PREFIX environment variable. As you already know, we require sudo permission to install node under /usr/local/n/versions/node/ directory. To avoid using sudo, you can create a dedicated directory for node installation in your $HOME and add them to your PATH.

To create a home directory for node installation, edit your user profile file, for example ~/.bashrc:

$ vi ~/.bashrc

Add the following lines:

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

Press ESC key and type :wq to save the file and exit.

1.3. Install n with n-install script

The another way to install n is through n-install script, which installs n directly from the GitHub.

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

This command will do the following set of tasks:

  • First, it sets both PREFIX and N_PREFIX to $HOME/n;
  • And, installs n to $HOME/n/bin;
  • Next, modifies the initialization files of supported shells to export N_PREFIX;
  • Then, adds $HOME/n/bin to the PATH;
  • Finally, installs the latest LTS Node.js version.

2. Manage nodejs versions with n in Linux

Installing and managing nodejs versions with n is pretty easy! Let us see a few examples.

2.1. Getting help

If you're new to n, it is probably a good idea to bring up the help section and learn how to use n.

To view the help section of n, simply run:

$ n --help

Go though the help manual to get a glimpse of each command usage.

 Usage: n [options] [COMMAND] [args]
 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
 -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 (if available)
   -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.
 install: i
   latest: current
   ls: list
   lsr: ls-remote
   lts: stable
   rm: -
   run: use, as
   which: bin
 Numeric version numbers can be complete or incomplete, with an optional leading 'v'.
   Versions can also be specified by label, or codename,
   and other downloadable releases by /
 4.9.1, 8, v6.1    Numeric versions lts               Newest Long Term Support official release latest, current   Newest official release auto              Read version from file: .n-node-version, .node-version, .nvmrc, or package.json engine            Read version from package.json boron, carbon     Codenames for release streams lts_latest        Node.js support aliases and nightly, rc/10 et al

2.2. Install Nodejs versions using n

To install the latest nodejs version with n, simply run:

$ n latest

Sample output:

 installing : node-v16.2.0
        mkdir : /home/ostechnix/.n/n/versions/node/16.2.0
        fetch : https://nodejs.org/dist/v16.2.0/node-v16.2.0-linux-x64.tar.xz
    installed : v16.2.0 (with npm 7.13.0)
 Note: the node command changed location and the old location may be remembered in your current shell.
          old : /home/ostechnix/.nvm/versions/node/v16.1.0/bin/node
          new : /home/ostechnix/.n/bin/node
 To reset the command location hash either start a new shell, or execute PATH="$PATH"
Install nodejs version with n in Linux
Install nodejs version with n in Linux

The old version of node will be moved to the new location i.e. /home/ostechnix/.n/bin/node in my case. Open a new shell session or restart the Terminal session to use node version from the new location.

Check the installed node version:

$ node --version

Check npm version:

$ npm -v

Similarly, you can install a LTS node version using n like below:

$ n lts

Sample output:

    installing : node-v14.17.0
        mkdir : /home/ostechnix/.n/n/versions/node/14.17.0
        fetch : https://nodejs.org/dist/v14.17.0/node-v14.17.0-linux-x64.tar.xz
    installed : v14.17.0 (with npm 6.14.13)

You can also install a specific Nodejs version, for example 16.0.0, using command:

$ n 16.0.0

As you may have noticed, n creates separate directories for each node version. For example, it creates /home/ostechnix/.n/n/versions/node/16.2.0 directory for latest node version, n/versions/node/14.17.0 for node LTS version and n/versions/node/16.0.0 for node version 16.0.0. This way it keeps each version is not mingled with one another, and we can switch to any version at any time.

2.3. Download Nodejs versions, but don't install

If you don't want to install a Nodejs version, but only want to download it, use -d, --download option:

$ n -d 15.8.0

This command will only download the node 15.8.0 version, but won't install it.

Sample output:

 installing : node-v15.8.0
        mkdir : /home/ostechnix/.n/n/versions/node/15.8.0
        fetch : https://nodejs.org/dist/v15.8.0/node-v15.8.0-linux-x64.tar.xz

2.4. List locally downloaded Nodejs versions

To view all downloaded Nodejs version in your local cache, simply run:

$ n ls


$ n list

Sample output:


2.5. List available Nodejs versions from the remote repository

We can use ls-remote option to view the recent 20 versions of Nodejs from the npm registry.

$ n ls-remote


$ n lsr

Sample output:

 Listing remote… Displaying 20 matches (use --all to see all).

The most recent version will be shown on the top.

By default, it lists 20 versions. You can also display a certain number of node versions as well.

$ n lsr 10

To list all available nodejs versions, use --all option.

$ n ls-remote --all

2.6. Display the path of the downloaded Nodejs version

To print the full path of a specific downloaded node version, run:

$ n which node/16.2.0

Sample output:


2.7. Switch Nodejs versions

The last installed version will be the default node version. If you want to switch to different version, type n and choose a version from the list of downloaded node versions.

$ n

This command will display all downloaded nodejs versions. You can choose the required version using up/down arrows and hit ENTER key to install it.

  ο node/16.0.0

 Use up/down arrow keys to select a version, return key to install, d to delete, q to quit
Switch Nodejs versions using n

The default version is indicated with a small circle in front of it.

If you don't want a specific downloaded nodejs version, simply choose it and press d to delete it. To exit, press q.

2.8. Execute downloaded Nodejs version

As stated already, the last installed version is the default node version. If you want to execute a different version without setting it default, you can use this command:

$ n run 15.8.0

Sample output:

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

Press Ctrl+D or type .exit to exit from node prompt.

2.9. Remove downloaded Nodejs versions

List how many node versions are downloaded with command:

$ n ls

To remove a specific version, just mention its version number like below:

$ n rm node/16.0.0

Alternatively, type n to view the available node versions:

$ n

Select the version you want to remove using up/down arrows and press d to delete it. You must be careful while deleting a node version. Because you will not be prompted to confirm the deletion. Once you hit the d key the selected version will be gone.

To remove all downloaded versions except the installed version, run:

$ n prune

To completely remove the installed Nodejs version, run:

$ n uninstall

You will asked if you want to delete both Node and Npm from your system. Type y and hit ENTER key to completely remove node and npm installed by n from your system.

 Do you wish to delete node and npm from /home/ostechnix/.n? y
 Uninstalling node and npm

However, this will not remove the node version installed using NVM or your distributions package manager. It will only remove the node versions that are installed with n.


The n script will be helpful when you want to use different Nodejs version for different project. You don't need to remove one version of Nodejs and install another for each project. You can install multiple Nodejs versions and instantly switch from one Nodejs version to another.


You May Also Like

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More