Introduction

Node.js is a JavaScript runtime for server-side programming. It allows developers to create scalable backend functionality using JavaScript, a language many are already familiar with from browser-based web development.

In this guide, we will show you three different ways to install Node.js on an Ubuntu server:

  • using apt to install the nodejs package from Ubuntu's default software repository
  • using apt with an alternate PPA software repository to install specific versions of the nodejs package
  • installing nvm, the Node Version Manager, and using it to install and manage multiple versions of Node.js

For many users, using apt with the default repo will be sufficient. If you need specific newer or legacy versions of Node, you should use the PPA repository. If you are actively developing Node applications and need to switch between node versions frequently, choose the nvm method.

Note: This article will walk you through installing Node.js on an Ubuntu server. If you want a one-click way to deploy a Node application to a live server, take a look at an app platform.

[info] Key Takeaways:

  • Node.js can be installed on Ubuntu using the default apt repositories, the NodeSource repository, or NVM depending on your version and flexibility requirements.
  • Installing Node.js from Ubuntu's default repositories is the simplest option but may not provide the latest available Node.js version.
  • The NodeSource repository allows you to install newer or specific Node.js versions using apt and includes npm by default.
  • NVM enables you to install, manage, and switch between multiple Node.js versions on the same system without affecting system-wide installations.
  • After installing Node.js, you can verify the installation by checking the Node.js version using the node -v command.
  • Node.js installed via apt or NodeSource can be removed using apt remove or apt purge, while NVM-installed versions are removed using nvm uninstall.
  • Permission issues with global npm installs commonly occur with system-wide installations and can be avoided by using NVM.
  • Choosing the right installation method depends on whether you prioritize simplicity, access to newer versions, or the ability to manage multiple Node.js versions.

Prerequisites

install illustration for: Prerequisites

To follow this guide, you will need an Ubuntu server. Before you begin, you should have a non-root user account with sudo privileges set up on your system. You can learn how to do this by following the Ubuntu initial server setup tutorial.

Option 1 — Installing Node.js with Apt from the Default Repositories

Ubuntu 24.04 contains a version of Node.js in its default repositories that can be used to provide a consistent experience across multiple systems. At the time of writing, the version in the repositories is v24.x. This will not be the latest version, but it is stable and sufficient for quick experimentation.

To get this version, you can use the apt package manager. Refresh your local package index first:

				
					sudo apt update
				
			

Then install Node.js:

				
					sudo apt install nodejs
				
			

Check that the install was successful by querying node for its version number:

				
					node -v
				
			
				
					[secondary_label Output]
v24.13.0
				
			

Note: Your output may differ depending on when you install.

If the package in the repositories suits your needs, this is all you need to do to get set up with Node.js. In most cases, you'll also want npm, the Node.js package manager. On some Ubuntu versions, npm is installed automatically with nodejs. If it is missing, you can install it separately with apt:

				
					sudo apt install npm
				
			

This allows you to install modules and packages to use with Node.js.

At this point, you have successfully installed Node.js and npm using apt and the default Ubuntu software repositories. The next section will show how to use an alternate repository to install different versions of Node.js.

Option 2 — Installing Node.js with Apt Using a NodeSource PPA

To install a different version of Node.js, you can use a *PPA* (personal package archive) maintained by NodeSource. These PPAs have more versions of Node.js available than the official Ubuntu repositories. Node.js v24 and v25 are available as of the time of writing.

First, install the PPA to get access to its packages. From your home directory, use curl to retrieve the installation script for your preferred version, making sure to replace <^>25.x<^> with your preferred version string (if different):

				
					cd ~
curl -sL https://deb.nodesource.com/setup_&lt;^&gt;25.x&lt;^&gt; -o /tmp/nodesource_setup.sh
				
			

Refer to the NodeSource documentation for more information on the available versions.

Inspect the contents of the downloaded script with nano or your preferred text editor:

				
					nano /tmp/nodesource_setup.sh
				
			

When you are satisfied that the script is safe to run, exit your editor. Then run the script with sudo:

				
					sudo bash /tmp/nodesource_setup.sh
				
			

The PPA will be added to your configuration and your local package cache will be updated automatically. You can now install the Node.js package in the same way you did in the previous section:

				
					sudo apt install nodejs
				
			

Verify that you've installed the new version by running node with the -v version flag:

				
					node -v
				
			
				
					[secondary_label Output]
v25.5.0
				
			

The NodeSource nodejs package contains both the node binary and npm, so you don't need to install npm separately.

