How to install node and npm on amazon linux 2 – AWS Ec2?

Follow the simple commands for installing Node.js 16.x and NPM 9.x on Amazon Linux 2 running on AWS Ec2 servers for developing web applications.

Node.js is a popular server-side Javascript runtime environment used by thousands of developers around the globe. It is built on Chrome’s V8 JavaScript engine making it possible to build scalable and efficient web applications.

While programming and creating web applications in Node.js developers need the support of common libraries and third-party packages and to easily download them there is a dedicated package manager called NPM – Node Package Manager. It also provides a way to manage dependencies for a project, making it easy to ensure that all required libraries are installed and up to date.

npm is typically installed along with Node.js and is used by running commands in the terminal to install, update, and remove packages. Even developers can also use the npm registry to publish their own packages for others to use.

Steps to install Nodejs and NPM on Amazon Linux 2

The steps given here will be the same for CentOS 7 as well, so if you are using it then can install the Node and NPM using the commands given below:

1. Update Amazon Linux

Start with the system update command to install the available security and application package updates. This will also refresh the YUM package manager cache built for various added repositories.

sudo yum update

2. Add Node.js 16.x LTS repository

As we know Amazon Linux 2 is based on CentOS 7 and the LTS version of Node.js supported by it is 16.x. We tried to install Node 18.x LTS but it gave a few dependencies errors which are not available to install such as libc.so.6(GLIBC_2.28)(64bit) using the base repository of Amazon Linux. Therefore, here we are going for the Nodejs-16.19.1 version.

curl -fsSL https://rpm.nodesource.com/setup_16.x | sudo bash -

Again run the system update command:

sudo yum update

3. Install Nodejs and NPM for Amazon Linux 2

After adding the repository, we can install the Nodejs and along with that NPM will also get installed automatically on our Amazon Linux. Hence, follow the given command and run it in your terminal.

sudo yum install -y nodejs

4. Check the installed versions

As the process of installing Node and NPM is completed, we can use the terminal to check their versions, this will also confirm they are working on our system without any issues.

node -v
npm -v
command to check node and npm versions

5. Update the NPM version

The version of the NPM by default will be 8.19.3, in case you want to upgrade it to the latest available or compatible one with the current system-installed Nodejs version then run:

sudo npm install -g npm@latest

6. Uninstallation

If the version of Nodejs you have installed is not working for your project and want to uninstall it. Then, here is the command to remove Node.js and NPM completely from Amazon Linux 2.

sudo yum remove nodejs
sudo rm -r /etc/yum.repos.d/nodesource*.repo
sudo yum clean all

Other related articles:

Leave a Comment

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