I remember the first time I tried to build a simple web app on my Debian laptop. I had heard that Node.js and npm were essential tools for modern web development. But when I tried to install them, I got confused by the different methods and versions available. After some trial and error, I finally figured it out. In this guide, I’ll share what I learned so you can install Node.js and npm on Debian without the headaches.
What Are Node.js and npm?
Node.js is a tool that lets you run JavaScript code outside of a web browser. It’s great for building web servers, tools, and applications.
npm stands for Node Package Manager. It’s a tool that helps you install and manage packages (libraries or tools) that you can use in your Node.js projects.
Think of Node.js as the engine, and npm as the toolbox.
Why Use Node.js and npm?
When I started learning JavaScript, I used it only in the browser. But with Node.js, I could write scripts to automate tasks, build servers, and even create desktop apps.
npm made it easy to add features to my projects without writing everything from scratch. Need to connect to a database? There’s a package for that. Want to build a web server? There’s a package for that too.
How to Install Node.js and npm on Debian
There are several ways to install Node.js and npm on Debian. I’ll explain three common methods:
- Using Debian’s default package manager (apt)
- Using NodeSource (for newer versions)
- Using Node Version Manager (nvm)
Method 1: Using apt (Debian’s Package Manager)
This is the simplest method, but it might not give you the latest version.
Steps:
- Open your terminal.
- Update your package list:
sudo apt update
- Install Node.js and npm:
sudo apt install nodejs npm
- Check the installed versions:
node -v npm -v
Note: This method might install an older version of Node.js. If you need a newer version, consider the next method.
Method 2: Using NodeSource (for Newer Versions)
NodeSource provides up-to-date versions of Node.js.
Steps:
- Install curl if you don’t have it:
sudo apt install curl
- Download and run the NodeSource setup script (replace
20.x
with your desired version):curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
- Install Node.js:
sudo apt install nodejs
- Verify the installation:
node -v npm -v
This method gives you a more recent version of Node.js and npm.
Method 3: Using Node Version Manager (nvm)
nvm allows you to install and manage multiple versions of Node.js. This is useful if you work on projects that require different Node.js versions.
Steps:
- Download and install nvm:(Node.js — Run JavaScript Everywhere)
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.3/install.sh | bash
- Close and reopen your terminal, or run:
source ~/.bashrc
- Install the latest LTS (Long Term Support) version of Node.js:
nvm install --lts
- Verify the installation:
node -v npm -v
With nvm, you can switch between Node.js versions easily:
nvm use 14
nvm use 16
Comparing the Methods
Here’s a quick comparison to help you choose:
Method | Pros | Cons |
---|---|---|
apt | Easy to use | Might install older versions |
NodeSource | Provides newer versions | Slightly more complex setup |
nvm | Manage multiple versions | Requires additional setup |
I personally use nvm because I work on different projects that need different Node.js versions. But if you’re just starting out, using apt or NodeSource is perfectly fine.(RoseHosting)
Tips and Reminders
- Always check the Node.js version after installation using
node -v
. - Use
npm -v
to check the npm version. - If you encounter permission issues with npm, consider using
nvm
or avoid installing packages globally. - Keep your Node.js and npm versions updated to benefit from the latest features and security patches.
Conclusion
Installing Node.js and npm on Debian doesn’t have to be complicated. Choose the method that best fits your needs:
- Use
apt
for a quick and easy setup. - Use NodeSource for newer versions.
- Use
nvm
if you need to manage multiple versions.
With Node.js and npm installed, you’re ready to start building powerful web applications and tools. Happy coding!