At this point, you have successfully installed Node.js and npm using apt and the NodeSource PPA. The next section will show how to use the Node Version Manager to install and manage multiple versions of Node.js.

Option 3 — Installing Node Using the Node Version Manager

Another way of installing Node.js that is particularly flexible is to use nvm, the Node Version Manager. This piece of software allows you to install and maintain many independent versions of Node.js, and their associated Node packages, at the same time.

To install NVM on your Ubuntu machine, visit the project's GitHub page. Copy the curl command from the README file that displays on the main page. This will get you the most recent version of the installation script.

Before piping the command through to bash, it is always a good idea to audit the script to make sure it isn't doing anything you don't agree with. You can do that by removing the | bash segment at the end of the curl command:

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

Review the script and make sure you are comfortable with the changes it is making. When you are satisfied, run the command again with | bash appended at the end. The URL you use will change depending on the latest version of nvm, but as of right now, the script can be downloaded and executed with the following:

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

This will install the nvm script to your user account. To use it, you must first source your .bashrc file:

				
					source ~/.bashrc
				
			

Now, you can ask NVM which versions of Node are available:

				
					nvm list-remote
				
			
				
					[secondary_label Output]
. . .
 v24.0.0
 v24.0.1
 v24.0.2
 v24.1.0
 v24.2.0
 v24.3.0
 v24.4.0
 v24.4.1
 v24.5.0
 v24.6.0
 v24.7.0
 v24.8.0
 v24.9.0
 v24.10.0
 v24.11.0 (LTS: Krypton)
 v24.11.1 (LTS: Krypton)
 v24.12.0 (LTS: Krypton)
 v24.13.0 (Latest LTS: Krypton)
 v25.0.0
 v25.1.0
 v25.2.0
 v25.2.1
 v25.3.0
 v25.4.0
 v25.5.0
. . .
				
			

It's a very long list. You can install a version of Node by writing in any of the release versions listed. For instance, to get version v24.13.0, you can run:

				
					nvm install v24.13.0
				
			

You can view the different versions you have installed by listing them:

				
					nvm list
				
			
				
					[secondary_label Output]
-&gt; v22.22.0
 v24.13.0
default -&gt; v24.13.0
iojs -&gt; N/A (default)
unstable -&gt; N/A (default)
node -&gt; stable (-&gt; v24.13.0) (default)
stable -&gt; 24.13 (-&gt; v24.13.0) (default)
. . .
				
			

This shows the currently active version on the first line (-> v22.22.0), followed by some named aliases and the versions that those aliases point to.

Note: If you also have a version of Node.js installed through apt, you may receive a system entry here. You can always activate the system-installed version of Node using nvm use system.

Additionally, there are aliases for the various long-term support (or LTS) releases of Node:

				
					[secondary_label Output]
