To install Node.js on Linux, follow these methods based on your distribution and needs:
1. Using Default Package Manager
Quick installation, but may provide older versions.
Ubuntu/Debian
sudo apt update
sudo apt install nodejs npm
node -v && npm -v # Verify installation
Fedora
sudo dnf install nodejs npm
node -v && npm -v
CentOS/RHEL
sudo yum install nodejs npm
node -v && npm -v
2. Using NodeSource Repository (Latest Versions)
Recommended for newer Node.js versions.
Ubuntu/Debian
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
sudo apt-get install -y nodejs
Fedora/CentOS/RHEL
curl -fsSL https://rpm.nodesource.com/setup_20.x | sudo bash -
sudo dnf install -y nodejs # For Fedora
# OR
sudo yum install -y nodejs # For CentOS/RHEL
3. Using Node Version Manager (nvm)
Best for managing multiple Node.js versions.
- Install nvm:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
source ~/.bashrc # Restart terminal or reload profile
- Install a Node.js version:
nvm install 20 # Latest v20.x.x
nvm use 20 # Switch to version
- List available versions:
nvm ls-remote # View all versions
nvm ls # View installed versions
4. Using Snap (Universal Packages)
Works on Snap-supported systems (e.g., Ubuntu).
sudo snap install node --classic --channel=20
Verify Installation
Check installed versions:
node -v # Should return e.g., v20.x.x
npm -v # Should return e.g., 10.x.x
Additional Notes
- Global npm Packages: Avoid
sudo
with npm. Usenvm
or configure a user directory. - Build Tools: Required for compiling native modules (e.g.,
node-gyp
):
sudo apt install build-essential # Ubuntu/Debian
sudo dnf groupinstall "Development Tools" # Fedora/CentOS
Troubleshooting
- Command Not Found: Ensure paths are set correctly (especially after using
nvm
). - Version Conflicts: Remove existing Node.js installations before using a new method:
sudo apt purge nodejs npm # Ubuntu/Debian
sudo dnf remove nodejs # Fedora/CentOS
Choose the method that best fits your workflow. For most users, NodeSource or nvm is recommended for up-to-date versions.
Difference Between Installing Node.js Using apt and NVM on Linux
Package Manager (apt) Installation
Advantages:
- Simplicity: One-line command installation
- System-wide access: Available to all users on the system
- Dependency management: Automatically handles dependencies
- Stability: Installs stable, tested versions for your distribution
Disadvantages:
- Outdated versions: Repository versions are often behind the latest Node.js releases
- Single version: Only one Node.js version can be installed system-wide
- Root privileges: Requires sudo access for installation and updates
- Update limitations: Tied to your distribution’s update cycle
NVM (Node Version Manager) Installation
Advantages:
- Multiple versions: Install and switch between different Node.js versions
- User-specific: Doesn’t require root privileges
- Project isolation: Use different Node.js versions for different projects
- Latest releases: Easy access to the newest Node.js versions
- Version-specific commands: Run commands with specific Node.js versions
- Easy upgrades/downgrades: Switch versions with a simple command
Disadvantages:
- User-specific only: Not system-wide by default
- Shell configuration: Requires modifications to shell initialization files
- Extra layer: Adds complexity compared to direct installation
- Manual PATH setup: May require manual configuration for some tools
When to Choose Each Method
Use apt when:
- You need a stable system-wide installation
- You’re setting up a production server with fixed Node.js requirements
- You prefer simplicity over version flexibility
- You’re fine with potentially older Node.js versions
Use NVM when:
- You’re a developer working on multiple projects
- You need to test code across different Node.js versions
- You want to easily upgrade to the latest releases
- You need to switch between versions regularly
NVM is particularly valuable for development environments, while apt installation may be sufficient for simpler use cases or production servers with stable version requirements.
Can I Install node.js Without Installing Npm?
Yes, you can install Node.js without npm, but it requires some specific approaches since the standard installation methods typically bundle them together. Here are your options:
1. Binary Installation with Manual Removal
You can download the Node.js binary distribution and remove npm afterward:
# Download and extract Node.js binary
VERSION=20.11.1
wget https://nodejs.org/dist/v$VERSION/node-v$VERSION-linux-x64.tar.xz
tar -xf node-v$VERSION-linux-x64.tar.xz
sudo mv node-v$VERSION-linux-x64 /opt/nodejs
# Add Node.js to PATH
echo 'export PATH=$PATH:/opt/nodejs/bin' >> ~/.bashrc
source ~/.bashrc
# Optionally remove npm
rm -rf /opt/nodejs/lib/node_modules/npm
rm /opt/nodejs/bin/npm
rm /opt/nodejs/bin/npx
2. Building from Source
You can compile Node.js from source with npm disabled:
# Install build dependencies (Ubuntu/Debian example)
sudo apt install -y python3 make g++
# Download and extract source
wget https://nodejs.org/dist/v20.11.1/node-v20.11.1.tar.gz
tar -xzf node-v20.11.1.tar.gz
cd node-v20.11.1
# Configure without npm
./configure --without-npm
# Build and install
make -j$(nproc)
sudo make install
3. Using Docker
Create a minimal Node.js Docker image without npm:
FROM alpine:3.18
RUN apk add --no-cache nodejs --repository="http://dl-cdn.alpinelinux.org/alpine/edge/main/"
RUN rm -rf /usr/lib/node_modules
Important Considerations
- Package Management: Without npm, you’ll need alternative ways to manage dependencies
- Global Tools: Many Node.js utilities rely on npm for installation
- Script Running: Without npm, you can’t use npm scripts defined in package.json
- Updates: Managing updates becomes more manual