Node.js is a JavaScript execution tool for server-side programming. It allows developers to create scalable backend functionality using JavaScript, a language most people are already familiar with from browser-based web development.
In this article, we will show you the most flexible way to install Node.js on an Ubuntu 22.04 server with Nvm (Node Version Manager).
Before starting the installation, let’s learn the latest nvm version from the official nvm repository https://github.com/nvm-sh/nvm/releases. The current official version appears to be 0.39.7.
NVM Installation
You can use the apt package manager to get the new version of nvm. First refresh your local package directory by typing:
sudo apt update
Then download the Nvm installation sh file according to the current version:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
The above code will install the nvm script into your user account. To use this, you first need to source your .bashrc file:
source ~/.bashrc
Use the nvm –version command to see which nvm version we are using.
nvm --version
0.39.7
You can now ask NVM which Node versions are available and proceed with the installation steps for Node.js based on the latest version:
nvm list-remote
// output
v20.4.0
v20.5.0
v20.5.1
v20.6.0
v20.6.1
v20.7.0
v20.8.0
v20.8.1
v20.9.0 (LTS: Iron)
v20.10.0 (LTS: Iron)
v20.11.0 (LTS: Iron)
-> v20.11.1 (Latest LTS: Iron)
v21.0.0
v21.1.0
v21.2.0
v21.3.0
v21.4.0
v21.5.0
v21.6.0
v21.6.1
v21.6.2
Node.js Installation
Copy the latest updated version from the versions listed above and run the following command to install this Node version:
nvm install 20.11.1
If there is more than one node version on your machine, you can make nvm use the version you want with the nvm use command.
nvm use 20.11.1
The version selected with the Use command will only have effect in your current session. If you want to make the version you selected permanent as the default, run the command below.
nvm alias default node
You can see the different versions you have installed and which current version you are currently using with the nvm list command:
nvm list
-> v20.11.1
default -> node (-> v20.11.1)
node -> stable (-> v20.11.1) (default)
stable -> 20.11 (-> v20.11.1) (default)
Uninstalling Node.js
To uninstall a version of Node.js that you installed using nvm, first determine whether it is the current active version:
nvm current
v20.11.1
If the version you are targeting is not the current active version, run the following command:
nvm uninstall v19.1.0
// Output
Uninstalled node v19.1.0
This command will remove the selected Node.js version. If the version you want to remove is the currently active version, you will first need to disable nvm to activate your changes:
nvm deactivate
After this process, you can remove the current version using the uninstall command used previously.
Conclusion
There are many ways to install and run Node.js on your Ubuntu 22.04 server. While it’s easiest to use the bundled version from the Ubuntu repository, using Nvm or NodeSource PPA offers additional flexibility.
For more information about programming with Node.js, please see the Node.js api documentation.