Edited 8 months ago by ExtremeHow Editorial Team
DebianNode.jsSoftware InstallationDevelopmentProgrammingCLILinuxOpen SourceSystem AdministrationIT
This content is available in 7 different language
Node.js is a popular JavaScript runtime environment that allows you to run JavaScript code on the server side. It is widely used to build scalable network applications and is valuable for developers working in the modern web development ecosystem. If you are using a Debian-based system, you may want to install Node.js to take advantage of its vast ecosystem of libraries and its capabilities. In this guide, we will go into detail about how to install Node.js on a Debian system. We will discuss different methods of installation and provide clear and precise steps for each method.
Before proceeding with the installation of Node.js, it is necessary to update the package index of your system. This update ensures that your package manager is aware of the latest versions of the software available in the repositories. You can update your package list with the following command:
sudo apt update
After updating the package index, it is also good practice to upgrade installed packages to their latest versions. This may take some time depending on the number of packages that need updating. To upgrade your packages, use the command:
sudo apt upgrade
The easiest way to install Node.js on Debian is to use the package manager, APT (Advanced Package Tool) from the official Debian repository. This method is best suited for users who prefer stability and are not concerned about using the latest version of Node.js.
First, check if Node.js is available in Debian's package list:
apt-cache show nodejs
If it is available, you can install Node.js using the following command:
sudo apt install nodejs
Once the installation process is complete, verify that Node.js is installed correctly by checking the version. You can do this by running:
nodejs --version
Note that the package in the default Debian repositories may not be the latest version. If you need the latest version, consider using an alternative method.
If you need the latest version of Node.js provided in the Debian repository, you can use the NodeSource repository. NodeSource is a company that specializes in providing and managing distributions of Node.js.
First, you need to add the NodeSource repository to your system. This can be done by running a script provided by NodeSource. You must determine which version of Node.js you need. Let’s say you need Node.js version 14, you would run:
curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
Wait for the script to finish executing. This script will add the NodeSource signing key to your system and create a new apt sources list file.
Now, you can install Node.js:
sudo apt install nodejs
Again, verify that Node.js is installed by checking the version:
node --version
This will display the version of Node.js that you installed from NodeSource.
When you install Node.js, NPM (Node Package Manager) is also installed. NPM is an important tool for managing JavaScript libraries and frameworks. To verify that NPM is installed correctly, you can check its version using:
npm --version
If the version number is displayed, then NPM is successfully installed on your system.
Node Version Manager (NVM) is a tool that allows developers to install and manage multiple versions of Node.js on a single system. This can be especially useful when testing different projects that require different versions of Node.js. Below are the steps to install Node.js using NVM:
First, download and install NVM using the curl command:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash
After running the script, you may need to close and reopen your terminal. Alternatively, to start using NVM without closing the terminal, source your profile:
source ~/.bashrc // or ~/.zshrc if you are using Zsh
Once NVM is installed, you can now use it to install Node.js. To install the latest version of Node.js, use the command:
nvm install node
If you want a specific version, like Node 14, you can use:
nvm install 14
To confirm the installation and see which version is active, use:
node --version
NVM also provides commands to control which version of Node.js is being used. You can switch between installed versions:
nvm use 14
If you want to set the default version of Node.js so that it is activated whenever you open the shell, use:
nvm alias default 14
After successfully installing Node.js on Debian, you may want to set up additional configurations based on your project requirements.
With npm (Node Package Manager), you can install packages globally with the following command structure:
npm install -g [package-name]
For example, if you wanted to install the globally popular Node.js framework, express
, you could do so like this:
npm install -g express
Creating a simple Node.js application setup involves initializing a project with NPM. This usually involves creating a package.json
file that contains information about the project and any dependencies it requires. Here's a basic example:
npm init -y
This command will generate a basic package.json
file in your current directory. You can then edit this file to include the details of your project.
Let's create a simple web server with Node.js. The following code will help you create a basic server:
const http = require('http');
const hostname = '127.0.0.1';
const port = 3000;
const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World\\n');
});
server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
Save this code in a file named server.js
. Now you can run the server using Node.js:
node server.js
Go to http://127.0.0.1:3000/
in your web browser, and you will see "Hello World!"
In this detailed guide, we explored several ways to install Node.js on a Debian system. We covered installing Node.js through the official Debian repository, using the NodeSource repository for more recent versions, and using NVM for enhanced flexibility in managing Node.js versions. The guide also explained the basics of working with Node.js and NPM, and even showed a small example to help you get started with Node.js development.
By following this guide, you should now be equipped with the knowledge to effectively deploy Node.js on your Debian system and build and debug applications. This baseline setup serves as an initial step to effectively leverage the extensive ecosystem of Node.js packages and community support.
If you find anything wrong with the article content, you can