UbuntuUbuntu Based

How To Install NVM on Ubuntu 24.04 LTS

Install NVM on Ubuntu 24.04

Node Version Manager (NVM) has become an essential tool for developers working with Node.js, allowing seamless management of multiple Node.js versions on a single system. This comprehensive guide provides detailed instructions for installing and configuring NVM on Ubuntu 24.04 LTS, enabling developers to efficiently manage their Node.js environments. The installation process is straightforward when following the proper steps, and the benefits of using NVM for development work are substantial, particularly for those working across multiple projects with varying Node.js version requirements.

Understanding Node Version Manager (NVM)

Node Version Manager (NVM) is a command-line utility specifically designed to manage multiple Node.js versions on a single machine. Unlike traditional installation methods that only allow one active Node.js version at a time, NVM creates isolated environments for each version, enabling developers to switch between them instantly. This tool serves as a critical component in modern web development workflows, especially for teams maintaining projects with different Node.js version dependencies.

NVM functions by creating separate directories for each Node.js version it manages, handling the installation process, and providing simple commands to activate the desired version as needed. This approach eliminates conflicts between Node.js versions and ensures that projects always run in their intended environment. For developers working on both legacy applications and cutting-edge projects simultaneously, this capability proves invaluable for maintaining productivity without constant reinstallation of Node.js.

The advantages of using NVM extend beyond mere version switching. Each Node.js installation managed through NVM maintains its own npm ecosystem, allowing project-specific dependencies to remain isolated from one another. This isolation prevents the common issue of package conflicts that can occur when global packages are shared across projects with different requirements. Furthermore, NVM simplifies testing applications across multiple Node.js versions, an essential task for ensuring compatibility and optimal performance.

NVM’s popularity has grown significantly as modern web development practices have evolved toward microservices and modular architectures, where different components may have varying Node.js version requirements. By providing a simple yet powerful interface for Node.js version management, NVM has become an indispensable tool in the professional developer’s toolkit, particularly for those working in environments where flexibility and precision in development environments are paramount.

Prerequisites for NVM Installation

Before proceeding with NVM installation on Ubuntu 24.04 LTS, certain system requirements and preparatory steps must be addressed to ensure a smooth installation process. These prerequisites establish the foundation for a successful NVM configuration and help avoid common issues that might arise during installation.

A properly functioning Ubuntu 24.04 LTS system forms the basic requirement for this installation process. Users should have SSH access with sudo privileges to execute the necessary commands for installation. This level of access is essential as the installation process involves system-wide changes and package installations that require elevated permissions.

The terminal interface will be the primary tool for NVM installation and usage, so basic familiarity with command-line operations is beneficial. Users should be comfortable navigating directories, executing commands, and editing configuration files through the terminal. This knowledge becomes particularly important when troubleshooting any issues that might arise during the installation process.

Before installation, it’s recommended to update the system package lists to ensure access to the latest versions of dependencies. This can be accomplished with the following command: sudo apt update && sudo apt upgrade -y. This update ensures that all packages are current and compatible with the tools being installed.

Several dependencies are required for NVM to function properly on Ubuntu 24.04 LTS. These include curl or wget for downloading the installation script, and build-essential packages for compiling Node.js from source when necessary. Users should install these dependencies using the command: sudo apt install curl build-essential apt-transport-https ca-certificates gnupg -y.

If Node.js is already installed on the system through other methods such as apt, Snap, or NodeSource PPA, it’s important to consider whether these existing installations might conflict with NVM. While NVM can coexist with other Node.js installations, it’s generally recommended to remove them to avoid potential path conflicts and ensure a clean environment for NVM operation.

Step-by-Step NVM Installation Process

Installing NVM on Ubuntu 24.04 LTS involves a series of well-defined steps that, when followed correctly, result in a properly configured environment for managing multiple Node.js versions. This section provides detailed guidance through each phase of the installation process.

Preparing Your System

Begin by ensuring your system packages are up-to-date to avoid compatibility issues. Execute the following command to update your package lists and upgrade installed packages:

