Setting up Node.js and NPM on a Linux System
Setting Up Node.js and NPM on a Linux System
In this tutorial, we will walk you through the process of setting up Node.js and NPM on a Linux system. If you're using a Linux distribution like Ubuntu, Fedora, or Debian, this guide will help you get started with Node.js development. Setting up Node.js on Linux provides you with a powerful and flexible environment to create full stack React applications. This tutorial was created in 2016, and any significant updates have been included to keep the information current.
Step 1: Update Your Package Manager
Before installing Node.js, it’s a good practice to update your package manager to ensure you have the latest versions available. Open your terminal and enter the following commands:
sudo apt update
sudo apt upgrade
These commands will update your system’s package information and upgrade the installed packages to their latest versions.
Critical Update (2024): It is now recommended to use
apt full-upgrade
instead ofapt upgrade
for more comprehensive package upgrades that involve removing or updating dependencies.
Step 2: Installing Node.js and NPM
You have several options for installing Node.js on Linux, including using the package manager, a NodeSource repository, or nvm (Node Version Manager).
Method 1: Installing Node.js from the Default Repositories
The simplest way to install Node.js is from the Linux package manager.
-
Install Node.js and NPM:
sudo apt install -y nodejs npm
-
Verify the Installation:
node -v npm -v
These commands will display the installed versions of Node.js and NPM, confirming a successful installation.
Method 2: Installing Node.js via NodeSource
For the latest versions, it’s often better to use the NodeSource repository.
-
Add NodeSource Repository:
curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash -
Replace
16.x
with the version you want to install (usesetup_18.x
for the latest LTS). -
Install Node.js:
sudo apt install -y nodejs
Critical Update (2024): It’s now common to use Node.js version 18 or higher for full compatibility with the latest JavaScript features and security updates.
Step 3: Using Node Version Manager (nvm)
Another popular method for installing Node.js is by using nvm. This allows you to manage multiple versions of Node.js and switch between them easily.
-
Install nvm:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
-
Load nvm (you may need to restart your terminal or run the following command to load nvm):
source ~/.nvm/nvm.sh
-
Install Node.js using nvm:
nvm install 16
Replace
16
with the version number you want to install. -
Verify the Installation:
node -v npm -v
Critical Update (2024): nvm is now the recommended way to manage Node.js versions on Linux, especially if you work on multiple projects requiring different versions of Node.js.
Step 4: Configuring Node.js for Development
After installing Node.js and NPM, there are a few steps you can take to configure your development environment:
-
Global Packages: Install packages like nodemon and npm-check-updates globally to help with development tasks.
npm install -g nodemon npm-check-updates
-
Permissions Issue: On Linux, you might encounter permission issues when installing packages globally. You can either use nvm, which avoids this problem entirely, or fix permissions by setting up npm to use a directory in your home folder for global installs.
Conclusion
Setting up Node.js and NPM on a Linux system provides a flexible and powerful environment for building modern applications. In this tutorial, you learned different ways to install Node.js, including the default repositories, NodeSource, and using nvm for version management. We also covered some essential configurations to get your environment ready for development.
Linux is a great platform for development, and with Node.js set up, you are now ready to start building scalable, full stack React applications. Remember, it's best to use nvm for managing Node.js versions, as it makes switching between different projects and maintaining compatibility much easier.
Ready to Level Up Your Skills?
Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!
Enroll Now for Free!