Friday, June 14, 2024
HomeHow toHow to Install and Use NVM (Node Version Manager) on Ubuntu 22.04

How to Install and Use NVM (Node Version Manager) on Ubuntu 22.04

NVM, also called “Node Version Manager” is used for installing and managing multiple Node.js versions in Linux. It provides a command line utility that helps developers to install multiple Node versions and switch between multiple versions as per their project requirements. NVM is very helpful if you are working on multiple projects that require different Node versions.

In this post, we will show you how to install and use NVM to manage Node.js on Ubuntu 22.04.

Prerequisites

  • A server running Ubuntu 22.04.
  • A root password is configured on the server.

Install NVM

By default, NVM is not included in the Ubuntu default repository. So you will need to install it from the script.

First, install the CURL and Gnupg2 with the following command:

apt-get install curl gnupg2 -y

Next, run the following command to download and run the NVM installation script:

curl https://raw.githubusercontent.com/nvm-sh/nvm/master/install.sh | bash

The above command will install NVM and makes all the required environment settings in the .bashrc file.

=> `nvm` Nodes), you can remove them from the system Node as follows:

     $ nvm use system
     $ npm uninstall -g a_module

=> Close and reopen your terminal to start using nvm or run the following to use it now:

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

Now, activate all settings using the following command:

source ~/.bashrc

Now, verify the NVM version using the following command:

nvm --version

You should see the following output:

0.39.1

Install Node.js with NVM

NVM is now installed in your system. You can now use the NVM command line to install any Node.js version to your system.

For example, to install the latest version of Node.js, run the following command:

nvm install node

You should see the following output:

Downloading and installing node v18.9.1...
Downloading https://nodejs.org/dist/v18.9.1/node-v18.9.1-linux-x64.tar.xz...
####################################################################################################################################### 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v18.9.1 (npm v8.19.1)
Creating default alias: default -> node (-> v18.9.1)

To verify the installed version of Node.js, run the following command:

node --version

You should see the following output:

v18.9.1

If you want to install the latest stable version of Node.js, run the following command:

nvm install node --lts

You should see the following output:

v18.9.1 is already installed.
Now using node v18.9.1 (npm v8.19.1)

To install the specific Node.js version (12.17.0), run the following command:

nvm install 12.17.0

You should see the following output:

Downloading and installing node v12.17.0...
Downloading https://nodejs.org/dist/v12.17.0/node-v12.17.0-linux-x64.tar.xz...
######################################################################################################################################## 100.0%
Computing checksum with sha256sum
Checksums matched!
Now using node v12.17.0 (npm v6.14.4)

Now, verify the current Node.js version using the following command:

node --version

You should see the following output:

v12.17.0

Use NVM to Manage Node.js Versions

To list all installed Node.js versions in your system, run the following command:

nvm ls

You should see the following output:

->     v12.17.0
        v18.9.1
         system
default -> node (-> v18.9.1)
iojs -> N/A (default)
unstable -> N/A (default)
node -> stable (-> v18.9.1) (default)
stable -> 18.9 (-> v18.9.1) (default)
lts/* -> lts/gallium (-> N/A)
lts/argon -> v4.9.1 (-> N/A)
lts/boron -> v6.17.1 (-> N/A)
lts/carbon -> v8.17.0 (-> N/A)
lts/dubnium -> v10.24.1 (-> N/A)
lts/erbium -> v12.22.12 (-> N/A)
lts/fermium -> v14.20.1 (-> N/A)
lts/gallium -> v16.17.1 (-> N/A)

You can find all available Node.js versions using the following command:

nvm ls-remote

To set your default Node.js version to 12.17.0, run the following command:

nvm use 12.17.0

You should see the following output:

Now using node v12.17.0 (npm v6.14.4)

To find the default version for the current user, run the following command:

nvm run default --version

You should see the following output:

Running node v16.9.0 (npm v7.21.1)
v16.9.0

You can also run a Node application with a specific Node.js version using the following command:

nvm run v12.17.0 app.js

To remove a specific Node.js version from your system, run the following command:

nvm uninstall v12.17.0

Conclusion

In this guide, we explained how to install and use NVM on Ubuntu 22.04. You can now use the NVM to manage multiple Mode.js versions and switch between them. I hope you can now run your application with any Node.js version.

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here


Most Popular