In the rapidly evolving landscape of web development, Node.js has emerged as a cornerstone technology, powering a wide array of applications with its efficient, event-driven, non-blocking I/O model. Amazon Linux 2, a highly performant and secure Linux environment provided by Amazon Web Services (AWS), is a preferred choice for running server applications, including those built with Node.js.

Advertisement

To install the specific Node.js version, Visit our tutorial Install Specific Node.Js Version with NVM.

This guide aims to walk you through the process of installing the latest version of Node.js on Amazon Linux 2 using the Yum repository.

Step 1: Configure Yum Repository

First of all, You need to enable the node.js yum repository in your system provided by the Node.js official website. You also need development tools to build native add-ons to be installed on your system.

For Latest Release:-

sudo yum install -y gcc-c++ make 
curl -sL https://rpm.nodesource.com/setup_21.x | sudo -E bash - 

For Stable Release:-

sudo yum install -y gcc-c++ make 
curl -sL https://rpm.nodesource.com/setup_20.x | sudo -E bash - 

Step 2: Install Node.js on Amazon Linux 2

After adding a yum repository to your system let’s install the Node.js package. NPM will also be installed with node.js. This command will also install many other dependent packages on your system.

sudo yum install -y nodejs 

Step 3: Check Node/NPM Versions

After installation checks the installed version of Node.js. You can find more details about the current version on node.js official website.

node -v  

v20.11.0

Also, check the version of npm.

npm -v  

8.19.3

Step 4: Create Demo Web Server (Optional)

This is an optional step. If you want to test your node.js install. Let’s create a web server with the “Welcome Node.js” text. Create a file demo_server.js

vim demo_server.js 

and add the following content


// Import the HTTP module to create an HTTP server.
const http = require('http');

// Create an HTTP server.
http.createServer(function (req, res) {
  // Set the response header with a status code of 200 (OK) and set the content type to text/plain.
  res.writeHead(200, {'Content-Type': 'text/plain'});
  
  // Send a response body with the text 'Welcome Node.js' and end the response.
  res.end('Welcome Node.js');
  
// The server listens on port 3000 and IP address 127.0.0.1.
}).listen(3000, "127.0.0.1");

// Log a message to the console indicating where the server is running.
console.log('Server running at http://127.0.0.1:3000/');

Now start the web server using the command.

node --inspect demo_server.js 

Debugger listening on ws://127.0.0.1:9229/8136b052-2b35-402e-8c5c-1d15b027a970
For help, see: https://nodejs.org/en/docs/inspector
Server running at http://127.0.0.1:3000/

Therefore the web server has been started on port 3000. Now access http://127.0.0.1:3000/ URL in the browser.

Conclusion

Congratulations! You have successfully installed the latest version of Node.js on your Amazon Linux 2 system using the Yum repository. By keeping Node.js updated to the latest version, you ensure that your projects benefit from the latest features, security patches, and performance enhancements. Remember, the world of technology is constantly changing, and staying abreast of these changes is key to maintaining and developing efficient, secure, and innovative applications. We hope this guide has been helpful in setting up Node.js on Amazon Linux 2, and we encourage you to explore further the vast possibilities that Node.js development has to offer.

You may also like: Using Node Version Manager (NVM) on Amazon Linux 2.

Share.

7 Comments

  1. On the official github nodejssource, as well as on aws documentation, says the following:

    WARNING: BUILD SYSTEM CURRENTLY BROKEN FOR NODEJS 18+

    You need to install nodejs v16

  2. While performing the steps I am getting below erro

    [ec2-user@ip-172-31-35-233 ~]$ sudo yum install -y nodejs
    Loaded plugins: extras_suggestions, langpacks, priorities, update-motd
    amzn2-core | 3.7 kB 00:00
    Resolving Dependencies
    –> Running transaction check
    —> Package nodejs.x86_64 2:18.8.0-1nodesource will be installed
    –> Processing Dependency: libc.so.6(GLIBC_2.28)(64bit) for package: 2:nodejs-18 .8.0-1nodesource.x86_64
    –> Processing Dependency: libm.so.6(GLIBC_2.27)(64bit) for package: 2:nodejs-18 .8.0-1nodesource.x86_64
    –> Finished Dependency Resolution
    Error: Package: 2:nodejs-18.8.0-1nodesource.x86_64 (nodesource)
    Requires: libc.so.6(GLIBC_2.28)(64bit)
    Error: Package: 2:nodejs-18.8.0-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

  3. I’ve just tried this and it shows that the nodesource repository got added to my AWS instance, but it says “No package node.js is available.”

  4. Rahul, you have written the required article. I want to tell you that I was searching for some solution for installing node.js on my amazon virtual machine. Fortunately, you helped me through this article.

    Now, I will perform the instructions mentioned here so that I could get my task done. Thank you.
    Please keep sharing and writing such small tutorials.

Leave A Reply

Exit mobile version