Amazon.co.uk Widgets

Log in

X
By Ryan Dahl, MIT, https://commons.wikimedia.org/w/index.php?curid=26936716
By Ryan Dahl, (MIT)

Installing Node.js on Ubuntu can be a convoluted process, particularly when it comes to managing multiple versions. Node Version Manager (NVM) provides a solution that allows developers to deal with versioning effectively. This guide offers a detailed, step-by-step walkthrough on installing Node.js via NVM on Ubuntu 22.04 LTS, ensuring that you maintain the latest Long Term Support (LTS) version of this essential runtime environment vital for backend services and real-time applications.

Node.js is an open-source JavaScript runtime that executes JavaScript code outside a web browser, predominantly used for building scalable network applications. However, various projects might require different versions. NVM addresses this issue seamlessly, allowing developers to switch between Node.js versions as required. Understanding the newer commands and installation steps is imperative in ensuring compatibility and maintaining best practices in development environments. Those looking to dive deeper into NVM can refer to its GitHub repository.

TL:DR – The installation of Node.js on Ubuntu via NVM includes prerequisites, step-by-step installation guidance, how to manage various Node.js versions effectively, and additional insights into best practices for development environments. Using NVM is highly recommended for developers who regularly update or switch node versions to maintain optimised functionality across projects. The version of Node installed by Ubuntu's package manager is woefully outdated so don't assume that will be ok.

Understanding Node Version Manager (NVM)

Node Version Manager, or NVM, is a widely-used tool that simplifies managing Node.js versions. It allows you to install different versions of Node.js, switch between those versions, and even set a default version to ensure your applications run reliably across different environments. The utility is especially popular among developers who work on multiple projects with differing requirements, as it eliminates many of the complications associated with version conflicts.

Initially created by Tim Caswell in 2010, NVM has evolved into an essential tool for modern Node.js development. The advantages of using NVM is that it not only simplifies version management but also aids testing across various Node.js versions, thus allowing developers to ensure their applications are compatible. With NVM, developers can have a variety of Node.js versions at their fingertips, dramatically decreasing downtime and enhancing productivity.

It is important to note, however, that NVM is not designed for production use. It primarily caters to development needs, allowing environments to shift fluidly between versions without the overhead of cumbersome manual installs. This article will guide you through the installation process on Ubuntu 22.04, focusing on installing the LTS version to ensure you are working with stable and supported features.

Prerequisites for installing NVM

Before installation of NVM and subsequently Node.js, there are essential prerequisites that must be attended to. First, ensure that you are operating on an Ubuntu 22.04 LTS system.

Secondly, it’s crucial to have several necessary dependencies installed beforehand. Using the terminal, update your package repositories and install utilities that allow you to download NVM easily. You will require commands such as curl or wget for downloading files and gpg for verifying these files. Installing those dependencies is a straightforward process:

sudo apt update && sudo apt install -y curl wget gnupg

Step-by-step installation of NVM

With the prerequisites in place, the next phase involves the actual installation of Node Version Manager. Start by executing the following command in your terminal, which retrieves and executes the install script from the NVM GitHub repository:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash

After running the script, it’s necessary to load NVM into your shell session. This can be done by adding the following lines to your profile script, typically found in ~/.bashrc or ~/.profile:

export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"

Once you have added these lines, ensure to source the profile script to apply the changes:

source ~/.bashrc

Verifying NVM installation

It's essential to verify the successful installation of NVM. You can accomplish this by running the command:

nvm --version

If the command returns a version number, pat yourself on the back; you have successfully installed NVM. If not, revisit the steps to ensure that all commands were executed correctly.

Installing Node.js with NVM

Armed with NVM, the next step is to install Node.js. You can choose to install either the latest LTS version or a specific version required for your project. In practice, using the Xcode style is quite popular, where you can simply type:

nvm install --lts

This command will automatically select the latest Long-Term Support version, ensuring that you’re developing with the most stable version available. Confirm the installation by running:

node -v

This command should return the version number of Node.js you just installed, which serves as a further validation of the installation process.

Managing Node.js versions with NVM

One of NVM's most beneficial features is that it allows you to manage multiple Node.js installations. To list installed versions, simply use:

nvm ls

Should you want to install a different version of Node.js, direct NVM with:

nvm install [version]

For example, to install Node.js version 16, you would use:

nvm install 16

Switching Node.js versions

Switching between Node.js versions is as simple as pie. Use the following command to activate any installed version:

nvm use [version]

Should you wish to set a default version that you want to activate when a new terminal session opens, the command is just as straightforward:

nvm alias default [version]

This can save time during development, as you won't need to repeatedly switch between versions manually.

Uninstalling Node.js versions

