Node.js, a powerful JavaScript runtime environment, has become an indispensable tool for developers building everything from web applications to desktop software and even embedded systems. Its asynchronous, event-driven architecture makes it exceptionally well-suited for scalable network applications. Ubuntu, a popular and user-friendly Linux distribution, provides an excellent platform for developing with Node.js. This guide will walk you through the most effective methods for installing Node.js on your Ubuntu system, ensuring you have a robust and up-to-date environment for your projects.
Understanding Node.js Installation Methods on Ubuntu
Before diving into the installation process, it’s crucial to understand the different approaches available. Each method offers distinct advantages regarding version management, ease of use, and system integration. The primary methods we will explore are using the Ubuntu repositories, utilizing NodeSource’s PPA (Personal Package Archive), and employing a version manager like NVM (Node Version Manager).

The Ubuntu Repository Method
The most straightforward, though often not the most current, method for installing Node.js on Ubuntu is through its default package repositories. This approach is convenient for users who need a stable, albeit potentially older, version of Node.js and prefer to rely on Ubuntu’s built-in package management system (apt).
Advantages and Disadvantages
The primary advantage of using the Ubuntu repositories is its simplicity. A single command can install Node.js and its associated package manager, npm (Node Package Manager). This method ensures that Node.js integrates seamlessly with other system packages and updates are handled automatically through the standard apt update and apt upgrade process.
However, the main drawback is that the versions of Node.js available in the default Ubuntu repositories are often significantly behind the latest stable releases. This can be a problem if your project requires newer features or compatibility with the most recent JavaScript standards and libraries. For developers who need bleeding-edge functionality or specific version requirements, this method is generally not recommended.
Step-by-Step Installation
-
Update Package Lists:
Before installing any new software, it’s good practice to update your local package index to ensure you are aware of the latest available versions. Open your terminal and execute:sudo apt update -
Install Node.js and npm:
Once the package lists are updated, you can install Node.js and npm. Thenodejspackage in Ubuntu typically includesnpm.sudo apt install nodejs npmThis command will download and install the version of Node.js and npm that is bundled with your current Ubuntu distribution.
-
Verify Installation:
To confirm that Node.js and npm have been installed correctly, you can check their versions:
bash
node -v
npm -v
These commands should output the installed version numbers, confirming a successful installation.
Leveraging NodeSource for Up-to-Date Node.js Versions
For developers who require more recent versions of Node.js than what the Ubuntu repositories offer, NodeSource provides a highly recommended alternative. NodeSource maintains an up-to-date PPA for Ubuntu that makes it easy to install and manage current Node.js releases. This method allows you to choose specific major versions of Node.js to install, such as the latest LTS (Long-Term Support) version or the current stable release.
Why Use NodeSource PPA?
The NodeSource PPA is favored by many developers because it offers a curated collection of Node.js packages that are regularly updated. This ensures that you can easily access and install the latest features, security patches, and performance improvements without having to compile Node.js from source or rely on potentially unstable third-party repositories. It strikes an excellent balance between ease of installation and access to current software.
Installation Steps with NodeSource
NodeSource provides setup scripts that automate the process of adding their PPA and installing the desired Node.js version. You can choose which major version to install by specifying it in the script URL.
-
Download and Run the NodeSource Setup Script:
First, you need to download and execute the setup script for the Node.js version you wish to install. For example, to install Node.js v20.x (a recent LTS version as of this writing), you would use the following commands. Replace20.xwith the desired major version if needed (e.g.,22.xfor the latest current release).curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -This command fetches the script from NodeSource’s official repository and pipes it directly to
bashwithsudoprivileges. The-Eflag preserves your existing environment variables, which is important for the script’s execution. -
Install Node.js:
After running the setup script, youraptpackage list will be updated with the NodeSource repository. You can now install Node.js and npm:sudo apt install nodejsNote that when using the NodeSource PPA, the
nodejspackage usually includesnpmas well, so you don’t need to installnpmseparately. -
Verify Installation:
As with the previous method, verify the installation by checking the versions:
bash
node -v
npm -v
These should now reflect the version you selected from the NodeSource PPA.
Managing Multiple Node.js Versions with NVM
For developers who work on multiple projects that may require different Node.js versions, or for those who frequently need to switch between LTS and the latest stable releases, NVM (Node Version Manager) is an indispensable tool. NVM is a script that allows you to install, manage, and switch between multiple independent Node.js versions on the same machine.