lts/* -&gt; lts/krypton (-&gt; v24.13.0)
lts/argon -&gt; v4.9.1 (-&gt; N/A)
lts/boron -&gt; v6.17.1 (-&gt; N/A)
lts/carbon -&gt; v8.17.0 (-&gt; N/A)
lts/dubnium -&gt; v10.24.1 (-&gt; N/A)
lts/erbium -&gt; v12.22.12 (-&gt; N/A)
lts/fermium -&gt; v14.21.3 (-&gt; N/A)
lts/gallium -&gt; v16.20.2 (-&gt; N/A)
lts/hydrogen -&gt; v18.20.8 (-&gt; N/A)
lts/iron -&gt; v20.20.0 (-&gt; N/A)
lts/jod -&gt; v22.22.0 (-&gt; N/A)
lts/krypton -&gt; v24.13.0
				
			

You can install a release based on these aliases as well. For instance, to install the latest long-term support version, krypton, run the following:

				
					nvm install lts/krypton
				
			
				
					[secondary_label Output]
Downloading and installing node v24.13.0...
. . .
Now using node v24.13.0 (npm v11.6.2)
				
			

You can switch between installed versions with nvm use:

				
					nvm use v25.0.0
				
			
				
					[secondary_label Output]
Now using node v25.0.0 (npm v11.6.2)
				
			

You can verify that the install was successful using the same technique from the other sections:

				
					node -v
				
			
				
					v24.13.0
				
			

The correct version of Node is installed on your machine as expected. A compatible version of npm is also available.

Removing Node.js

You can uninstall Node.js using apt or nvm, depending on how it was installed. To remove the version from the system repositories, use apt remove:

				
					sudo apt remove nodejs
				
			

By default, apt remove retains any local configuration files that were created since installation. If you don't want to save the configuration files for later use, use apt purge:

				
					sudo apt purge nodejs
				
			

To uninstall a version of Node.js that you installed using nvm, first determine whether it is the current active version:

				
					nvm current
				
			

If the version you are targeting is not the current active version, you can run:

				
					nvm uninstall &lt;^&gt;node_version&lt;^&gt;
				
			
				
					[secondary_label Output]
Uninstalled node &lt;^&gt;node_version&lt;^&gt;
				
			

This command will uninstall the selected version of Node.js.

If the version you would like to remove is the current active version, you first need to deactivate nvm to enable your changes:

				
					nvm deactivate
				
			

Now you can uninstall the current version using the uninstall command used previously. This removes all files associated with the targeted version of Node.js.

Practical Use Cases

Installing Node.js for App Development

When starting a new project, installing Node.js is a crucial step. Node.js allows developers to run JavaScript on the server-side, making it a popular choice for web applications. For example, if you're building a real-time chat application, you might want to use Node.js with a framework like Express.js to handle incoming requests and send responses.

To illustrate this, let's consider a scenario where you're building a simple chat application using Node.js and Express.js. You want to create a server that listens for incoming messages and broadcasts them to all connected clients. Here's a simple example of how you might structure your server-side code using Node.js and Express.js:

				
					const express = require('express');
const app = express();
const server = require('http').createServer(app);
const io = require('socket.io')(server);

app.get('/', (req, res) =&gt; {
 res.sendFile(__dirname + '/index.html');
});

io.on('connection', (socket) =&gt; {
 console.log('a user connected');
 socket.on('disconnect', () =&gt; {
 console.log('user disconnected');
 });
 socket.on('chat message', (msg) =&gt; {
 io.emit('chat message', msg);
 });
});

server.listen(3000, () =&gt; {
 console.log('listening on *:3000');
});
				
			

This example demonstrates how Node.js can be used to create a server that handles real-time communication between clients.

To install Node.js for app development, you can use the methods described earlier in this tutorial. For instance, you can use apt to install Node.js from the Ubuntu repository or use nvm to install a specific version of Node.js. Here's an example command to install Node.js using apt:

				
					sudo apt update &amp;&amp; sudo apt install nodejs
				
			

Alternatively, you can use nvm to install a specific version of Node.js. First, install nvm if you haven't already, then use the following command to install a specific version of Node.js:

				
					nvm install &lt;version&gt;
				
			

Replace <version> with the version number you want to install.

Setting up Backend Projects

Node.js is widely used for setting up backend projects due to its event-driven, non-blocking I/O model. This makes it lightweight and efficient for handling multiple concurrent connections. For example, if you're building a RESTful API, Node.js can be used to create the server-side logic, handle database interactions, and return data to the client.

When setting up a backend project, you might want to use a framework like Express.js or Koa.js to simplify the process. These frameworks provide a structure for organizing your code and handling HTTP requests and responses.

Managing Multiple Node.js Versions with NVM

Managing multiple Node.js versions is essential when working on different projects that require different versions of Node.js. NVM (Node Version Manager) is a popular tool for managing multiple Node.js versions on a single machine.

For example, let's say you're working on two projects: one that requires Node.js version 22.x and another that requires Node.js version 24.x. With NVM, you can easily switch between these versions without affecting the system's default Node.js version.

To switch between Node.js versions using NVM, you can use the following command:

				
					nvm use &lt;version&gt;
				
			

Replace <version> with the version number you want to use. This command will switch your Node.js version to the specified one, allowing you to work on your project without affecting other projects that require different versions.

Common Errors and Debugging

Permission issues with global installs

Permission issues with global installs occur when you try to install a package globally using npm without having the necessary permissions. This is because global packages are installed in a system directory that requires root access.

Why it occurs: This error occurs because npm tries to write to a directory that requires root access, but the user running npm does not have those permissions.

How to fix it: To fix this issue, you can use one of the following methods:

  • Run the command with sudo: sudo npm install -g <package-name>
  • Change the ownership of the npm directory to the current user: sudo chown -R $USER:$(whoami) /usr/local/{lib/node_modules,bin,share/man} && sudo chown -R $USER:$(whoami) ~/.npm
  • Use a package manager like nvm or yarn that does not require root access for global installs.

NVM not found after install (profile not sourced)

NVM not found after install occurs when you install NVM but do not source the NVM script in your shell profile.

Why it occurs: This error occurs because NVM is not added to your system's PATH after installation. The NVM script needs to be sourced in your shell profile to make NVM available.

How to fix it: To fix this issue, you need to add the following line to your shell profile file (e.g., ~/.bashrc or ~/.zshrc):

				
					source ~/.nvm/nvm.sh
				
			

Then, restart your terminal or run source ~/.nvm/nvm.sh to apply the changes.

Conflicts between apt and NVM-installed versions

Conflicts between apt and NVM-installed versions occur when you have Node.js installed using both apt and NVM. This can cause issues with package management and version conflicts.

Why it occurs: This error occurs because both apt and NVM install Node.js in different locations, leading to conflicts between the two installations.

How to fix it: To fix this issue, you should remove the Node.js installation from apt and use NVM as the primary method for managing Node.js versions. Here's how to do it:

  • Remove Node.js installed using apt: sudo apt purge nodejs
  • Install Node.js using NVM: nvm install node
  • Make sure to use NVM to manage your Node.js versions going forward.

Comparison of Installation Methods

Method Description Pros Cons
Apt Installs Node.js from Ubuntu's repository Easy to install, no additional setup required Limited version control, may not be the latest version
NVM Installs and manages multiple Node.js versions Allows for easy version switching, supports multiple versions Requires additional setup, can be complex for beginners
NodeSource PPA Installs Node.js from a third-party repository Provides access to the latest Node.js versions, easy to install May not be as secure as other methods, requires adding a third-party repository
Binary Installs Node.js from pre-compiled binaries Fast installation, no dependencies required May not be compatible with all systems, requires manual updates
Source Installs Node.js from source code Provides complete control over the installation, can be customized Time-consuming, requires technical expertise, manual updates required

FAQs

1. What's the best way to install Node.js on Ubuntu?

The best way to install Node.js on Ubuntu is to use a package manager like NVM (Node Version Manager) or a NodeSource PPA. These methods provide more flexibility and allow for easy version management.

				
					# Install Node.js using NVM
nvm install node

# Install Node.js using NodeSource PPA
sudo apt update &amp;&amp; sudo apt install nodejs
				
			

2. How do I check my Node.js version?

To check your Node.js version, run the command node -v in your terminal. This will display the version of Node.js installed on your system.

				
					node -v
				
			

3. Can I install multiple versions of Node.js?

Yes, you can install multiple versions of Node.js using a package manager like NVM. NVM allows you to install, manage, and switch between different versions of Node.js easily.

				
					# Install a specific version of Node.js using NVM
nvm install 24.x

# List all installed Node.js versions
nvm ls
				
			

4. What is NVM and why should I use it?

NVM (Node Version Manager) is a tool that allows you to easily install, manage, and switch between different versions of Node.js on your system. You should use NVM because it provides a convenient way to manage multiple Node.js versions, which is particularly useful for developers who need to work with different projects that require different Node.js versions.

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

# Use NVM to install Node.js
nvm install node
				
			

5. How do I uninstall Node.js completely?

To uninstall Node.js completely, you need to remove all Node.js packages and dependencies. If you installed Node.js using apt, you can uninstall it by running sudo apt purge nodejs. If you used NVM, you can uninstall Node.js by running nvm uninstall node.

				
					# Uninstall Node.js using apt
sudo apt purge nodejs

# Uninstall Node.js using NVM
nvm uninstall node
				
			

6. Does Ubuntu come with Node.js preinstalled?

Most Ubuntu installations do not come with Node.js preinstalled. You need to install it manually using a package manager or by downloading the binaries.

				
					# Check if Node.js is installed
node -v
				
			

7. How do I update Node.js on Ubuntu?

To update Node.js on Ubuntu, you can use the package manager you used to install it. If you used apt, you can update Node.js by running sudo apt update && sudo apt full-upgrade. If you used NVM, you can update Node.js by running nvm install node --latest.

				
					# Update Node.js using apt
sudo apt update &amp;&amp; sudo apt full-upgrade

# Update Node.js using NVM
nvm install node
				
			

Conclusion

There are several ways to set up Node.js on your Ubuntu server, each with its own advantages. Whether you choose to use the packaged version, nvm, or a NodeSource PPA, the key is to select the method that best fits your project's requirements.

To further explore Node.js and its applications, we recommend checking out the following tutorials and resources:

By following these guides, you'll be well on your way to mastering Node.js and deploying your applications efficiently.