Node.js v18 on Ubuntu 22.04
Problem Statement
When trying to install Node.js v18 on Ubuntu 22.04 using official repository scripts, you may unexpectedly receive an older version (v12) instead. This occurs because:
- The default Ubuntu repositories contain outdated Node.js versions
- Conflicts with existing Node.js installations can prevent the correct version from installing
- Repository configuration steps may not execute properly
This causes compatibility issues with modern JavaScript frameworks like React that require at least Node.js v14.
Recommended Solutions
Method 1: Install via Nodesource Repository
Optimal for system-wide installations and production environments:
# 1. Remove existing conflicting installations
sudo apt purge nodejs -y
sudo apt autoremove -y
# 2. Install dependencies
sudo apt update
sudo apt install -y ca-certificates curl gnupg
# 3. Add nodesource repository
sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | sudo gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg
# 4. Set up repository for v18
echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_18.x nodistro main" | sudo tee /etc/apt/sources.list.d/nodesource.list
# 5. Install Node.js
sudo apt update
sudo apt install nodejs -y
Key advantages:
- Maintained by Node.js official partners
- Automatic security updates via APT
- System-wide availability for all users
- No memory limitations
Verification steps
After installation, confirm the version:node --version
# Should return: v18.x.x
Method 2: Install Using Node Version Manager (nvm)
Best for development environments with multiple Node.js versions:
# 1. Install nvm
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash
# 2. Reload shell profile
source ~/.bashrc # or source ~/.zshrc for zsh users
# 3. Install Node.js 18
nvm install 18
# 4. Set as default version
nvm alias default 18
Key advantages:
- Switch between Node.js versions instantly
- No sudo permissions required
- Version-specific dependencies
- Perfect for per-project configurations
Why Your Initial Approach Failed
Your original installation method likely failed because:
- Old Node.js installations create package conflicts (
apt
prioritizes existing packages) - Ubuntu's default repositories contain older Node.js versions that take precedence
- Missing GPG key verification leads to unreliable repository setup
Additional Recommendations
Important Configuration
- Always purge old Node.js installations with
sudo apt purge nodejs && sudo apt autoremove
before new installations - Verify repository setup by checking
/etc/apt/sources.list.d/nodesource.list
Version Management
When using nvm:
# List available remote versions
nvm ls-remote
# Switch between installed versions
nvm use 16
# Set project-specific Node version (creates .nvmrc file)
nvm use 18 --default
Verification and Troubleshooting
Confirm successful installation with:
node -v # Should return v18.x or higher
npm -v # Should show corresponding npm version
Common issues solutions:
- Command not found errors: Reload shell with
source ~/.bashrc
or reboot - Permission errors: Use
sudo
only for system-wide installations (Method 1) - Version mismatches: Remove conflicting installations with
sudo apt purge nodejs npm
Uninstallation Steps
For Method 1:
sudo apt purge nodejs -y
sudo rm /etc/apt/sources.list.d/nodesource.list
sudo rm /etc/apt/keyrings/nodesource.gpg
sudo apt update
For Method 2:
nvm uninstall 18
# To remove nvm: delete ~/.nvm directory
Choose Method 1 for production servers requiring consistent system-wide versions. Prefer Method 2 for development machines needing version flexibility. Both approaches resolve the core issue of installing Node.js v18 while avoiding Ubuntu's default outdated packages.