Node.js is a popular tool for running JavaScript on servers. It lets developers use the same language for both front-end and back-end work. This makes building web apps easier and faster.
Installing Node.js on Linux is simple. You can add Node.js to most Linux systems in just a few steps using the command line. There are several ways to do this, depending on your Linux version.
Node.js works well on Linux for running server-side code. It’s great for making fast, scalable network apps. Many big companies use Node.js for their backends. Learning how to install it is the first step to using this powerful tool.
Prerequisites
Before installing Node.js on Linux, you need to meet certain software requirements and understand Node.js versions. These steps will help you prepare your system for a smooth installation process.
Software Requirements
To install Node.js on Linux, you need a few basic tools. First, make sure you have a Linux system with admin access. You’ll use the command line, so get familiar with it.
Most Linux systems come with these tools:
- A terminal application
- A package manager (like apt for Ubuntu)
- Sudo privileges
If you’re using Ubuntu, you already have these. For other Linux versions, check your system docs.
Understanding Node.js and Its Versions
Node.js has two main version types: LTS and Current. LTS stands for Long Term Support. It’s stable and good for most users. Current versions have the newest features but might be less stable.
When picking a version, think about what you need:
- LTS: Best for most projects. It gets bug fixes for a long time.
- Current: Good if you want the latest features.
You can find the version numbers on the Node.js website. They use even numbers (like 14, 16, 18) for LTS versions. Odd numbers are for Current versions. Pick the one that fits your needs best.
Setting Up Node.js
Node.js can be set up on Linux using different methods. Each approach has its own benefits for installing and managing Node.js versions.
Using Package Managers
Package managers offer an easy way to install Node.js. They handle dependencies and updates automatically.
On Ubuntu and Debian, you can use apt:
sudo apt update
sudo apt install nodejs npm
For CentOS or Fedora, use yum or dnf:
sudo yum install nodejs
or
sudo dnf install nodejs
After installation, check the version:
node --version
npm --version
These commands show if Node.js and npm were installed correctly.
Node Version Manager (NVM)
NVM lets you install and switch between multiple Node.js versions. This is helpful for testing code with different Node.js versions.
To install NVM:
- Download the install script:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
- Close and reopen your terminal.
- Install Node.js:
nvm install node
- Use a specific version:
nvm use 14.17.0
NVM makes it easy to change Node.js versions as needed for different projects.
Nodesource Repository
Nodesource provides up-to-date Node.js versions for many Linux distributions. It’s a good option for getting the latest stable release.
To use Nodesource:
- Run this command to add the repository:
curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
- Install Node.js:
sudo apt-get install -y nodejs
This method ensures you get a recent Node.js version that is tested and stable.
Installation Steps
Node.js can be installed on Linux systems using different methods. Each approach has its own benefits. Let’s look at the main ways to get Node.js up and running on your Linux machine.
Installing Node.js Using Apt
The apt package manager offers a simple way to install Node.js on Ubuntu and Debian-based systems. First, update the package list:
sudo apt update
Then install Node.js and npm:
sudo apt install nodejs npm
This installs the latest version in the Ubuntu repositories. It may not be the newest Node.js release. To check the installed version, run:
node -v
npm -v
Node.js from Nodesource Repository
For newer Node.js versions, use the Nodesource repository. Start by getting the setup script:
curl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
Replace “14.x” with your desired version. Then install:
sudo apt-get install -y nodejs
This method installs both Node.js and npm. Verify the installation:
node -v
npm -v
Using NVM to Install Node.js
Node Version Manager (NVM) lets you install and manage multiple Node.js versions. To install NVM, run:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
Close and reopen your terminal. Then install Node.js:
nvm install node
This installs the latest stable version. To use a specific version:
nvm install 14.17.0
Switch between versions with:
nvm use 14.17.0
Running Node.js Post-Installation
After installing Node.js, you can start using it for JavaScript development. Create a file named “app.js” with this content:
console.log("Hello from Node.js!");
Run it with:
node app.js
You should see the message printed to the console. This confirms Node.js is working correctly on your Linux system.
Managing Node.js Versions
Node Version Manager (NVM) lets you install and use different Node.js versions. This tool helps you switch between versions and remove old ones.
Listing Installed Node.js Versions
To see all Node.js versions on your system, use NVM. Open a terminal and type:
nvm ls
This shows a list of installed versions. The current active version has an arrow next to it. To check your active version, you can also use:
node -v
This command prints the version number of Node.js you’re using right now.
Switching Between Node.js Versions
NVM makes it easy to change Node.js versions. To switch, use this command:
nvm use <version>
Replace with the one you want to use. For example:
nvm use 14.17.0
You can also use aliases like ‘node’ for the latest version or ‘lts’ for the latest LTS version. To set a default version, use:
nvm alias default <version>
This sets the version that loads when you open a new terminal.
Removing Unwanted Node.js Versions
As you add new versions, you may want to remove old ones. To uninstall a version, use:
nvm uninstall <version>
For example:
nvm uninstall 12.18.3
This removes the specified version from your system. It’s a good idea to keep the versions you need for your projects and remove others to save space.
NVM handles the removal process for you. It takes care of cleaning up files and folders linked to that version.
Additional Configurations and Usage
After installing Node.js, you can set up custom configurations and use it in different ways. This includes changing npm settings, adding Node.js to your system path, and using it with web servers.
Setting Global npm Configuration
npm lets you change its default settings. You can set a new location for global packages. This helps keep your system clean.
To change the global package folder:
- Make a new directory for npm packages
- Tell npm to use this new folder
Here’s how:
mkdir ~/.npm-global
npm config set prefix '~/.npm-global'
Now npm will install global packages to this folder. You’ll need to add it to your PATH. Open your .bashrc
file and add this line:
export PATH=~/.npm-global/bin:$PATH
This change lets you use global packages from any folder.
Environmental Variables and .bashrc
The .bashrc
file sets up your command line when you start it. You can add Node.js settings here.
To add Node.js to your PATH:
- Open
.bashrc
with a text editor - Add this line at the end:
export PATH=$PATH:/path/to/node
Replace “/path/to/node” with where Node.js is on your system.
You can also set other Node.js options in .bashrc
. For example, to set the Node.js environment:
export NODE_ENV=development
This tells Node.js you’re working on a project, not running it for real users.
Node.js as Part of a Web Server
Node.js works well for making web servers. It can handle many users at once.
To make a simple web server with Node.js:
- Create a new file called
server.js
- Add this code:
const http = require('http');
const server = http.createServer((req, res) => {
res.writeHead(200, {'Content-Type': 'text/plain'});
res.end('Hello World!');
});
server.listen(3000, () => {
console.log('Server running on port 3000');
});
- Run the server with
node server.js
This code makes a server that says “Hello World!” when you visit it in a web browser.
You can also use Node.js with other web server software. For example, you can use Node.js with Nginx as a reverse proxy. This setup can handle more traffic.
Troubleshooting Common Issues
Installing Node.js on Linux can sometimes hit a few snags. Here are some common problems and how to fix them.
Permission Problems
When installing Node.js, you might see errors about not having the right permissions. This often happens when trying to install globally.
To fix this:
- Use sudo before your install command
- Change ownership of the npm directories
Here’s how to change ownership:
sudo chown -R $(whoami) ~/.npm
sudo chown -R $(whoami) /usr/local/lib/node_modules
This lets you install Node.js without root access.
Network Issues
Sometimes network problems can stop Node.js from installing. Check these things:
- Make sure you’re online
- Test your internet speed
- Check if you’re behind a firewall
If you’re using a proxy, set it up for npm:
npm config set proxy http://proxy.company.com:8080
npm config set https-proxy http://proxy.company.com:8080
Try using a different DNS server if you’re having trouble reaching the Node.js website.
Version Conflicts
Different projects might need different versions of Node.js. This can cause conflicts.
To manage multiple versions:
- Use NVM (Node Version Manager)
- Install specific versions with nvm
Here’s how to use NVM:
nvm install 14.17.0
nvm use 14.17.0
Check installed versions with nvm ls
. Switch between them easily for each project.
If you’re using an older Linux version, make sure it supports the Node.js version you want to install.
Frequently Asked Questions
Installing Node.js on Linux can be simple. Here are answers to common questions about getting Node.js up and running on different Linux systems.
What are the steps to install Node.js on an Ubuntu system?
To install Node.js on Ubuntu:
- Open a terminal
- Update the package list:
sudo apt update
- Install Node.js:
sudo apt install nodejs
- Check the version:
node --version
This process installs Node.js quickly on Ubuntu systems.
How can I install a specific version of Node.js using NVM on Linux?
NVM lets you install different Node.js versions. To use it:
- Install NVM
- Run:
nvm install <version>
- Switch versions with:
nvm use <version>
NVM makes it easy to manage multiple Node.js versions on one system.
What is the process to install npm alongside Node.js on a Linux machine?
npm usually comes with Node.js. If not:
- Update packages:
sudo apt update
- Install npm:
sudo apt install npm
- Verify:
npm --version
This adds npm to your system if it’s not already there.
Can you provide guidance on running Node.js in a Linux environment?
To run Node.js on Linux:
- Open a terminal
- Type
node
to start the Node.js shell - Write JavaScript code directly
- Or run a file with:
node filename.js
This allows quick testing and running of Node.js scripts.
Is there a way to install Node.js globally on a Linux distribution?
Yes, you can install Node.js globally:
- Use a package manager like apt
- Run:
sudo apt install nodejs
- This makes Node.js available to all users
Global installs are useful for system-wide access to Node.js.
What are the command-line instructions for updating Node.js to the latest version on Linux?
To update Node.js on Linux:
- If using a package manager:
sudo apt update && sudo apt upgrade nodejs
- With NVM:
nvm install node --latest-npm
- Check the new version:
node --version
These steps ensure you have the newest Node.js version on your Linux system.