sudo apt update && sudo apt upgrade -y

Next, install the essential dependencies required for NVM installation and operation:

sudo apt install curl build-essential apt-transport-https ca-certificates gnupg -y

These packages provide the necessary tools for downloading the installation script and compiling Node.js versions when needed.

Downloading and Running the Installation Script

The NVM installation script needs to be downloaded from the official GitHub repository and executed. You can use curl to accomplish this with the following command:

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

Note that the version number (v0.40.1 in this example) should be replaced with the latest version available on the NVM GitHub repository for optimal security and feature support. Alternatively, if you prefer using wget instead of curl, you can use:

wget -qO- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bash

The installation script automatically adds the necessary configuration to your shell profile file (~/.bashrc, ~/.zshrc, or similar depending on your shell).

Configuring Your Shell Environment

After the script completes, you need to load NVM into your current shell session. You can either close and reopen your terminal or run the following command to apply the changes immediately:

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 command initializes the NVM environment variables and makes the NVM command available in your current session.

Verifying the Installation

To confirm that NVM has been installed correctly, check the installed version by running:

nvm --version

If you see a version number displayed, such as “0.40.1,” then NVM has been successfully installed and is ready for use. If you receive a “command not found” error, ensure that you’ve properly loaded the NVM script into your current session or try closing and reopening your terminal.

Basic NVM Usage and Commands

After successfully installing NVM on Ubuntu 24.04 LTS, understanding the core commands is essential for effectively managing Node.js versions. These commands form the foundation of NVM’s functionality and enable developers to seamlessly work with multiple Node.js environments.

Installing Node.js Versions

The primary purpose of NVM is to install and manage different Node.js versions. To install the latest available version of Node.js, use the command:

nvm install node

This command downloads and installs the most recent stable release of Node.js. If you need a specific version, you can specify the version number:

nvm install 18

This would install Node.js version 18.x (the latest minor and patch version in the 18.x series). You can also install more precise versions:

nvm install 20.12.0

Each installation includes the corresponding npm version that was released with that Node.js version.

Switching Between Node.js Versions

One of NVM’s most powerful features is the ability to quickly switch between installed Node.js versions. To change to a specific version, use:

nvm use 18

This command activates Node.js version 18.x for your current terminal session. To make a particular version your default across all sessions, use:

nvm alias default 20

This sets Node.js version 20.x as your system-wide default when opening new terminals.

Listing Installed Versions

To see all Node.js versions currently installed through NVM, execute:

nvm list

This displays a comprehensive list of installed versions, with the currently active version marked with an arrow. The command also shows any named aliases you’ve created.

Checking the Current Version

To verify which Node.js version is currently active, use:

nvm current

Alternatively, you can check the Node.js version directly with:

node -v

Both commands confirm which version is currently in use, helping ensure you’re working with the intended environment.

Uninstalling Node.js Versions

When a particular Node.js version is no longer needed, it can be removed to free up disk space:

nvm uninstall 16

This command completely removes the specified Node.js version and its associated npm packages from your NVM directory.

Managing Multiple Node.js Versions

The primary advantage of NVM lies in its ability to efficiently manage multiple Node.js versions on a single system, providing developers with unprecedented flexibility in their workflow. This capability is particularly valuable when working across projects with different Node.js version requirements or when testing applications for compatibility across multiple runtime environments.

Setting Up Project-Specific Node.js Versions

For projects that require specific Node.js versions, NVM offers a convenient way to configure version requirements at the project level. Creating a .nvmrc file in your project’s root directory with a single line containing the desired Node.js version enables automatic version switching. For example, a file containing just 18 would specify Node.js version 18. When entering the project directory, you can run nvm use without any arguments, and NVM will read the .nvmrc file and switch to the specified version automatically.

This approach ensures consistent development environments across team members and prevents issues arising from version mismatches. It also simplifies onboarding processes for new developers joining a project, as they can automatically use the correct Node.js version without manual configuration.

Managing npm Packages Across Versions

