How to install NVM on Amazon Linux 2023

Nodejs is already popular among developers for creating server and client-side Javascript command line tools and applications. So, those planning to use Node on Amazon Linux 2023 would require NVM to manage various versions of Nodejs & NPM efficiently. That’s why in this tutorial we discuss the commands we require to install NVM – Node Version Manager on Amazon Linux 2023 AWS EC2 instance.

Well, NVM is an open-source CLI tool designed for developers working on multiple projects with different Node.js versions on a single machine or for testing app compatibility across different Node versions.

Key Features of NVM are the management & installation of multiple Node.js Versions; it can be used to specify global and local Node.js versions; able to manage NPM versions as well, and offers easy Node & NPM version switching.

What do we need for installation?

  • Amazon Linux 2023 (AMI) Instance
  • SSH to access the command terminal
  • A user account with sudo privileges

Connect to Your Instance

After creating the Amazon Linux instance on EC2 or LightSail, we will have the option to connect our created instance using the browser-based SSH client. Users can use that. Alternatively, we can also use the local terminal or SSH client to connect the instance with the help of a Private Key available to download on the Instance page or EC2 or LightSail Dashboard.

System and Software Updates

After having the command terminal access of the remote Amazon Linux 2023, the first thing we should do is run the system update command. It will make sure our system is up to date from a security point of view and also the existing packages are in their latest versions.

sudo dnf update

Installing NVM on Amazon Linux 2023

Well, installing Node.js will not configure NVM automatically like NPM to use, therefore we manually need to install Node Version Manager on Amazon Linux 2023. Also, unlike Node, NVM is not present to install using the default package manager – DNF, hence we need to download a script from its GitHub page. Here is the command to run:

curl https://raw.githubusercontent.com/creationix/nvm/master/install.sh | bash
Setup NVM on Amazon Linux using Script

Once the installation is completed by the script, enable the NVM for your current terminal session by sourcing the profile file:

source ~/.bashrc

Check Version

To ensure that we have the latest version of the NVM installed on our Amazon Linux system, we can use the given command:

nvm --version

To know about the available options use: nvm --help

Check NVM version

Environmental Variables Set by NVM

Understanding the environmental variables set by NVM can help you manage Node.js versions more effectively:

  • NVM_DIR: Points to the directory where NVM is installed.
  • NVM_BIN: Indicates the location where Node, NPM, and globally installed packages reside.
  • NVM_INC: Specifies the directory for Node’s including files, useful for building C/C++ add-ons.
  • NVM_CD_FLAGS: Contains flags to maintain compatibility with different shell environments like zsh.
  • NVM_RC_VERSION: Reflects the Node version specified in the .nvmrc file, if present.

Installing Node.js Using NVM

Now, if you don’t have Nodejs already on your system or want to install some specific old or new version of Node using NVM then here is the command to do that…

For the latest Node LTS version

Those who don’t want to be specific and looking to install the latest LTS version of Nodejs can run the given command on their Amazon Linux:

nvm install --lts
Use NVM install LTS Nodejs version

However, if you want to be more specific then need to mention the version number of the Node.

Syntax

nvm install <version-number>

For instance: To fulfill the requirement of your project that needs Node 18.16.0 version, here is the command to get it…

nvm install 18.16.0
install a specific version of Node.js using NVM

 

Switching Between Node Versions

We have already discussed how we can download and install different versions of Node along with NPM on Amazon Linux 2023, but out of multiple versions how to choose one to use for the current terminal session.

  • Select the Node.js Version for the current terminal session: To switch between installed Node.js versions, use the command syntax:
nvm use version-number

For example, we want to switch to Node version 18.16.0:

nvm use 18.16.0

After switching versions, to verify it:

node --version

By running the above command, the current version of Node will be 18.x, however only for our current terminal session. It will not change the default version of the system.

  • Set a Default Node.js Version using NVM: Now, if you want to use or change the default version of Nodejs on Amazon Linux 2023 using the NVM – Node version manager, the command syntax will be:
nvm alias default version-number

For example, setting version 18.16.0 as the default:

nvm alias default 18.16.0

List All Installed Node.js Versions

To view all versions of Node.js installed on your system, use:

nvm ls

Remove Unneeded Node.js Versions

If for any reason, some version of Node is not required anymore then to uninstall it using the NVM, the command will be:

nvm uninstall version

For example, to uninstall Node version 18.16.0:

nvm uninstall 18.16.0

Creating and Using Aliases for Node.js Versions

Create a New Alias

If you are using some version of Node frequently but remembering its exact version number details is difficult, in such a situation we can create an alias for easy Node version management.

Syntax:

nvm alias <alias_name> <version_number>  

For example, we want to call Node version 18.16.0 by using an easy-to-remember name (a.k.a alias) “for-testing“. Then for that the command we be like this:

nvm alias for-testing 18.16.0

Use an Existing Alias

After setting Alias, whenever, we want to switch to a different version using an alias the command syntax will be:

nvm use <alias_name>

For example, we used a “for-testing” alias for Node version 18.16.0, so to switch to that using an alias, the command will be:

nvm use for-testing

Conclusion

So, far we have not only learned the way to install NVM on Amazon Linux 2023 but even how to use it for managing different versions of Node. NVM makes the management of Node quite easy and even the installation of different versions, hence in modern web development we cannot ignore the importance of it for leveraging the power and flexibility of Amazon Linux 2023.

Other Articles:

 

Leave a Comment

This site uses Akismet to reduce spam. Learn how your comment data is processed.