DEV Community

Ibrahim
Ibrahim

Posted on

Managing Multiple Node.js Versions on Linux

Node.js has many versions, and different projects may require different versions.

To easily manage multiple versions of Node.js on Linux, we can use an npm package called n.

To start using n, install the pacakge using the following command:

npm install -g n
Enter fullscreen mode Exit fullscreen mode

If Node.js and npm are not installed yet, use the following command instead:

curl -L https://bit.ly/n-install | bash
Enter fullscreen mode Exit fullscreen mode

Once installed, you can install and switch to a specific Node.js version using n <version>. For example:

n 16.17.1
# copying : node/16.17.1
# installed : v16.17.1 to /usr/local/bin/node
# active : v16.17.1 at /usr/local/bin/node
Enter fullscreen mode Exit fullscreen mode

If you encounter an error like this:

Error: EACCES: permission denied, symlink '/usr/local/n/versions/node/16.17.1/bin/node' -> '/usr/local/bin/node'
Enter fullscreen mode Exit fullscreen mode

You can solve this by setting the N_PREFIX environment variable in your .profile, .bashrc or .zshrc file (depending on your shell) to $HOME/.n.

export N_PREFIX=$HOME/.n
export PATH=$N_PREFIX/bin:$PATH
Enter fullscreen mode Exit fullscreen mode

Then, restart your shell or run the following command:

source ~/.bashrc # depending on your shell
Enter fullscreen mode Exit fullscreen mode

Now, try installing it again:

n 16.17.1
Enter fullscreen mode Exit fullscreen mode

Once installation is complete, verify the current Node.js version:

node -v
# v16.17.1
Enter fullscreen mode Exit fullscreen mode

To list all installed Node.js versions and switch to one of them, run the following command:

n
Enter fullscreen mode Exit fullscreen mode

A list of installed Node.js versions will appear. Use the up/down arrow keys to highlight a version, Enter to activate it, d to delete it, and q to exit. For example:

#    node/14.21.3
#    node/16.7.0
#    node/16.17.0
#  ο node/16.17.1
#    node/22.11.0
#    node/22.15.1
#    node/22.16.0
Enter fullscreen mode Exit fullscreen mode

To install the latest or LTS version of Node.js, use the latest or lts keywords.

n latest
n lts
Enter fullscreen mode Exit fullscreen mode

That's how to manage multiple Node.js versions on Linux. Hope this guide was helpful!

Top comments (0)