How to install nodejs on linux?
- User
How to install nodejs on linux?
- Assistant
Installing Node.js on a Linux system can be accomplished through various methods. I will outline some of the most commonly used approaches: using the package manager for your distribution, using the Node Version Manager (nvm), and downloading from the official Node.js website.
Method 1: Using the Package Manager
On Debian-based distributions (e.g., Ubuntu):
- Update the package index:
bash
sudo apt update - Install Node.js:
bash
sudo apt install nodejs - Install npm (Node Package Manager):
bash
sudo apt install npm
You can alternatively use the NodeSource repository to get a more recent version of Node.js:
-
Add the NodeSource repository:
bashcurl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash -Replace
16.xwith the version you want (e.g.,14.xor18.x). -
Install Node.js:
bashsudo apt-get install -y nodejs
On RHEL-based distributions (e.g., CentOS, Fedora):
-
Add the NodeSource repository:
bashcurl -fsSL https://rpm.nodesource.com/setup_16.x | sudo bash -Replace
16.xwith the desired version. -
Install Node.js and npm:
bashsudo yum install nodejs
Method 2: Using Node Version Manager (nvm)
nvmis a version manager for Node.js, which allows you to install and switch between multiple versions of Node.js.-
Install nvm:
bashcurl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.2/install.sh | bash
- Update the package index: