Three Simple Ways You Can Install Node.js On Ubuntu 22.04

Try this guide with our instant dedicated server for as low as 40 Euros

install nodejs ubuntu

By providing your email address or using a single sign-on provider to create an account, you agree to our Terms of Service and that you have reviewed our Privacy Policy.

Thank you for contacting us! We will get in touch with you soon.

In recent years, node.js has emerged as a powerful development platform, especially for browser-based applications.

Node.js is designed to build scalable network applications like chat and streaming platforms.

It uses an event-driven, non-blocking I/O model that is lightweight and efficient, perfect for data-intensive real-time applications. Given these capabilities, it is a popular choice for building APIs and microservices.

In this article, we’ll go into the details of installing Node.js on Ubuntu 22.04. However, before we go into the process details, we’d like to discuss NPM because it is an integral component of node.js development projects.

Table Of Content

  1. A Short Introduction to NPM
  2. Three Ways You Can Install Node.js on Ubuntu 22.04
  3. Method 1: Install Node.js From the Official Repository on Ubuntu 22.04
  4. Method 2: Install a Specific Node.js Version with NodeSource
  5. Method 3: Install Node.js and NPM Using NVM/a>
  6. Use NPM to Simplify Node.js Management on Ubuntu 22.04
  7. How to Secure Node.js?
  8. How to Remove Node.js from Ubuntu Server?
  9. Conclusion
  10. FAQs

A Short Introduction to NPM

Node Package management, or NPM, is a package manager for JavaScript. 

It includes Node.js, a runtime environment for executing JavaScript code outside a web browser. 

NPM has evolved into a critical tool in the JavaScript ecosystem, allowing developers to take advantage of the vast JS package ecosystem to streamline the development process. 

Here are a few NPM characteristics that make it such a powerful JS development component:

Package Management 

NPM simplifies installing, managing, and updating Node.js packages and dependencies. It offers access to a large and mature ecosystem of open-source packages you can use in your apps.

Command Line Interface

NPM includes a command-line interface (CLI) that allows you to communicate with the package management. You can manage packages using npm install, update, and uninstall commands.

Package.json

NPM keeps track of your project’s dependencies, scripts, and other metadata in a file called package.json. This file acts as a manifest for your project and lets you specify the required packages and dependencies.

Easy Version Naming 

NPM employs semantic versioning, consisting of three integers separated by dots (1.2.3). These numbers denote the major, minor, and patch versions. You may define version ranges for your dependencies using NPM, allowing you control over which versions to install.

Support for Custom Scripting

In the package.json file, developers may define custom scripts. These scripts may be launched using the NPM CLI to automate basic operations like running tests, creating assets, launching a development server, and more.

Huge Package Registry

The npm Registry is NPM’s default registry, which holds millions of packages. If necessary, you can also create private registrations or use other registrars.

NPM is a powerful tool for managing JavaScript packages and dependencies in your Node.js projects. It simplifies integrating third-party code into your applications and helps ensure your project remains up-to-date and secure.

After that brief introduction to NPM, let’s discuss the various options for installing Node.js on an Ubuntu machine.

Before trying out any of the three methods, you need a Ubuntu 22.04 server and a non-root account with sudo privileges. 

nodejs_nvm_ubuntu

Three Ways You Can Install Node.js on Ubuntu 22.04

Installing Node.js on Ubuntu 22.04 is a very flexible process, and you can opt for several methods to carry out the installation. Let’s discuss three ways to install Node.js on an Ubuntu 22.04 server easily. 

Method 1: Install Node.js From the Official Repository on Ubuntu 22.04

The default Ubuntu repositories include Node.js and npm. As a result, you can easily install them using the APT package manager. 

Start by updating the Ubuntu package index with the following command:

# apt update

Next, install Node.js and NPM with the following command. The APT package manager will download and unpack the involved packages.

# apt install nodejs npm -y

nodejs-install

Once the installation finishes, we recommend checking the Node.js installation by printing out the version information:

# node -v

nodeversion1st

Method 2: Install a Specific Node.js Version with NodeSource

The method mentioned above installs the Node.js version in the official Ubuntu repository.

However, if you wish to install a different Node.js version on Ubuntu 22.04 server, you can use a PPA supplied by NodeSource to install a different version of Node.js. These PPAs have more Node.js versions than the official Ubuntu repository. 

As of this writing, Node.js v14, v16, and v18 are all accessible.

To demonstrate the method, we’ll install Node.js version 20.x. You can install other versions, such as 18 or 19, using this method. 

Start with the following curl command that gets the desired repository from NodeSource:

# curl -sL https://deb.nodesource.com/setup_20.x -o nodesource_setup.sh

nodesource-sh

The PPA from NodeSource will be added to the settings, and your local package cache will be automatically updated. 

Start the installation process by calling up the setup script in the terminal. 

# bash nodesource_setup.sh 

bash-nodesource

Next, install Node.js with the following command: 

# apt install nodejs

Once the process finishes, you can verify the installation by printing the version details. 

# node -v

nodeversion2nd

Method 3: Install Node.js and NPM Using NVM

If you have multiple Node.js versions on your server, you can use Node Version Manager or NVM to manage these versions. 

You can use NVM to install NodeJS and NPM on your machine.

Start by taking a look at the following script:  

# curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh

curlnvm_show

  • To download and install NVM, run the following script:

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

downld_script

Next, run the following commands to load NVM: 

# export NVM_DIR="$HOME/.nvm"

# [ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"

# [ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"

Once these commands finish, NVM will be installed on your server. Next, check the version: 

# nvm -v

nvm-v2

Now, to install the available versions, use the following command: 

# nvm ls-remote

nvm_versions

Install the latest Node.js version with the following command:

# nvm install node

nvminstall

Include the version number in the command if you wish to install a specific version. For instance, if you wish to install version 17.1.0, you can use the following command with the version: 

# nvm install v17.1.0

Use NPM to Simplify Node.js Management on Ubuntu 22.04

Experts advise installing NPM alongside Node.js because NPM simplifies the management of the Node.js service. For instance, you can use the following NPM commands to manage the interaction with the packages: 

  • npm ls: List the installed packages to see whether you need to install something before running it.
  • npm run-script: Executes a given script.
  • npm start: Launches a package.
  • npm stop: Terminates the execution of a package.
  • npm restart: Restarts a package. This kicks off the stop-start cycle for the package.

How to Secure Node.js?

Unfortunately, Node.js provides a powerful scripting engine that is easy to abuse. That’s why installing Node.js without following the recommended practices is a surefire invitation for cyberattacks.

Securing a Node.js application involves implementing various best practices and security measures. Here are some crucial steps to secure a Node.js application:

  1. The root user(s) should not launch Node.js service: Running code as the root user gives any hacker an excellent resource for disassembling everything else. We highly recommend launching Node.js with only the required permissions for the application.
  2. Update dependencies: Regularly update your application’s dependencies, including the Node.js runtime and external libraries. This is essential for closing off any vulnerabilities in dependencies that you might overlook in a manual patching or update process. 
  3. Use robust authentication: Your application’s first line of defense is to guarantee that the user is not a hacker. It is best to utilize a solution like Authenticator or SecureAuth for authentication.
  4. Use a reverse proxy: A reverse proxy is a type of web server that allows you to limit the number of requests a Node.js application may get. Essentially, the reverse proxy accepts the user request, validates it, and only then transmits it to the Node.js application.
  5. Set package access levels: One of the reasons for using package management like NPM is to regulate who can (and, more importantly, can’t) access packages. 
  6. Enable Transport Layer Security (TLS): Use HTTPS with a valid SSL/TLS certificate to encrypt communication between the client and server. This helps protect sensitive data from interception and tampering.
  7. Secure the production environment: Ensure that your production environment is properly secured. Restrict server access, turn off unnecessary services and ports, use secure SSH configurations, and monitor system logs for suspicious activities regularly. 

How to Remove Node.js from Ubuntu Server?

You can remove Node.js with apt or nvm depending on how you 

installed it.

If you used the APT to install it, use the apt remove command to remove the Node.js version from the system repositories:

# apt remove nodejs

removenodejs

By default, the apt remove command keeps any local configuration files. If you don’t wish to keep the configuration files, run the apt purge command:

# apt purge nodejs

If you used NVM to install Node.js, you should first establish if it is the currently active version:

# nvm current

nvm-current

Now, before installing the Node.js version, you need to deactivate it with the following command:

# nvm deactivate 

nvm-deactivate

Finally, uninstall the currently active version with the following command: 

# nvm uninstall node_current_verion

nvmre-unnstall

Also Read: How to Install node.js on Debian: 3 Simple Methods

Conclusion  

We hope that you now understand how to install Node.js on Ubuntu 22.04. With Node.js successfully installed on your Ubuntu 22 system, you can develop and run Node.js applications. To maintain the security of your Node.js apps, remember to follow security best practices, keep your dependencies current, and apply suitable security measures.

The challenge with Node.js applications is providing a stable platform that continues to keep up with the increasing workload. RedSwitches provides bare-metal dedicated servers where you can install Node.js on Ubuntu (or ask our support engineers to do it for you). Our support teams proactively resolve all queries our users raise in record time. 

Frequently Asked Questions

Q-1) What exactly is NPM?

NPM (Node Package Manager) is the Node.js default package manager for installing, managing, and sharing packages containing reusable JavaScript code.

Q-2) How can I get the most recent NPM version?

You may use the following command to update NPM to the most recent version: npm install -g npm. This will update NPM on your system globally.

Q-3) How can I use the NPM registry to identify and install popular packages?

Popular packages may be found on the NPM website or by the npm search command. Once you’ve found the necessary package, run npm install package-name to install it.

Q-4) Can I add my packages to the NPM repository?

You may publish your packages to the NPM repository. You may develop your package and publish it for others to use by following the NPM instructions and rules.

Try this guide with our instant dedicated server for as low as 40 Euros