How to Install Node.js and npm on Ubuntu 22.04: Step-by-Step Guide

Node.js is a JavaScript runtime built on Chrome’s V8 JavaScript engine. It’s designed for creating scalable network applications. npm, on the other hand, is the default package manager for Node.js, and it’s a treasure trove of libraries that can be easily integrated into your Node projects.

Why Node.js and npm on Ubuntu 22.04?

  • Versatility: From small-scale applications to large-scale enterprise systems, Node.js is versatile.
  • Performance: With its non-blocking I/O and event-driven architecture, Node.js ensures swift execution.
  • Ecosystem: npm boasts an expansive ecosystem, offering over a million open-source packages.

How to Install Node.js and npm on ubuntu 22.04

Learn how to install Node.js and npm on Ubuntu 22.04. Discover the versatility, performance, and vast ecosystem they offer for web development.

Update and Upgrade Your System

First and foremost, ensure your Ubuntu system is up-to-date:

sudo apt update
sudo apt upgrade

Ubuntu repository

sudo apt install nodejs

NodeSource

If instead you’d like to install node.js via nodesource, you can do as follow:

curl -sL https://deb.nodesource.com/setup_16.x | sudo -E bash -

Make sure to replace 16 by the version of node.js you’d like to install! In this case this will install node.js 16.

NVM (Node Version Manager)

For those who work on multiple projects and need different Node.js versions, NVM is a lifesaver. It allows you to install multiple versions of Node.js and switch between them effortlessly.

First, install NVM:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.38.0/install.sh | bash

Then, close and reopen your terminal or source your profile:

source ~/.bashrc

Now, let’s install node.js through NVM:

nvm install node

This will install the latest node.js version, in the case you need a specific version you can specify it:

nvm install [version_number]

Once installed, verify the installation;

Verifying our Node.js Installation

nodejs -v
output

labs@linuxify:~# node -v
v16.20.0

Example

Here’s a quick example of a quick application to see if our setup is working correctly:

Create index.js

echo 'console.log("Hello, World! This is Linuxify");' > hello.js

Run the application:

node hello.js

If its successful, then the output should be as follow:

labs@linuxify:~/dev# node hello.js 
Hello, World! This is Linuxify

Conclusion

With the power of Node.js and npm at your fingertips, you’re now ready to dive into the world of modern web development on your Ubuntu 22.04 server. Whether you’re building web servers, APIs, or any other networked applications, this duo will ensure you have a robust and efficient environment. Hope you enjoyed this quick guide on how to Install Node.js and npm on Ubuntu 22.04.

Happy coding!

Node.js Documentation

NPM documentation

Leave a Comment