Welcome to this comprehensive guide on installing Node.js on Ubuntu 22.04 via NVM (Node Version Manager). By the end, website owners, including those on Liquid Web hosting, will be able to install, manage, and switch between multiple versions of Node.js with ease.
NVM makes installing and managing different versions of Node.js a snap. Once Node.js is installed, your server environment is primed and ready to be used for the development of your Node.js application. At that juncture, or at any point along the way, Liquid Web staff is ready to assist you in setting up an advanced, stable development environment. You have a technology partner in your corner!
Main takeaways for the reader
These bullet points represent the core takeaways for the readership found in this article:
- An overview of Node.js and Node Version Manager (NVM)
- Installing NVM and dependencies
- Installing a Node.js version
- Using a specific a Node.js version
- Managing multiple installed Node.js versions with NVM
- Uninstalling Node.js versions
- Reviewing of the most popular NVM commands
What is Node.js?
Node.js is an open source JavaScript runtime environment. It is memory efficient and is intentionally asynchronous. It is deployable on multiple platforms, including Windows, Mac OS X, and Linux, and is suited for rapid development. It’s event-driven architecture make it highly scalable and useful for intensive data-driven applications.
Overview of Node.js and Node Version Manager (NVM)
Node.js allows you to run JavaScript on the server. It is used to build fast, scalable backend services and real-time web applications. As a website owner, Node.js lets you implement server-side code in JavaScript instead of PHP or other platforms. However, one challenge with Node.js is version management. Different projects may require different Node.js versions, and you need an easy way to switch between versions. This is where NVM comes in.
NVM or Node Version Manager is a bash script for managing multiple Node.js versions on one Linux server. With NVM installed, you can:
- Install new Node.js versions side-by-side.
- Switch between different versions easily.
- Set a default Node.js version for your system.
- Install Node.js versions on a per-user basis.
Overall, NVM brings flexibility and avoids version conflicts as you work on different Node.js projects.
This guide will take you through installing NVM and Node.js on Ubuntu 22.04 from scratch. Equipped with this knowledge, website owners can start building Node.js backends comfortably.
Prerequisites
Before following this NVM Ubuntu guide, make sure that you have the following prerequisites in place:
- You have an Ubuntu 22.04 server ready for use. The steps in this post were tested on a fresh cloud server.
- You have a regular, non-root user with sudo privileges set up on your server.
Do not run the NVM install scripts as root or with sudo. NVM manages Node.js versions per user.
If you need help setting up an initial user, refer to the Ubuntu 22.04 server setup guide.
Step #1. Install NVM dependencies
NVM itself has some dependencies like curl, wget, gpg, etc. Install them with following command syntax:
sudo apt update
sudo apt install curl wget gnupgYou likely already have these on your system. But it doesn’t hurt to update them before proceeding.
Step #2. Install NVM
Now, let’s install NVM itself. See the next sections.
2.1. Install NVM
We’ll fetch the official install script from the Node Version Manager GitHub repository. As your regular user, run the following command:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bashThis command downloads and executes the script to install NVM. Behind the scenes, it does the following:
- Clones the nvm repository to ~/.nvm.
- Adds the source lines to your shell profile (~/.bashrc, ~/.zshrc, etc.).
- Changes some permissions for NVM’s install directory.
After running the command, you’ll see output like this:
=> Appending nvm source string to /home/<user>/.bashrc
=> Appending bash_completion source string to /home/<user>/.bashrc
...
=> Close and reopen your terminal to start using nvm or run the
following to use it now:
export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh" # This loads nvm2.2. Load NVM
To load NVM now, copy and paste the two export lines from the output into your terminal and press the Enter key. Alternatively, you can open a new shell session entirely by closing and reopening your terminal window/tab.
Verify NVM is set up properly by checking its version using the nvm –version command:
nvm --versionYou should see the following details on the screen:
0.39.3Hooray, NVM is now installed and ready!
Step #3. Install a Node.js version
It’s time to use Node Version Manager (NVM) superpowers and install some Node.js — or Node for short.
3.1. Install a Node v18
For the purpose of this tutorial, we will take Node.js v18 for a spin with the nvm install node command:
nvm install nodeBehind the scenes, this command does the following tasks:
- Downloads the Node.js software from the repository with the official binaries.
- Unpacks it into a versioned subfolder under ~/.nvm.
- Links the Node (node) and NPM or Node Package Manager (npm) binaries appropriately.
The output on the screen will look similar to following example:
Downloading and installing node v18.13.0...
...
Now using node v18.13.0 (npm v8.19.2)3.2. Confirm the Node.js version
Now, let’s confirm the Node.js version via the node -v command:
node -vYou should see node v18.13.0 printed out on the display, proving NVM installed Node v18 correctly.
3.3. List all Node.js versions installed
To see all Node.js versions installed by NVM, run the nvm ls command:
nvm lsFor now, it will show output similar to this example:
-> v18.13.0
system
default -> node (-> v18.13.0)
...This output above indicates the following statuses:
- That node v18.13.0 was installed and is currently active (notice the arrow pointing to the version value of v18.13.0).
- That there’s also the system Node.js from apt.
- That default points to node alias (for latest Node).
3.4. Add another Node version
Let’s add another Node version, say Node v16, which was is the current LTS (Long Term Support) instance at the time of this article’s publication. So, we can use the nvm install –lts command to add it:
nvm install --lts3.5. List all Node.js versions installed again
List versions again with the nvm ls command. You’ll now see both Node v16 and Node v18 available.
This demonstrates the true power of NVM, which allows you to effortless switch Node.js versions as needed. Keep reading for details on managing multiple versions.
Step #4. Managing Node.js versions with NVM
Now for the fun part, using NVM to switch between different Node releases. Follow along in the subsequent sections to see how this can be done.
4.1. List the available Node.js versions
To see all Node versions available for installation, run the nvm ls-remote command:
nvm ls-remoteThis ls-remote command queries the official Node servers and shows an extensive list of versions, both LTS and current versions. As of the time of this writing, the output includes the versions list below:
v18.13.0 (Latest LTS: Hydrogen)
v16.19.0 (LTS: Gallium)
v14.21.2 (LTS: Fermium)Step # 5. Install specific Node.js versions
5.1. Passing the version number with the nvm install command
You can install a specific version by passing the version number to the nvm install command:
nvm install 12.22.12The command syntax used above will download and install Node v12.22.12.
Similarly, install an older LTS release run the nvm install –lts=erbium command:
nvm install --lts=erbium5.2. List locally installed versions
List locally installed versions with the nvm ls command again:
nvm ls
-> v12.22.12
v14.21.2 (LTS: Fermium)
v16.19.0 (LTS: Gallium)
v18.13.0
system
default -> node (-> v18.13.0) (default)Now Node v12, Node v14, Node v16, and Node v18 releases are ready as shown above.
5.3. Set the default Node.js version
The default Node.js version will be chosen automatically in new shell sessions. To set this default version, use the following command syntax:
nvm alias default 14.21.2Note that now Node v14 becomes the default version used. You can verify the current default version with the following command:
nvm run default --versionYou can change the default Node version anytime with the nvm alias default command.
5.4. Use a specific Node.js version
Suppose you want to use Node v16 for a project. Just run the nvm use 16 command:
nvm use 16This command switches the node and npm commands to point to Node v16 binaries. Check the node version with the node -v command:
node -v
v16.19.0Now, this shell session is locked to Node v16.19.0. The change persists temporarily in the shell. Opening a new terminal window will activate the default Node shown above instead.
5.5. Run Node commands under specific versions
You can also run Node/NPM under a specific version without changing the global pointers. Here is an example:
nvm run 16 --versionThis command will run node –version specifically under Node v16 only.
Similarly, you can install packages for a single Node release:
nvm exec 12.22.12 npm install -g http-serverThis uses Node v12.22.12 to globally install the http-server package.
Uninstall Node.js versions
Removing a Node.js version is as easy as running the nvm uninstall command:
nvm uninstall 12.22.12Now Node v12 is uninstalled. You can then run the nvm ls command to confirm only Node v16 and Node v18 remain.
Note that you can only remove Node.js versions that are not currently active or set as the default version. Therefore, you may need to first switch the used version to another version with the nvm use command if needed.
A review of the most popular NVM commands
In summary, Node Version Manager makes juggling Node.js releases a breeze. Some examples of the most popular NVM commands include the following:
- nvm ls — list installed Node versions
- nvm ls-remote — list all available Node versions
- nvm install 8.19.2 — install a specific Node version
- nvm uninstall 8.19.2 — remove a Node version
- nvm alias default 8.19.2 — set a global default Node version
- nvm use 8.19.2 — switch current shell to a Node version
- nvm run 6 –version — run command under a Node version
These are just some of the most common workflows. For more details on usage, check out the NVM README.
Now, let’s cover some bonus topics around managing production systems with NVM. Our bonus deep drive begins in the next sections.
Bonus topics
Setting up NVM and Node is quick, but here are some of the other questions that come up. Let’s explore a few those questions.
Should I use NVM in production?
Developers widely use NVM for local development environments. It enables testing across Node.js versions easily. The same convenience applies on production servers to some extent. NVM makes upgrading Node straightforward without system conflicts. Need a newer Node LTS release? Just use nvm install and migrate.
However, using too many Node versions simultaneously can make production needlessly complex:
- Resource demands grow with each installed version.
- More vectors for maintenance, security issues.
- Operational overhead tracking multiple versions.
Instead, carefully consider how many Node.js variants you actually require:
- For frontend builds, stick to 1-2 LTS releases.
- For older backend services, keep required legacy Node versions.
- Don’t install versions you won’t use.
- Set a default LTS version for stability.
Also, have a transition plan when Node.js versions reach EOL. So, use NVM judiciously. It adds flexibility but not at the cost of unnecessary complexity.
Should I use system packages or NVM packages?
Most Linux distributions ship with a system Node.js package installable via the distro package manager, such as:
- apt install nodejs on Debian/Ubuntu
- dnf install nodejs on CentOS/RHEL/Fedora
But these system packages come with some downsides.
You can only have one Node.js version installed
What if you need to test Node v14 and Node v16 side-by-side? System packages don’t help.
Tend to be dated releases
Debian- or RHEL-based packages are quite stable but older. If you need newer Node LTS releases, you have to add repos or compile Node manually.
Global npm packages conflict across versions
If you upgrade the system Node.js, global npm packages installed for the previous version may break. NVM avoids all these headaches by fully isolating Node versions, including npm packages. It downloads official Node binaries with no system interference or conflicts.
So, while you can use the OS-packaged Node, NVM brings more flexibility. At worst, you can have both NVM and system Node.js installed side-by-side if some legacy app needs the OS default.
Does NVM affect globally installed npm packages?
NVM installs separate npm packages per Node.js version. For instance, Node v14 includes npm v6.x by default, while Node v16 provides the newer npm v8.x. This approach prevents collisions between package dependencies.
When you install a npm package globally via NVM, it goes into the npm folder for that Node.js version only. So, if you now switch Node versions, the globally installed package becomes unavailable. NVM keeps npm packages scoped under each Node release.
In short, remember these rules:
- Global npm packages need reinstalling after Node.js upgrades
- Global installs apply only to the current NVM Node version
- Local installs in node_modules work as usual
Overall, NVM compartmentalizes Node releases, helping avoid system conflicts.
Uninstalling Node.js and NVM
Let’s finish off by covering the removal procedures for Node.js and NVM.
Uninstall specific Node.js versions
If you wish, you can uninstall Node releases installed via NVM using the nvm uninstall command:
nvm uninstall 12.22.12This command removes that entire Node version. Check remaining versions with the nvm ls command afterward.
Uninstall the NVM script
To fully remove NVM itself, follow these steps:
1. First, uninstall all Node.js versions with nvm uninstall –lts and nvm uninstall node.
2. Next, delete the nvm folder with the command shown:
rm -rf ~/.nvm3. Edit the shell profile and remove the lines NVM appended in the Step #2 earlier in this document for source ~/.nvm/nvm.sh etc. This could be ~/.bash_profile, ~/.zshrc, etc.
4. Lastly, close and reopen the terminal or use source ~/.bashrc so changes take effect.
Now, NVM usage should fail with the command not found error message. The system is clean.
Uninstall system Node.js packages
If you installed OS-level Node.js packages via apt or other managers, removing them is also straightforward according to your flavor on Linux.
Debian/Ubuntu
On the Debian/Ubuntu flavors of Linux, the command would be as follows:
udo apt purge nodejs npmRHEL/CentOS
On the Debian/Ubuntu flavors of Linux, the command would be as follows:
sudo dnf erase nodejs npmThis command removes distro-provided Node.js and NPM binaries. Note that this command does not affect NVM packages. So, in summary, while NVM takes seconds to set up, removing takes a few more steps.
Wrap-up for your install of Node.js on Ubuntu 22.04
That wraps up our guide on installing Node.js on Ubuntu 22.04 via ode Version Manager (NVM). Here are some key takeaways:
- NVM lets you effortlessly run multiple Node.js versions.
- Install NVM first, then Node releases as desired.
- Easily switch Node versions across projects.
- Global npm packages apply only per Node version.
- Use NVM to upgrade/downgrade Node releases.
Website owners can now use NVM to start building JavaScript backends and APIs. Your unmanaged VPS server is now Node-ready for your applications. For production use, track of Node support timelines and limit the number of versions. Maintain a transition plan for upgrading Ubuntu releases in future as well.
We hope you enjoyed this step-by-step NVM tutorial! Node.js brings the power of JavaScript to the server, and now you can harness it smoothly. If you want to launch your next Node.js application, Liquid Web offers high-performance managed and unmanaged cloud VPS solutions tailored to Node workloads.
Liquid Web delivers unrivaled performance, so you don’t have to compromise on speed. Whether you’re running mission-critical applications or managing a small website, Liquid Web hosting solutions are designed to meet the ever-growing requirements of your business.
Note on the original publish date: This blog was originally published in May 2020. It has since been updated for accuracy and comprehensiveness.
Chika Ibeneme