DEV Community

Cover image for How to Install and Configure Node.js on EC2 Instance Amazon Linux 2
Mohammad Abu Mattar
Mohammad Abu Mattar

Posted on • Edited on

How to Install and Configure Node.js on EC2 Instance Amazon Linux 2

Introduction

Node.js does not exist in the default Amazon Linux 2 repository. So, we need to add the Node.js repository to the system. In this post, we will learn how to install and configure Node.js on EC2 Instance Amazon Linux 2.

Prerequisites

  • AWS Account
  • EC2 Instance Amazon Linux 2
  • SSH Client

Step 1: Update the System Packages and Install Dependencies Packages

First, we need to update the system packages and install dependencies packages.

# Update the system packages
sudo yum update -y

# Install dependencies packages
sudo yum install gcc-c++ make -y
Enter fullscreen mode Exit fullscreen mode

Step 2: Install Node.js

First, we need to install Node.js on our EC2 Instance. To do that, we need to add the Node.js repository to the system. To add the Node.js repository, we need to run the following command:

# Install Node.js repository 14.x
curl -sL https://rpm.nodesource.com/setup_14.x | sudo bash -

# Install Node.js repository 16.x
curl -sL https://rpm.nodesource.com/setup_16.x | sudo bash -

# Install Node.js repository 17.x
curl -sL https://rpm.nodesource.com/setup_17.x | sudo bash -
Enter fullscreen mode Exit fullscreen mode

Step 3: Install Node.js

After choosing the Node.js version, we need to install Node.js on our EC2 Instance. To do that, we need to run the following command:

# update the system
sudo yum update -y

# Install Node.js
sudo yum install nodejs -y
Enter fullscreen mode Exit fullscreen mode

Step 4: Check Node.js Version

After installing Node.js, we need to check the Node.js version. To do that, we need to run the following command:

node -v
Enter fullscreen mode Exit fullscreen mode

Output depends on the Node.js version that you choose.

# Node.js 14.x
v14.21.3

# Node.js 16.x
v16.19.1

# Node.js 17.x
v17.9.0
Enter fullscreen mode Exit fullscreen mode

Conclusion

In this post, we learned how to install and configure Node.js on EC2 Instance Amazon Linux 2. We learned how to add the Node.js repository to the system and install Node.js on our EC2 Instance.

References

Top comments (4)

Collapse
 
octaneinteractive profile image
Wayne Smallman • Edited

Hi, I got errors when installing v18:

Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
Resolving Dependencies
--> Running transaction check
---> Package nodejs.x86_64 2:18.14.2-1nodesource will be installed
--> Processing Dependency: libc.so.6(GLIBC_2.28)(64bit) for package: 2:nodejs-18.14.2-1nodesource.x86_64
--> Processing Dependency: libm.so.6(GLIBC_2.27)(64bit) for package: 2:nodejs-18.14.2-1nodesource.x86_64
--> Finished Dependency Resolution
Error: Package: 2:nodejs-18.14.2-1nodesource.x86_64 (nodesource)
           Requires: libc.so.6(GLIBC_2.28)(64bit)
Error: Package: 2:nodejs-18.14.2-1nodesource.x86_64 (nodesource)
           Requires: libm.so.6(GLIBC_2.27)(64bit)
 You could try using --skip-broken to work around the problem
 You could try running: rpm -Va --nofiles --nodigest
Enter fullscreen mode Exit fullscreen mode

I then tried the other versions and got the same error.

Collapse
 
mkabumattar profile image
Mohammad Abu Mattar • Edited

Hi there! It looks like you're encountering some errors when trying to install nodejs v18. It seems that the Amazon Linux 2 AMI repository does not have the updated version of GLIBC 2.27 or higher that is required by nodejs v18.

To resolve this issue, you can consider installing nodejs v17 or an older version. You will need to remove the v18 package from your repositories and clean the cache using the following command:

sudo rm -rf /etc/yum.repos.d/nodesource*.repo && sudo sudo rm -rf /var/cache/yum && sudo yum clean all

# clean cache
sudo rm -rf /var/cache/yum
Enter fullscreen mode Exit fullscreen mode

After that, you can install one of the older versions of nodejs.

Image description

Alternatively, you can use a different AMI that has the required dependencies for nodejs v18. If that's not an option, you can use Docker to create a container with the required dependencies and install nodejs v18 within that container.

I hope this helps! Let me know if you have any further questions.

Collapse
 
octaneinteractive profile image
Wayne Smallman

Hi Mohammad, I ran into other problems with Amazon Linux 2 and had to switch to Ubuntu 22 instead, which is working as expected.

Collapse
 
alexandernem profile image
Alexander Nemsadze

Same problem, thanks for solution.