Quick Answer: Best method: use nvm. Install nvm:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.3/install.sh | bash. Then:nvm install --lts. Verify:node -v && npm -v.
Which Method to Use
| Method | Best For | Multiple Versions | Sudo Required |
|---|---|---|---|
| nvm (recommended) | Development | Yes | No |
| NodeSource repo | Production servers | No | Yes |
| Official installer | Windows/macOS beginners | No | Yes |
| Docker | Isolated environments | Yes (per container) | No |
| apt (default) | Not recommended | No | Yes |
Why not apt install nodejs? Ubuntu/Debian ship very old Node.js versions. Ubuntu 22.04 ships Node 12 — many modern packages need Node 18+.
Method 1: nvm (Recommended)
nvm (Node Version Manager) lets you install and switch between multiple Node.js versions without sudo.
Install nvm
# Install nvm
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.3/install.sh | bash
# Reload shell
source ~/.bashrc
# or for zsh:
source ~/.zshrc
# Verify
nvm --version
Install Node.js
# Install latest LTS (recommended)
nvm install --lts
# Install latest version
nvm install node
# Install specific version
nvm install 20
nvm install 18.19.0
# Verify
node -v # e.g., v20.12.0
npm -v # e.g., 10.5.0
Switch Versions
# List installed versions
nvm ls
# Switch to a version
nvm use 20
nvm use 18
# Set default version
nvm alias default 20
# Use version for current project only (.nvmrc)
echo "20" > .nvmrc
nvm use # Reads from .nvmrc
# Uninstall a version
nvm uninstall 18
nvm on macOS
Same as Linux — nvm works natively:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.3/install.sh | bash
source ~/.zshrc
nvm install --lts
nvm on Windows
Use nvm-windows — a separate project:
- Download installer from the releases page
- Run the installer
- Open a new terminal:
nvm install lts
nvm use lts
node -v
Method 2: NodeSource Repository (Production Servers)
For servers where you want one stable version managed by apt:
# Add NodeSource GPG key and repository (Node.js 20 LTS)
curl -fsSL https://deb.nodesource.com/gpgkey/nodesource-repo.gpg.key | sudo gpg --dearmor -o /etc/apt/keyrings/nodesource.gpg
echo "deb [signed-by=/etc/apt/keyrings/nodesource.gpg] https://deb.nodesource.com/node_20.x nodistro main" | sudo tee /etc/apt/sources.list.d/nodesource.list
# Install
sudo apt update
sudo apt install -y nodejs
# Verify
node -v
npm -v
For Node 22, change node_20.x to node_22.x in the echo command above.
Method 3: Official Installer
macOS
Download from nodejs.org or use Homebrew:
# Using Homebrew
brew install node
# Specific version
brew install node@20
# Verify
node -v
npm -v
Windows
- Download from nodejs.org — choose LTS
- Run the installer (check "Add to PATH")
- Open Command Prompt or PowerShell:
node -v
npm -v
Method 4: Docker
Run Node.js in a container without installing it on your system:
# Run Node.js interactively
docker run -it node:20 node
# Run a script
docker run -v $(pwd):/app -w /app node:20 node script.js
# For a project with package.json
docker run -v $(pwd):/app -w /app node:20 sh -c "npm install && npm start"
Dockerfile for Node.js Projects
FROM node:20-alpine
WORKDIR /app
COPY package*.json ./
RUN npm install
COPY . .
EXPOSE 3000
CMD ["node", "server.js"]
npm Basics
npm (Node Package Manager) comes with Node.js.
# Initialize a project
npm init -y
# Install a package (project-local)
npm install express
# Install globally
npm install -g nodemon
# Install dev dependency
npm install --save-dev jest
# Remove a package
npm uninstall express
# Update all packages
npm update
# List installed packages
npm list
npm list -g # Global packages
# Check for outdated packages
npm outdated
# Run scripts (from package.json)
npm start
npm test
npm run build
npx — Run Without Installing
# Run a package without installing it
npx create-react-app my-app
npx serve ./build
npx ts-node script.ts
Yarn (Alternative to npm)
# Install Yarn
npm install -g yarn
# Or via corepack (Node 16.10+)
corepack enable
corepack prepare yarn@stable --activate
# Yarn commands
yarn init
yarn add express
yarn add --dev jest
yarn remove express
yarn install
yarn upgrade
Common Issues
| Problem | Fix |
|---|---|
node: command not found |
Shell not reloaded. Run source ~/.bashrc or open new terminal |
npm WARN old lockfile |
Normal warning. Run npm install to update |
EACCES permission denied |
Don't use sudo with npm. Use nvm instead |
node: --openssl-legacy-provider |
Node 17+ changed OpenSSL. Add flag or downgrade to Node 16 |
npm ERR! code ENOENT |
Missing package.json. Run npm init -y first |
| Old Node.js from apt | Don't use apt install nodejs. Use nvm or NodeSource repo |
gyp ERR! build error |
Install build tools: sudo apt install build-essential python3 |
Fix npm Global Permissions
If you get EACCES errors with global npm installs:
# Option 1: Use nvm (best solution — no sudo needed)
# Option 2: Change npm's default directory
mkdir ~/.npm-global
npm config set prefix '~/.npm-global'
echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.bashrc
source ~/.bashrc
Check Your Installation
# Node.js version
node -v
# npm version
npm -v
# Quick test
node -e "console.log('Node.js is working!')"
# Check where Node is installed
which node
which npm
Related Guides
- Docker Cheat Sheet — containerize Node apps
- Docker Compose Examples — deploy Node apps
- Nginx Reverse Proxy — proxy Node apps
- Bash Cheat Sheet — shell scripting
- Git Cheat Sheet — version control