Install Node.js on Ubuntu
How to Install Node.js on Ubuntu: A Complete Guide
Node.js is a powerful JavaScript runtime that enables server-side execution of JavaScript code. This guide covers multiple installation methods, their pros and cons, and helps you choose the right approach for your needs.
Understanding Node.js Versions
Before installation, understand Node.js versioning:
- LTS (Long Term Support) Versions: Currently 20.x and 18.x - best for production
- Current Version: Latest features but less stable
- Development Version: Experimental features
Method 1: Installing Node.js from Ubuntu Repository
This method uses Ubuntu's default package manager. While convenient, it might not provide the latest version.
Update your system's package index (always start with this):
sudo apt update
Install Node.js and npm packages:
sudo apt install nodejs npm
Verify your installation:
nodejs --version
npm --version
Advantages of this method:
- Simplest installation process
- Automatic security updates through Ubuntu's package manager
- Guaranteed compatibility with Ubuntu's ecosystem
Disadvantages:
- Usually older version of Node.js
- May lack latest features and performance improvements
- Some npm packages might require newer Node.js versions
Method 2: Installing Node.js Using NodeSource Repository
NodeSource maintains up-to-date Node.js binaries. This method provides newer versions while maintaining system package management benefits.
First, install curl if not already installed:
sudo apt install curl
Download and execute the NodeSource setup script:
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
Understanding the command:
-fsSL
: curl flags for silent, show errors, follow redirectssudo -E
: preserve user environment variables
Install Node.js (includes npm):
sudo apt install nodejs
Verify the installation:
node --version
npm --version
Version Selection Guide:
setup_20.x
- Latest LTS version, recommended for most userssetup_18.x
- Previous LTS, still widely supportedsetup_19.x
- Current version, for testing new features
Method 3: Installing Node.js Using Node Version Manager (nvm)
NVM is ideal for developers who need to switch between Node.js versions. It's particularly useful for testing applications across different Node.js versions.
First, install required dependencies:
sudo apt install curl build-essential
Download and run the nvm installation script:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
The script will clone the nvm repository to ~/.nvm and add environment variables to your profile.
Load nvm in your current session:
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"
Common nvm commands:
# List available remote versions
nvm ls-remote
# Install specific version
nvm install 20.11.1
# Install latest LTS version
nvm install --lts
# List installed versions
nvm ls
# Switch to specific version
nvm use 20.11.1
# Set default Node.js version
nvm alias default 20.11.1
Installing Build Tools
Many Node.js packages require compilation. Install essential build tools:
sudo apt install build-essential python3
Additional useful development tools:
sudo apt install git git-core
Advanced npm Configuration
Set up a better npm environment:
# Create global installation directory
mkdir ~/.npm-global
# Configure npm to use new directory
npm config set prefix '~/.npm-global'
# Add to PATH
echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.profile
# Update current session
source ~/.profile
Recommended npm configurations:
# Save exact versions
npm config set save-exact true
# Set default package init values
npm config set init.author.name "Your Name"
npm config set init.author.email "[email protected]"
npm config set init.license "MIT"
# Configure npm to use less output
npm config set loglevel warn
Project-Specific Configuration
Create a project-specific .nvmrc file:
echo "20.11.1" > .nvmrc
# Automatically use project's Node.js version
nvm use
Production Environment Setup
For production servers, set NODE_ENV:
echo 'export NODE_ENV=production' >> ~/.profile
source ~/.profile
Install production monitoring tools:
npm install -g pm2
pm2 startup
Troubleshooting Common Issues
Fix EACCES Errors
# Check npm cache ownership
sudo chown -R $USER:$GROUP ~/.npm
# Clear npm cache
npm cache clean --force
# Reset npm cache
rm -rf ~/.npm
Node Command Not Found
# Create symlink if needed
sudo ln -s $(which nodejs) /usr/bin/node
# Verify PATH
echo $PATH | grep node
Security Best Practices
- Regularly update packages:
npm audit npm update npm audit fix
- Use package-lock.json for consistent installations
- Implement rate limiting in production
- Configure secure HTTP headers
Monitoring and Maintenance
# Check for outdated packages
npm outdated
# List global packages
npm list -g --depth=0
# Monitor running Node.js processes
pm2 monitor
Next Steps
After installation, consider:
- Setting up a development environment (VS Code, Git)
- Installing essential global packages (nodemon, yarn)
- Configuring ESLint and Prettier
- Setting up debugging tools
Additional Resources
- Official Node.js Documentation
- npm Security Best Practices
- Node.js Design Patterns
- Performance Tuning Guidelines