Node Version Manager (NVM) is an essential tool for any developer working with Node.js. It allows you to install multiple versions of Node.js and switch between them as needed, ensuring compatibility and ease of development across various projects. This guide will walk you through the process of installing NVM on Amazon Linux 2, a popular choice for cloud-based applications due to its stability, performance, and long-term support.
Prerequisites
Before you begin, ensure you have:
- Access to a terminal or SSH client.
- Sudo privileges on an Amazon Linux 2 instance.
Step 1: Update Your System
Start by updating your Amazon Linux 2 system to ensure all existing packages are up to date. Open your terminal and execute the following command:
sudo yum update -y
Step 2: Install Curl
Curl is required to download the NVM installation script. If it’s not already installed on your system, you can install it by running:
sudo yum install curl -y
Step 3: Install NVM
Once curl is installed, you can install NVM using the curl command to download and execute the installation script from the NVM GitHub repository. As of my last update, you should use the following command (note that you might want to check the NVM GitHub page for the latest installation script):
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
This command downloads the script and executes it. The script clones the NVM repository to ~/.nvm and adds the script to your profile (~/.bash_profile, ~/.zshrc, ~/.profile, or ~/.bashrc) to load NVM into your session.
Step 4: Activate NVM
After installation, you need to either log out and log back in, or you can source your profile file to add NVM to your current session. To do this, run:
source ~/.bash_profile
Replace ~/.bash_profile with the appropriate file if you’re using a different shell.
Step 5: Verify Installation
To verify that NVM has been installed correctly, use the following command:
nvm --version
You should see the version of NVM that was installed, confirming that the installation was successful.
Step 6: Install Node.js
Now that NVM is installed, you can install Node.js. To install the latest version of Node.js, you can use:
nvm install node
To install a specific version of Node.js, replace node with the version number, like so:
nvm install 20.11.0
Step 7: Switching Between Node Versions
With NVM, you can easily switch between Node.js versions depending on your project requirements. To switch to a version you’ve installed, use:
nvm use <version>
Replace <version> with the version number you wish to use.
Step 8: Setting a Default Node Version
To avoid having to switch manually every time you open a new terminal, you can set a default Node version with:
nvm alias default <version>
Conclusion
Congratulations! You’ve successfully installed NVM on Amazon Linux 2 and learned how to manage multiple Node.js versions. This setup will greatly enhance your development workflow, allowing you to work on projects that require different Node.js versions without any hassle.