nvmInstalling Node.js Using Node Version Manager (NVM)

thowfick official
3 min read3 days ago

Installing Node.js Using Node Version Manager (NVM)

When working with Node.js, flexibility is crucial, especially when managing multiple versions for different projects. One of the best ways to achieve this is by using NVM (Node Version Manager). NVM allows you to install and maintain multiple versions of Node.js, along with their associated Node packages, on your system.

Installing NVM

To install NVM on your Ubuntu machine, visit the NVM GitHub page and copy the latest installation command. Before executing, review the script to ensure it’s safe:

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

Once reviewed, execute the command by appending | bash:

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

This installs NVM for your user account. To apply the changes, source your .bashrc file:

source ~/.bashrc

Checking Available Node.js Versions

To view available Node.js versions, run:

nvm list-remote

Example Output:

v18.10.0
v18.11.0
v18.12.0 (LTS: Hydrogen)
v18.12.1 (LTS: Hydrogen)
v18.13.0 (LTS: Hydrogen)
v18.14.0 (LTS: Hydrogen)
v18.14.1 (LTS: Hydrogen)
v18.14.2 (LTS: Hydrogen)
v18.15.0 (LTS: Hydrogen)
v18.16.0 (LTS: Hydrogen)
v18.16.1 (LTS: Hydrogen)
v18.17.0 (LTS: Hydrogen)
v18.17.1 (LTS: Hydrogen)
v18.18.0 (LTS: Hydrogen)
v18.18.1 (LTS: Hydrogen)
v18.18.2 (LTS: Hydrogen)
v18.19.0 (LTS: Hydrogen)
v18.19.1 (LTS: Hydrogen)
v18.20.0 (LTS: Hydrogen)
v18.20.1 (LTS: Hydrogen)
v18.20.2 (LTS: Hydrogen)
v18.20.3 (LTS: Hydrogen)
v18.20.4 (LTS: Hydrogen)
v18.20.5 (LTS: Hydrogen)
v18.20.6 (LTS: Hydrogen)
v18.20.7 (Latest LTS: Hydrogen)
v19.0.0
v19.0.1
v19.1.0
v19.2.0
v19.3.0
v19.4.0
v19.5.0
v19.6.0
v19.6.1
v19.7.0
v19.8.0
v19.8.1
v19.9.0
v20.0.0
v20.1.0
v20.2.0
v20.3.0
v20.3.1
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 (LTS: Iron)
v20.12.0 (LTS: Iron)
v20.12.1 (LTS: Iron)
v20.12.2 (LTS: Iron)
v20.13.0 (LTS: Iron)
v20.13.1 (LTS: Iron)
v20.14.0 (LTS: Iron)
v20.15.0 (LTS: Iron)
v20.15.1 (LTS: Iron)
v20.16.0 (LTS: Iron)
v20.17.0 (LTS: Iron)
v20.18.0 (LTS: Iron)
v20.18.1 (LTS: Iron)
v20.18.2 (LTS: Iron)
v20.18.3 (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
v21.7.0
v21.7.1
v21.7.2
v21.7.3
v22.0.0
v22.1.0
v22.2.0
v22.3.0
v22.4.0
v22.4.1
v22.5.0
v22.5.1
v22.6.0
v22.7.0
v22.8.0
v22.9.0
v22.10.0
v22.11.0 (LTS: Jod)
v22.12.0 (LTS: Jod)
v22.13.0 (LTS: Jod)
v22.13.1 (LTS: Jod)
v22.14.0 (Latest LTS: Jod)
v23.0.0
v23.1.0
v23.2.0
v23.3.0
v23.4.0
v23.5.0
v23.6.0
v23.6.1
v23.7.0
v23.8.0
v23.9.0

Installing a Specific Node.js Version

To install a specific version, such as v20.18.0, use:

nvm install v20.18.0

Check installed versions with:

nvm list

Example Output:

->     v20.18.0
default -> v20.18.0
node -> stable (-> v20.18.0) (default)
lts/iron -> v20.18.0

To switch between versions:

nvm use v20.18.0

Method 2: To install an LTS release using an alias(LTS release Name):

nvm install lts/Iron

(or)

nvm install lts/Jod

(or)

nvm install lts/Hydrogen

Example Output:

Downloading and installing node v20.18.3...
Downloading https://nodejs.org/dist/v20.18.3/node-v20.18.3-linux-x64.tar.xz...
####################################################################################################################################################################################### 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v20.18.3 (npm v10.8.2)

Verify installation:

node -v

Example Output:

v20.18.3

Automating the Installation Process

To automate the installation of NVM and the latest LTS version of Node.js, use the following script:

1. Create a Script File

Save the following script as install_node_nvm.sh:

#!/bin/bash

# Installing NVM
echo "Installing NVM..."
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
source ~/.bashrc

echo "NVM installed successfully!"

# Checking available Node.js versions
echo "Fetching available Node.js versions..."
nvm list-remote

# Installing the latest LTS version of Node.js
NODE_VERSION=$(nvm list-remote | grep -i 'Latest LTS' | tail -1 | awk '{print $1}')
echo "Installing the latest LTS version of Node.js: $NODE_VERSION..."
nvm install $NODE_VERSION
nvm use $NODE_VERSION

echo "Node.js $NODE_VERSION installed successfully!"

echo "Automation script execution completed!"

2. Make the Script Executable

Run the following command to grant execute permissions:

chmod +x install_node_nvm.sh

3. Execute the Script

Run the script to install NVM and the latest LTS version of Node.js:

./install_node_nvm.sh

Removing Node.js

If you have a system-installed version and want to remove it, run:

sudo apt remove nodejs

For a complete removal, including config files:

sudo apt purge nodejs

To remove a version installed via NVM:

nvm current
nvm uninstall node_version

Example Output:

Uninstalled node node_version

If removing the currently active version, first deactivate NVM:

nvm deactivate

Then uninstall the desired version as shown above. This removes all associated files for that Node.js version.

By using NVM, you can efficiently manage multiple versions of Node.js, ensuring flexibility and compatibility across different projects. Happy coding!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

No responses yet

Write a response