How To Install Node.js on Debian 12
In this tutorial, we will show you how to install Node.js on Debian 12. Node.js has emerged as a powerful and versatile runtime environment for building scalable network applications using JavaScript. Its event-driven, non-blocking I/O model makes it an excellent choice for developing real-time applications, APIs, and microservices. As the demand for Node.js-based solutions continues to grow, developers often need to install and configure Node.js on their preferred operating system, such as Debian 12.
This article assumes you have at least basic knowledge of Linux, know how to use the shell, and most importantly, you host your site on your own VPS. The installation is quite simple and assumes you are running in the root account, if not you may need to add ‘sudo
‘ to the commands to get root privileges. I will show you step-by-step install Node.js on a Debian 12 (Bookworm).
Prerequisites
- A server running one of the following operating systems: Debian 12 (Bookworm).
- It’s recommended that you use a fresh OS install to prevent any potential issues.
- SSH access to the server (or just open Terminal if you’re on a desktop).
- An active internet connection. You’ll need an internet connection to download the necessary packages and dependencies for Node.js.
- A
non-root sudo user
or access to theroot user
. We recommend acting as anon-root sudo user
, however, as you can harm your system if you’re not careful when acting as the root.
Install Node.js on Debian 12 Bookworm
Step 1. Before installing any package, it’s a good practice to update the package lists to ensure you have access to the latest available versions. Open a terminal and run the following command:
sudo apt update sudo apt install build-essential curl gcc g++ make
This command will refresh the repository, allowing you to install the latest versions of software packages.
Step 2. Installing Node.js on Debian 12.
Node.js versions provided by the official Debian repositories might be outdated. To install the latest Node.js release, we’ll use the NodeSource repository. Execute the following commands to add the repository:
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo bash -
Now that we’ve added the NodeSource repository, we can install Node.js using the package manager:
sudo apt install nodejs
To confirm the successful installation of Node.js, check its version:
node -v
If you work on multiple projects with different Node.js version requirements, it’s wise to use Node Version Manager (NVM). With NVM, you can easily switch between Node.js versions. Execute the following command to install NVM:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
Close and reopen your terminal to start using NVM, then install a specific Node.js version:
nvm install 16.4.0
Double-check the installed Node.js version to ensure everything is working as expected:
node -v
Step 3. Running a Sample Node.js Application.
Let’s run a simple example to confirm that Node.js is up and running. Create a new file named “hello.js” and add the following code:
console.log("Hello, Node.js!");
Save the file, then run the application using the Node.js command:
node hello.js
The console should display “Hello, Node.js!” if everything is in order.
Step 4. Troubleshooting Tips.
- Permission Issues: If you encounter permission errors during installation or while running Node.js applications, avoid using the “
sudo
” command unnecessarily. Instead, consider changing the ownership of the directory where you plan to run your applications:
sudo chown -R $USER:$(id -gn $USER) /path/to/your/project
- Node.js Not Found: If the “
node
” command is not recognized, ensure that the Node.js binary path is added to your system’s PATH environment variable. Add the following line to your “
:/.bashrc" or "/.bash_profile" file
export PATH=$PATH:/usr/bin/node
Remember to keep your Node.js environment up to date and explore the vast resources available to unleash the true potential of this remarkable technology.
Congratulations! You have successfully installed Node.js. Thanks for using this tutorial to install Node.js on Debian 12 Bookworm. For additional help or useful information, we recommend you check the official Node.js website.