The Power of Version Management
NVM operates by installing Node.js in your user’s home directory, rather than system-wide. This isolation is key to its flexibility. You can install any version of Node.js you need, and then with simple commands, switch the active version for your current terminal session. This avoids conflicts between project dependencies that might be tied to specific Node.js versions and simplifies testing your applications on different Node.js environments.
Installing and Using NVM
-
Install NVM:
The recommended way to install NVM is by downloading and running the installation script from its official GitHub repository. You can do this usingcurlorwget. First, check the NVM GitHub page for the latest installation command. As of this writing, it typically looks like this:curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.5/install.sh | bash(Replace
v0.39.5with the latest NVM version.)After the script completes, it will tell you to close and re-open your terminal or run a command like
source ~/.bashrc(or~/.zshrcif you use Zsh) to load NVM into your current session. -
Verify NVM Installation:
Once your terminal is reloaded, verify that NVM is installed and working:command -v nvmThis command should output
nvmif it’s installed and recognized. -
Install a Node.js Version:
Now you can use NVM to install specific Node.js versions. To install the latest LTS version:nvm install --ltsTo install the latest current version:
nvm install nodeYou can also install a specific version, e.g., Node.js v18.17.0:
nvm install 18.17.0 -
Use a Node.js Version:
After installing, you need to tell NVM which version you want to use for your current session:nvm use --ltsOr for a specific version:
nvm use 18.17.0To set a default Node.js version that will be used whenever you open a new terminal:
nvm alias default node # Sets the latest installed version as defaultor
nvm alias default lts/* # Sets the latest installed LTS as default -
List Installed Versions:
To see all the Node.js versions you have installed with NVM:nvm ls -
Uninstall a Node.js Version:
If you no longer need a specific version:
bash
nvm uninstall 18.17.0
Node.js and npm with NVM
When you use a specific Node.js version via nvm use, both node and npm commands will automatically point to the executables of that installed version. This ensures a clean and isolated environment for each Node.js runtime.
Post-Installation: Global Packages and Build Tools
Once Node.js is installed, you might need to install global packages or development tools. Understanding how to manage these is crucial for a productive Node.js development workflow on Ubuntu.
Installing Global npm Packages
Global npm packages are typically command-line tools that you want to be accessible from anywhere on your system. Examples include task runners like gulp or nodemon, or utility CLIs.
When using the Ubuntu repository or NodeSource PPA installations (which install Node.js system-wide), global packages are installed in a system-wide location. With NVM, global packages are installed within the NVM directory for the currently active Node.js version, keeping them isolated.
To install a global package:
npm install -g <package_name>
For example, to install nodemon globally:
npm install -g nodemon
If you encounter permission errors when installing global packages with system-wide Node.js installations, you might need to configure npm to use a different directory for global packages or use sudo (though the latter is generally discouraged for security reasons unless absolutely necessary). NVM elegantly bypasses these permission issues by installing globally available packages within the user’s home directory.
Essential Build Tools
For certain Node.js modules that involve compiling native add-ons (e.g., those written in C++), you will need development tools installed on your system. These tools allow Node.js to compile these native extensions.
The most common package you’ll need is the build-essential package, which includes the GNU compiler collection (GCC), make, and other necessary development utilities.
To install build tools:
sudo apt update
sudo apt install build-essential
This command will install the essential compilation tools on your Ubuntu system, ensuring that any Node.js modules requiring native compilation can be built successfully.

Conclusion
Choosing the right method for installing Node.js on Ubuntu depends on your specific needs and development practices. For quick setup and if the version is sufficient, the Ubuntu repositories suffice. For up-to-date versions without complex management, NodeSource’s PPA is an excellent choice. However, for developers who require flexibility, version switching, and isolation across multiple projects, NVM stands out as the most powerful and recommended solution. Whichever method you choose, following these steps will set you up with a fully functional Node.js environment on your Ubuntu machine, ready for building modern, scalable applications.
