How to Install and Use Node.js & npm on Debian & Ubuntu
Node.js is a JavaScript runtime built on Chrome’s V8 engine that allows you to run JavaScript on the server side. npm is the default package manager for Node.js. This guide covers installing and using Node.js and npm on Debian and Ubuntu.
Check Current Node.js Version
Check if Node.js is already installed:
node --version
node -v
npm --version
npm -v
Method 1: Install from Debian Repository (Easiest)
Step 1: Update Package Lists
sudo apt update
Step 2: Install Node.js
sudo apt install -y nodejs
Check versions:
node -v
npm -v
Method 2: Install Using NodeSource Repository (Recommended)
Install specific Node.js versions (20.x, 18.x, 16.x):
Step 1: Add NodeSource Repository
For Node.js 20.x (LTS):
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
For Node.js 18.x:
curl -fsSL https://deb.nodesource.com/setup_18.x | sudo -E bash -
Step 2: Install Node.js
sudo apt install -y nodejs
Step 3: Verify Installation
node -v
npm -v
Method 3: Install Using nvm (For Multiple Versions)
nvm allows you to manage multiple Node.js versions:
Step 1: Install nvm
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
Step 2: Configure nvm
Add to ~/.bashrc:
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
[ -s "$NVM_DIR/bash_completion" ] && \. "$NVM_DIR/bash_completion"
source ~/.bashrc
Step 3: Install Node.js Versions
nvm install 20
nvm install 18
nvm install --lts
nvm Commands
- List installed versions:
nvm list - Use specific version:
nvm use 20 - Set default version:
nvm alias default 20 - Uninstall version:
nvm uninstall 18
Using Node.js
Run JavaScript File
Create a test file:
nano hello.js
console.log("Hello, World!");
const add = (a, b) => a + b;
console.log("2 + 3 =", add(2, 3));
const os = require('os');
console.log("Platform:", os.platform());
node hello.js
Start REPL
Interactive Node.js console:
node
REPL commands:
.help– Show help.clear– Clear context.exit– Exit REPL
Simple HTTP Server
const http = require('http');
const server = http.createServer((req, res) => {
res.writeHead(200, { 'Content-Type': 'text/plain' });
res.end('Hello from Node.js!');
});
server.listen(3000, () => {
console.log('Server running at http://localhost:3000/');
});
node server.js
Using npm – Package Manager
Initialize a Project
Create package.json:
npm init -y
Install a Package
npm install lodash
Install as Dev Dependency
For development only (testing, etc.):
npm install --save-dev jest
Install Global Package
Available system-wide:
npm install -g pm2
Uninstall Package
npm uninstall lodash
List Installed Packages
npm list
List Global Packages
npm list -g --depth=0
Update Packages
npm update
Run Scripts
Run scripts from package.json:
npm run build
npm run dev
npm start
npm test
Common npm Packages
- express – Web framework
- react – UI library
- vue – Progressive framework
- lodash – Utility library
- axios – HTTP client
- nodemon – Auto-restart server
- pm2 – Process manager
- typescript – TypeScript compiler
- jest – Testing framework
- webpack – Bundler
Example: Express Web Server
mkdir myapp
cd myapp
npm init -y
npm install express
nano index.js
const express = require('express');
const app = express();
const port = 3000;
app.get('/', (req, res) => {
res.send('Hello from Express!');
});
app.listen(port, () => {
console.log(`Server at http://localhost:${port}`);
});
node index.js
npm Commands Reference
npm install package– Install packagenpm install package@version– Install specific versionnpm install -g package– Install globallynpm uninstall package– Remove packagenpm list– List packagesnpm update– Update packagesnpm search package– Search packagesnpm docs package– View docs
Troubleshooting
npm: command not found
Reinstall Node.js and npm:
sudo apt remove --purge nodejs npm
sudo apt autoremove
sudo apt install -y nodejs
Permission Errors (EACCES)
Fix npm global directory:
mkdir ~/.npm-global
npm config set prefix '~/.npm-global'
echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.bashrc
source ~/.bashrc
Slow npm Install
Use a different registry:
npm config set registry https://registry.npmmirror.com
Conclusion
You now have Node.js and npm installed! Key commands to remember:
- node file.js – Run JavaScript file
- npm install package – Install package
- npm install -g package – Install globally
- npm init -y – Create package.json
- npm run dev – Run dev script
nodemon during development to auto-restart your server when files change.