There are new Node.js major releases every six months, when a new odd version is released the previous even version undergoes a transition to Long Term Support (LTS), which gives that version 18 months of active support from the date it is designated LTS. Luckily nvm offers an easy way to install and switch to different Node.js versions. In this tutorial, we will show you how to install different Node.js versions using nvm.
Table of Contents
- OS supported by Node.js.
- Access to the root user account (or access to an admin account with root privileges)
For this example we will be using Ubuntu 20.04 as the default OS, Node.js can be also installed and run on many different distributions.
Log in and update the system
First, you will need to, log in to your Ubuntu 20.04 server via SSH as the root user: ssh root@IP_Address -p Port_number You will need to replace ‘IP_Address‘ and ‘Port_number‘ with your server’s respective IP address and SSH port number. Additionally, replace ‘root’ with the username of the admin account if necessary.
Before starting, you have to make sure that all Ubuntu OS packages installed on the server are up to date. You can do this by running the following commands:
apt-get update -y apt-get upgrade -y
You can now go ahead and install nvm using the following commands:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash source ~/.bashrc
We use the first command to download and execute the script that installs nvm and with source ~/.bashrc, we are running the source command with .bashrc file as an argument for the changes to apply.
After the installation has finished, we can check if nvm has been installed with the following commands:
nvm –version nvm -v
The npm can be used to install different Node.js versions. For example, you can use the following command to check all the available versions for Node.js:
This will print a large list of all the available versions for Node.js starting from version v0.1.14 to the latest version currently available.
Installing Node.js and switching versions
Now to install the latest Node.js version we can simply run:
nvm install node
If we now check the Node.js version with:
node -v v17.5.0
You will notice that the latest available version is now installed on our server. If we want to install the latest LTS (Long term support) Node.js version we can use this command adding the –lts flag at the end:
nvm install –lts
To install a specific Node.js version, for example, let’s say we need Node.js v14.15.3 installed, you can use the following command:
nvm install 14.15.3
Now to list all the Node.js versions currently installed on your server run:
Switching between versions is also very easy with nvm, to switch to another Node.js version that is already installed on your server, simply run the command:
nvm use 17.1.0
For switching to different versions that are already installed you can use the following commands for different scenarios:
nvm use node #uses latest Node.js version available on server. nvm use --lts #This will switch to the latest LTS version nvm use 14.15.3 #Switches to the version v14.15.3 specifically.
Now to run node app.js with a specific version, you can use:
nvm run 17.5.0 app.js
To run the application with PATH pointing to a certain version run:
nvm exec 17.5.0 node app.js
Surely changing versions comes in handy but to keep the default version without the need to switch versions every time and check what version you are currently on, you can use this command to set the default Node.js version :
nvm alias default 14.15.3
Or simply use this command to set the latest available Node.js version as the default one:
nvm aliast default node
That’s it, you have learned how to install Node.js and how to install and switch between versions.
Of course, you don’t have to install and switch Node.js versions by yourself, if you use one of our Linux VPS Hosting services, in which case you can simply ask our expert Linux admins to install specific Node.js versions for you. They are available 24×7 and will take care of your request immediately.
PS. If you liked this post on how to install Node.js versions with nvm, please share it with your friends on the social networks using the buttons below or simply leave a reply in the comments sections. Thanks.