
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 20.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:
Have an Ubuntu 20.04 server.
Access the server using SSH as a non-root user with sudo privileges.
When selecting a Node.js version for your project, consider factors like stability, performance, and application compatibility. Below are the supported Node.js release types:
Factors to consider when choosing a release:
The default Ubuntu 20.04 APT repositories may not have the latest version of Node.js. To install the most recent version, use the NodeSource PPA to add the latest repository for the specific Node.js version you want to install.
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 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 handy tool for managing multiple Node.js versions on your server. You can install and switch between different Node.js versions based on your project needs. Follow the steps below to set up NVM and install the required Node.js versions.
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 successfully installed Node.js and NPM on your Ubuntu 20.04 server. With the Node Version Manager (NVM), you can easily switch between different Node.js versions to meet your project's requirements.
0 Comments
Be the first to comment and share your perspective with the community.