Keeping your development environment up-to-date is crucial for taking advantage of the latest features, performance improvements, and security patches. When it comes to Node.js and NPM, using a Personal Package Archive (PPA) is the recommended way to get the most recent stable versions on Ubuntu, rather than relying on the potentially outdated versions in the default Ubuntu repositories.
This guide will walk you through the process of installing the latest Node.js and NPM on Ubuntu using NodeSource’s PPA.
Why Use NodeSource PPA?
- Latest Versions: Get the newest stable releases of Node.js and NPM directly.
- Consistent Updates: Easily update to future versions using standard
aptcommands. - Reliable: NodeSource PPAs are well-maintained and widely used by the Node.js community.
Step 1: Update Your System
Before installing new software, it’s always a good practice to update your package list and upgrade existing packages to their latest versions.
sudo apt update
sudo apt upgrade -y
Step 2: Install curl (if not already installed)
The curl command is used to download files from the internet. You’ll need it to fetch the NodeSource setup script.
sudo apt install curl -y
Step 3: Add the NodeSource PPA
NodeSource provides separate PPAs for different Node.js release lines (e.g., Node.js 18.x, 20.x, etc.). You should choose the PPA that corresponds to the latest LTS (Long Term Support) or Current stable release you want to install.
For example, to install Node.js 20.x (LTS as of this update), run:
curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash -
If you want to install a different major version (e.g., 18.x), simply replace setup_20.x with setup_18.x in the command.
This command does two things:
- Downloads the NodeSource setup script.
- Runs the script with
sudo -E bash -, which adds the NodeSource PPA to your system’s software sources and updates theaptpackage list.
Step 4: Install Node.js and NPM
Once the PPA is added and your package list is updated, you can install Node.js and NPM. The NodeSource packages include both.
sudo apt install nodejs -y
Step 5: Verify the Installation
After the installation is complete, you can verify that Node.js and NPM are installed correctly and check their versions.
node -v
npm -v
You should see the version numbers corresponding to the Node.js and NPM releases from the PPA you chose.
Step 6: (Optional) Install Build Tools
Some NPM packages require compiling C/C++ addons. To ensure these packages can be installed successfully, it’s good practice to install essential build tools.
sudo apt install build-essential -y
Conclusion
You now have the latest stable version of Node.js and NPM installed on your Ubuntu system, ready for development. Thanks to the NodeSource PPA, keeping your environment updated will be as simple as running sudo apt update && sudo apt upgrade.
Discover more from TCMHACK
Subscribe to get the latest posts sent to your email.
