How to install Nodejs on Amazon Linux 2

In this article, I will tell you how to install Nodejs on Amazon Linux 2.

First, install NVM using CURL

curl -o- | bash

Then run this command

export NVM_DIR="$([ -z "${XDG_CONFIG_HOME-}" ] && printf %s "${HOME}/.nvm" || printf %s "${XDG_CONFIG_HOME}/nvm")"
[ -s "$NVM_DIR/" ] && \. "$NVM_DIR/" # This loads nvm

To verify that nvm has been installed, do run this command

command -v nvm

After that, run this command to install Node 11 using NVM

nvm install 11

So you install NodeJS 11 on your EC2 instance. Now if you want to install a different version like NodeJs 12 then please run this command in your terminal

nvm install 12

This command installs Nodejs on your EC2 instance, but it’s not the active version. To switch to NodeJs 12, please run the following command.

nvm use 12 

Now you have activated Node 12 on your instance. To make Node 12 the default version please run the following command

nvm alias default 12

This command will make 12 as the default Node version for this instance. Even if you stop and start your ec2 instance, Node 12 will be the default version on your machine. To check the Node version run the following command.

node -v

if you see the output like this “v12.20.0” then it means NodeJs installed on your EC2 instance successfully and your current node version is 12.20

Hope this will help you to get started with NodeJs. Feel free to watch my screen recording on youtube below.

If you are using Amazon Linux 2, then this video might help you

Spread the love