At some point, you may no longer need specific versions of Node.js, and NVM includes a handy command for this very occasion:

nvm uninstall [version]

Just ensure the version you wish to uninstall isn't the currently active version; otherwise, you may run into issues. If it is, switch to a different version before executing the command.

Bonus considerations

While using NVM is advantageous for development, some may question whether it's suitable for production. Generally, it's not recommended to use NVM or similar tools for production environments due to performance and security implications. Instead, consider using a method that complies with your server's stability requirements.

Additionally, it's crucial to understand the difference between Node.js installed through NVM and system packages. NVM installs Node in your user directory, which can help avoid permission issues that come with global system installations. This separated environment is an invaluable feature that simplifies project dependency management.

Lastly, NVM manages global packages within each Node.js version. This means that if you have dependencies required for multiple projects, installing those global packages via NVM is accomplished easily without conflict.

Conclusion

In summary, installing Node.js on Ubuntu via NVM streamlines version handling, providing a robust solution for developers tackling diverse projects. By following the outlined steps, you can ensure an LTS version is installed correctly while being able to manage multiple Node.js installations seamlessly. Using NVM not only enhances productivity by reducing manual version switching but also prevents the common pitfalls associated with package conflicts.

For those looking to maintain best practices in development environments, making NVM an integral part of your workflow is essential. If you haven't yet implemented NVM in your Node.js projects, now may be the perfect time to do so. By taking control over your Node.js versions, you ultimately ensure your applications run effectively and with minimal hassle.

Alternative titles

  • How to manage Node.js installations on Ubuntu with NVM
  • A step-by-step guide to installing Node.js using NVM on Ubuntu 22.04
  • Mastering Node.js version management on Ubuntu with NVM
  • Installing Node.js and managing versions with NVM on Ubuntu LTS
  • Streamline your Node.js development on Ubuntu with NVM

Licences, trademarks, source code licences and attributions

Licences, trademarks, source code licences and attributions

Multizone and this site is not affiliated with or endorsed by The Joomla! Project™. Any products and services provided through this site are not supported or warrantied by The Joomla! Project or Open Source Matters, Inc. Use of the Joomla!® name, symbol, logo and related trademarks is permitted under a limited licence granted by Open Source Matters, Inc. 928uk® is a trademark of Multizone Limited, registered in the UK. AdMob™, AdSense™, AdWords™, Android™, Chrome OS™, Chromebook™, Chrome™, DART™, Flutter™, Firebase™, Firestore™, Fuchsia™, Gmail™, Google Maps™, Google Pixel™, Google Play™, Pixelbook Go™, and Pixel™ and other trademarks listed at the Google Brand Resource center are trademarks of Google LLC and this site is not endorsed by or affiliated with Google in any way. Apple and the Apple logo are trademarks of Apple Inc., registered in the U.S. and other countries. App Store is a service mark of Apple Inc. The OSI logo trademark is the trademark of Open Source Initiative. Microsoft, CoPilot, Microsoft 365, Microsoft Teams, and Windows are trademarks of the Microsoft group of companies. ROKU, the ROKU Logo, STREAMING STICK, ROKU TV, ROKU POWERED, the ROKU POWERED Logo, ROKU READY, the ROKU READY Logo, the purple fabric tag design,and the purple d-pad design are trademarks and/or registered trademarks of Roku, Inc. in the UK, the United States and other countries. UNIX® and the X® logo are registered trademarks of The Open Group. Any other product or company names may be trademarks™ or registered® trademarks of their respective holders. Use of these trademarks in articles here does not apply affiliation or endorsement by any of them.

Where the source code is published here on ezone.co.uk or on our GitHub by Angus Fox, Multizone Limited it is licenced according to the open source practice for the project concerned.

BSD 3-Clause "New" or "Revised" Licence
Original source code for mobile apps are licenced using the same licence as the one used by "The Flutter Authors". This Licence, the BSD 3-Clause "New" or "Revised" Licence (bsd-3-clause) is a permissive licence with a clause that prohibits others from using the name of the project or its contributors to promote derived products without written consent.
GNU General Public Licence v2.0 or later
Original source code for Joomla! published here on ezone.co.uk by Angus Fox, Multizone Limited is licenced using the same licence as the one used by Joomla!. This Licence, the GNU General Public Licence Version 2 or later (gpl-2.0) is the most widely used free software licence and has a strong copyleft requirement. When distributing derived works, the source code of the work must be made available under the same licence.

You can use any code you find here, just respect the licences and dont use the name of this site or our company to promote derived products without written consent. I mean, why would you? You're not us!

Amazon Associate
As an Amazon Associate we earn from qualifying purchases.
Logo
Our Logo Image is by Freepik. We chose it because its an M and also the letter A twice - and that represents us.