Each Node.js installation managed by NVM maintains its own separate npm environment and global packages. This isolation prevents package conflicts between different Node.js versions but requires understanding how to manage global packages effectively. When switching between Node.js versions, packages installed globally in one version are not automatically available in another.

To install a package globally for all Node.js versions, you need to install it in each version separately or use the --reinstall-packages-from option when installing a new Node.js version:

nvm install 20 --reinstall-packages-from=18

This command installs Node.js version 20 and migrates all global packages from version 18, ensuring continuity in your development tools.

Working with LTS and Current Versions

Node.js follows a release schedule that includes both Long-Term Support (LTS) versions and current releases. NVM simplifies working with this release model by providing aliases for LTS versions. To install the latest LTS version, use:

nvm install --lts

This is particularly useful for production environments where stability is prioritized over access to the latest features. For development purposes, you might prefer the current release, which can be installed using nvm install node as mentioned earlier.

Understanding the differences between these version types and when to use each is an important aspect of Node.js version management. LTS versions receive security updates and bug fixes for an extended period, making them suitable for production applications, while current releases provide access to the latest features but with a shorter support window.

Troubleshooting Common NVM Issues

Despite its straightforward installation process, users may encounter certain issues when setting up or using NVM on Ubuntu 24.04 LTS. Understanding these common problems and their solutions helps ensure a smooth experience with NVM and prevents unnecessary frustration during development work.

“Command Not Found” Errors

One of the most frequent issues users face is the “nvm: command not found” error after installation. This typically occurs because the NVM initialization script hasn’t been properly loaded into the current shell session. To resolve this issue, ensure that the NVM configuration has been added to your shell profile file (~/.bashrc, ~/.zshrc, etc.) and that you’ve either restarted your terminal or sourced the file manually:

source ~/.bashrc

If the configuration is missing from your profile file, you can add it manually by including the following lines:

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 ensures that NVM is properly initialized in each new terminal session.

Permission and Path Issues

Permission problems may arise if the NVM directory wasn’t created with the correct ownership or permissions. These issues typically manifest as errors when attempting to install or switch between Node.js versions. Ensure that the NVM directory (usually ~/.nvm) is owned by your user account and has appropriate permissions:

chown -R $(whoami):$(whoami) ~/.nvm
chmod -R 755 ~/.nvm

Path-related issues can occur when multiple Node.js installations exist on the system, leading to conflicts between NVM-managed versions and those installed through other methods. To avoid such conflicts, it’s recommended to remove any Node.js installations that weren’t installed through NVM, or at least ensure that the NVM paths take precedence in your PATH environment variable.

Installation Failure for Specific Node.js Versions

Sometimes, installing specific Node.js versions through NVM may fail due to missing dependencies or network issues. When encountering installation failures, first check your internet connection and proxy settings if applicable. Then, ensure all required build dependencies are installed:

sudo apt install build-essential libssl-dev

These packages provide the necessary tools for compiling Node.js from source, which NVM may need to do for certain versions. If issues persist, try installing with verbose output to identify specific errors:

nvm install 16 --verbose

This provides detailed information about the installation process, helping pinpoint where failures occur.

Congratulations! You have successfully installed NVM. Thanks for using this tutorial for installing the NVM (Node Version Manager) on Ubuntu 24.04 LTS system. For additional help or useful information, we recommend you check the official NVM website.

VPS Manage Service Offer
If you don’t have time to do all of this stuff, or if this is not your area of expertise, we offer a service to do “VPS Manage Service Offer”, starting from $10 (Paypal payment). Please contact us to get the best deal!

r00t

r00t is an experienced Linux enthusiast and technical writer with a passion for open-source software. With years of hands-on experience in various Linux distributions, r00t has developed a deep understanding of the Linux ecosystem and its powerful tools. He holds certifications in SCE and has contributed to several open-source projects. r00t is dedicated to sharing her knowledge and expertise through well-researched and informative articles, helping others navigate the world of Linux with confidence.
Back to top button