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
If Node.js and npm are not installed yet, use the following command instead:
curl -L https://bit.ly/n-install | bash
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
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'
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
Then, restart your shell or run the following command:
source ~/.bashrc # depending on your shell
Now, try installing it again:
n 16.17.1
Once installation is complete, verify the current Node.js version:
node -v
# v16.17.1
To list all installed Node.js versions and switch to one of them, run the following command:
n
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
To install the latest or LTS version of Node.js, use the latest or lts keywords.
n latest
n lts
That's how to manage multiple Node.js versions on Linux. Hope this guide was helpful!
Top comments (0)