How to Install Node.js on Ubuntu 22.04: Three Proven Methods
Node.js is one of the most powerful and widely adopted JavaScript runtime environments available today. Built on Chrome's V8 engine, it enables developers to execute JavaScript code server-side, making it the backbone of countless scalable, high-performance web applications, APIs, and microservices. Whether you're deploying a full-stack application on a VPS Hosting plan or experimenting locally, knowing how to correctly install and manage Node.js on Ubuntu 22.04 is an essential skill.
This comprehensive guide covers three distinct installation methods, each suited to different use cases:
- Installing Node.js from Ubuntu's default repositories
- Installing a specific version via the NodeSource PPA
- Managing multiple versions with Node Version Manager (NVM)
By the end, you'll know exactly which method fits your project requirements β and how to get Node.js running reliably on your Ubuntu 22.04 server.
Prerequisites
Before you begin, make sure you have:
- A server or local machine running Ubuntu 22.04 LTS
- A user account with sudo privileges
- Basic familiarity with the Linux command line
- An active internet connection
If you're working on a remote server, a VPS with cPanel or a bare Dedicated Server both provide ideal environments for Node.js deployments.
Method 1: Install Node.js from Ubuntu's Default Repositories
This is the simplest and fastest installation method. It's best suited for developers who don't require the absolute latest Node.js version and simply want a stable, functional runtime up and running quickly.
Step 1: Update the Package Index
Always start by refreshing your system's package list to ensure you're pulling the most current versions available in the repository:
sudo apt updateStep 2: Install Node.js and npm
Ubuntu 22.04's default repositories include Node.js along with npm (Node Package Manager). Install both with a single command:
sudo apt install nodejs npmThis will install Node.js and npm as system-wide packages, making them available to all users on the machine.
Step 3: Verify the Installation
Once the installation completes, confirm that both Node.js and npm are correctly installed by checking their versions:
node -v
npm -vYou should see output similar to:
v12.22.9
6.14.15> Note: The version available in Ubuntu 22.04's default repositories may not be the latest stable release. If your project requires a newer version, proceed to Method 2 or Method 3.
When to Use This Method
- You need a quick, no-frills installation
- Your application is compatible with the repository's bundled Node.js version
- You're setting up a development or testing environment on a Shared Web Hosting or entry-level VPS
Method 2: Install Node.js Using the NodeSource PPA
If you need a specific or more recent version of Node.js β such as the latest LTS release β the NodeSource PPA is the recommended approach. NodeSource maintains up-to-date packages for multiple Node.js versions and includes all security patches and new features.
Step 1: Add the NodeSource Repository
Use curl to download and execute the NodeSource setup script for your desired version. The example below installs Node.js 18.x (LTS):
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -To install a different version, simply replace 18.x with your target version number (e.g., 20.x for Node.js 20):
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -This command adds the NodeSource repository to your system's package sources and imports the necessary GPG signing key to verify package authenticity.
Step 2: Install Node.js
With the repository added, install Node.js using apt:
sudo apt install nodejsnpm is bundled automatically with this installation method β no separate install step is required.
Step 3: Verify the Installation
Confirm the correct version has been installed:
node -v
npm -vExpected output for Node.js 18.x:
v18.20.2
10.5.0When to Use This Method
- You need a specific LTS or current release of Node.js
- You want automatic security updates via
apt upgrade - You're running a production application on a VPS Hosting environment and need a stable, well-maintained package source
Method 3: Install Node.js Using Node Version Manager (NVM)
NVM (Node Version Manager) is the most flexible installation method and is strongly recommended for developers who work across multiple projects with different Node.js version requirements. With NVM, you can install, switch between, and manage any number of Node.js versions β all without affecting system-wide packages.
Step 1: Install NVM
Download and run the official NVM installation script:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bashThe script clones the NVM repository into ~/.nvm and adds the necessary configuration lines to your shell profile. After the script completes, either restart your terminal or reload your shell configuration:
source ~/.bashrcVerify that NVM is available:
nvm --versionStep 2: Install Node.js Using NVM
To install the latest stable version of Node.js:
nvm install nodeTo install a specific version (e.g., Node.js 18.0.0):
nvm install 18.0.0To see all available Node.js versions you can install:
nvm ls-remoteYou can install as many versions as you need β NVM stores each one independently in your home directory.
Step 3: Set the Default Node.js Version
To switch to a specific version for your current terminal session:
nvm use 18.0.0To set a version as the global default across all new terminal sessions:
nvm alias default 18.0.0Step 4: Verify the Installation
Check that the correct version is active:
node -v
npm -vSwitching Between Versions
One of NVM's most powerful features is effortless version switching. For example, if Project A requires Node.js 16 and Project B requires Node.js 20:
nvm use 16
# Work on Project A
nvm use 20
# Switch to Project BWhen to Use This Method
- You're a developer managing multiple projects with different Node.js dependencies
- You need to test your application across different Node.js versions
- You want complete control over your runtime environment without touching system packages
- You're working in a containerized or isolated development environment
Installing npm Separately (If Needed)
In virtually all cases, npm is installed automatically alongside Node.js using any of the three methods above. However, if for any reason npm is missing from your system, you can install it independently:
sudo apt install npmVerify the installation:
npm -vYou can also update npm to the latest version at any time using:
npm install -g npm@latestHow to Uninstall Node.js on Ubuntu 22.04
Uninstalling Node.js Installed via apt
If you installed Node.js using Method 1 or Method 2, remove it with:
sudo apt remove nodejsTo also remove configuration files and clean up unused dependencies:
sudo apt purge nodejs
sudo apt autoremoveUninstalling a Specific NVM Version
For NVM-managed installations, uninstall a specific version with:
nvm uninstall 18.0.0Uninstalling NVM Entirely
To completely remove NVM from your system:
rm -rf ~/.nvmThen remove the NVM-related lines from your ~/.bashrc, ~/.bash_profile, or ~/.zshrc file.
Choosing the Right Method: Quick Comparison
| Feature | Ubuntu Repositories | NodeSource PPA | NVM |
|---|---|---|---|
| Ease of installation | βββββ | ββββ | βββ |
| Latest version available | β | β | β |
| Multiple version support | β | β | β |
| Best for production | Limited | β | β |
| Best for development | β | β | βββββ |
| Auto security updates | β | β | Manual |
What's Next After Installing Node.js?
Once Node.js is installed and verified, you're ready to start building. Here are some common next steps:
- Initialize a new project:
npm init - Install packages:
npm install express - Run a Node.js application:
node app.js - Set up a process manager like PM2 to keep your app running in production:
npm install -g pm2 - Secure your application with an SSL certificate β AlexHost offers affordable SSL Certificates to protect your Node.js apps in production
- Register a domain for your project through Domain Registration to make your application publicly accessible
Conclusion
You now have a complete understanding of all three methods to install Node.js on Ubuntu 22.04:
- Ubuntu Repositories β Best for quick setups where version specificity isn't critical
- NodeSource PPA β Best for production environments requiring a specific, up-to-date LTS version
- NVM β Best for developers juggling multiple projects with varying Node.js requirements
Each method has its strengths, and the right choice depends entirely on your use case. For most production deployments on a managed VPS Hosting environment, the NodeSource PPA offers the ideal balance of simplicity and version control. For active development workflows, NVM's flexibility is unmatched.
With Node.js properly installed, you're equipped to build fast, scalable server-side applications and tap into the vast npm ecosystem β one of the largest package repositories in the world. Happy coding!
on All Hosting Services