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.
First, install curl.
sudo apt update
sudo apt install -y curl
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.
root user shell.
Make sure Node.js is installed correctly by getting its version number.
It should display something like:
The exact version number might vary depending on which version you installed and/or when you are reading this guide.
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
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.
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.