The Simple Guide to Installing Node.js using APT and NodeSource

The Simple Guide to Installing Node.js using APT and NodeSource

Installing Node.js is a crucial step for developers looking to leverage its powerful features and vast ecosystem. In this comprehensive guide, we will walk you through the process of installing Node.js using APT (Advanced Package Tool) and NodeSource. By following these straightforward instructions, you'll be equipped with the latest (LTS or current) version of Node.js, supporting a great development experience and access to a wide range of Node.js libraries and frameworks.

Installation

Binary distributions of Node.js are provided by NodeSource. Installing Node.js on Debian Linux via the NodeSource distributions is an officially supported method.

First, install curl.

sudo apt update
sudo apt install -y curl

Assume the root user.

sudo -s

Install Node.js via NodeSource.

curl -fsSL https://deb.nodesource.com/setup_18.x | bash - && apt-get install -y nodejs

The above command will install Node.js version 18.x, which was the latest LTS release as of this writing. If a new release is available, or you want to use another version, simply change 18 to the major version number you wish to install.

Exit the root user shell.

exit

Make sure Node.js is installed correctly by getting its version number.

node -v

It should display something like:

v18.16.1

The exact version number might vary depending on which version you installed and/or when you are reading this guide.

Upgrading

After following the steps above, minor and patch upgrades to the version of Node.js you installed can be managed via APT. You can update sources like so:

sudo apt update

With that done, your operating system is now aware of any updates to Node.js. You can upgrade your system like so:

sudo apt upgrade

This will upgrade all packages on your system as usual, which now includes Node.js.

Upgrading Major Versions

Since the source list we told APT about only includes version Node.js 18 (or whichever version you chose), it will only upgrade Node.js within that major release. This is a safe practice to adhere to. However, if you are ready to upgrade to a newer major release, follow the steps below.

First, remove the current version of Node.js.

sudo apt remove nodejs

Remove the NodeSource APT list for the current version.

sudo rm /etc/apt/sources.list.d/nodesource.list

Set up the new source and install Node.js.

curl -fsSL https://deb.nodesource.com/setup_20.x | bash - && apt-get install -y nodejs

Notice the 20 in the command above. This means APT will now manage Node.js versions 20.x from now on. Change 20 to whichever version you want to install.

Conclusion

You are now ready to run and serve Node.js apps. You may choose to write and/or run completely custom Node.js apps, or you may choose to go with a framework like Next.js.

Installing Node.js and managing it using APT and NodeSource is a straightforward process that allows developers to quickly set up a reliable and up-to-date Node.js environment. By following the steps outlined in this guide, you can easily install Node.js on your system, ensuring you have access to the latest stable features and improvements. With Node.js in place, you can unleash the full potential of JavaScript, build scalable applications, and tap into the vast ecosystem of Node.js packages.

Cover photo by simmel on Unsplash.