
Node.js is an open-source, cross-platform JavaScript runtime for building scalable server-side applications. NPM manages libraries, dependencies, and scripts in Node.js projects, enabling server-side JavaScript use.
This article demonstrates how to install Node.js and NPM on Ubuntu 22.04. You will install both using the NodeSource PPA and manage multiple Node.js versions with NVM (Node Version Manager) on your server.
Before you begin:
Have an Ubuntu 22.04 server.
Access the server using SSH as a non-root user with sudo privileges.
When selecting a Node.js version, consider the following:
Factors to consider:
The Ubuntu 22.04 APT repositories may not have the latest Node.js version. To install the latest version, use the NodeSource PPA for the most up-to-date repository information for a specific Node.js version.
Update the server package index.
Download your desired Node.js version PPA installation script. For example, run the following command to use the Node.js version 22.x.
Visit the official NodeSource documentation to verify all available Node.js versions you can install on your server.
Run the Node.js setup script.
Install Node.js and NPM using the following command.
View the installed Node.js version on your server.
Output:
View the installed NPM version on your server.
Output:
Create a new project directory such as example-site.
Switch to the directory.
Initialize a new Node.js project using npm.
Install the express module using npm.
Create a new index.js application file.
Add the following code to the file.
Save and close the file.
The above Node.js code creates an Express application that listens on port 3000 and outputs a Hello World! Greetings from Vultr message.
Allow port 3000 through the firewall to enable network connections to the application.
Reload the firewall to apply changes.
Start the Node.js application.
Access your Node.js application using your server IP and port 3000 in a web browser such as Chrome.
NVM (Node Version Manager) allows you to install and switch between multiple Node.js versions on your server. Follow the steps below to install various Node.js versions using NVM based on your project needs.
Download the latest NVM installation script on your server.
Please, refer to the NVM documentation to get the latest version at the time of your installation.
Run the script to install NVM in your user environment.
Reload your server environment variables.
List all available Node.js versions you can install on your server.
Your output should look like the one below.
Install a specific Node.js version. For example, Node.js 20.
List all installed Node.js versions and verify the default version on your server.
Output:
Run the following command to install a specific Node.js version such as 20 using NVM.
Output:
View the active Node.js version in your user environment.
Output:
View the NPM version in your user environment.
Output:
Run the following command to a specific version as the default Node.js version in your environment.
In this article, you installed Node.js and NPM on your Ubuntu 22.04 server. You can now develop server-side applications and manage multiple Node.js versions using Node Version Manager (NVM).
0 Comments
Be the first to comment and share your perspective with the community.