
Node.js is an open-source, cross-platform JavaScript runtime environment for developing server-side scalable applications. The Node Package Manager (NPM) is an essential tool for managing libraries, dependencies, and scripts in a Node.js project. By installing Node.js and NPM on your server, you can use JavaScript as a server-side language.
This article explains how to install Node.js and NPM on Ubuntu 24.04. You will install both Node.js and NPM using the NodeSource PPA and run multiple versions using NVM (Node Version Manager) on your server.
Before you begin:
Access the server using SSH.
Create a non-root user with sudo privileges and switch to the user.
Consider the stability, performance, and application compatibility when selecting a Node.js version to use in your project. Below are the supported Node.js release types to consider when selecting a version.
Consider the following factors when choosing a Node.js release:
The Ubuntu 24.04 APT repositories contain a pre-built Node.js package that may not be the latest version when installed. To install the latest version of Node.js, use the NodeSource PPA to install the latest repository information for a specific Node.js version.
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 a new express application that listens for connections on the host 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) is a useful tool that enables you to install and switch between multiple Node.js versions on your server. Follow the steps below to install multiple Node.js versions on your server using NVM depending on your project requirements.
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.
You have installed Node.js and NPM on your Ubuntu 24.04 server. You can develop applications and switch between different Node.js versions on your server using the Node Version Manager (NVM). For more information, please visit the Node.js documentation.
0 Comments
Be the first to comment and share your perspective with the community.