Install Node.js on Fedora
Today we talk about installing Node.js on Fedora, one of the most developer-friendly Linux distributions available. Node.js is a powerful JavaScript runtime that allows you to run server-side JavaScript code, build network applications, and much more. It's an essential tool for modern web development, and Fedora's robust package management system makes it an excellent environment for Node.js development.
In this tutorial, we'll walk through the process of installing Node.js on Fedora, ensuring you have the latest features and updates. We'll cover multiple installation methods, how to manage Node.js versions, and best practices for maintaining your Node.js environment.
Prerequisites
Before we begin, make sure you have the following:.
- A Fedora system (Fedora 34 or later is recommended for the latest Node.js features).
- Access to a user account with
sudo
privileges for administrative tasks. - Basic familiarity with the terminal and command-line operations.
Updating the System
It's always a good idea to start with an updated system. Open your terminal and run the following commands to update your package lists and upgrade existing packages:.
sudo dnf update -y
Installation Methods
Using NodeSource Repository
NodeSource provides a repository containing the latest versions of Node.js. Here's how to install Node.js from the NodeSource repository:.
Choose the Node.js version you want to install. You can find the available versions and corresponding commands on the NodeSource GitHub page.
Run the installation script provided by NodeSource. For example, to install Node.js 16.x, you would use:.
```bash
curl -sL https://rpm.nodesource.com/setup_16.x | sudo bash -
```
- Install Node.js and npm (Node Package Manager):.
```bash
sudo dnf install nodejs -y
```
After the installation completes, you can verify the Node.js and npm versions:.
node --version
npm --version
Our output:.
v16.x.x
x.x.x
Using NVM (Node Version Manager)
NVM is a tool that allows you to install and manage multiple Node.js versions. Here's how to install NVM and use it to install Node.js:.
- Install NVM by running the following command:.
```bash
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.1/install.sh | bash
```
Reload your shell configuration or restart your terminal session.
Install the desired version of Node.js using NVM. For example, to install Node.js 14.x:.
```bash
nvm install 14.x
```
- Set the default Node.js version:.
```bash
nvm alias default 14.x
```
- Verify the installation:.
```bash
node --version
npm --version
```
Expected output:.
v14.x.x
x.x.x
Using the Official Fedora Repositories
Fedora's official repositories may not always have the latest Node.js version, but they are a reliable source for a stable release. Here's how to install Node.js from the official repositories:.
sudo dnf install nodejs npm -y
Verify the installation:.
node --version
npm --version
Using Snap Packages
Fedora also supports Snap packages, which can be used to install Node.js:.
- Enable Snapd on Fedora:.
```bash
sudo dnf install snapd -y
sudo ln -s /var/lib/snapd/snap /snap
```
- Install Node.js using Snap:.
```bash
sudo snap install node --classic --channel 16/stable
```
- Verify the installation:.
```bash
node --version
npm --version
```
Expected output:.
v16.x.x
x.x.x
Managing Node.js Versions with NVM
If you installed Node.js using NVM, you could easily switch between versions. Here's how:.
- List installed Node.js versions:.
```bash
nvm ls
```
- Switch to a different version:.
```bash
nvm use v12.x.x
```
- Set a default version for new shells:.
```bash
nvm alias default v12.x.x
```
Configuring npm
To configure npm, you might want to set up a default registry or configure your npm permissions. Here are some useful npm commands:.
- Change npm registry:.
```bash
npm config set registry https://registry.npmjs.org/
```
- Configure npm to not use
sudo
(optional, but recommended):.
```bash
sudo chown -R $(whoami) $(npm config get prefix)/{lib/node_modules,bin,share}
```
Installing Global Packages
To install packages globally with npm, use the -g
flag:.
npm install -g package-name
This is useful for command-line tools that you want to access from any location in your terminal.
Setting Up a Node.js Project
Here's how to set up a new Node.js project:.
- Create a project directory and navigate into it:.
```bash
mkdir my-node-project
cd my-node-project
```
- Initialize a new Node.js project:.
```bash
npm init -y
```
This creates a `package.json` file with default values.
- Add dependencies to your project:.
```bash
npm install express
```
This installs the `express` package and adds it to your `package.json` file.
Security Considerations
Security is paramount when developing with Node.js. Here are some tips:.
- Keep Node.js up to date to ensure you have the latest security patches.
- Run
npm audit
regularly to identify and fix vulnerabilities in your project dependencies.
Troubleshooting Common Issues
- Permission issues: If you encounter permission issues with global packages, make sure you've configured npm to install packages without using
sudo
. - Outdated packages: If your system packages are outdated, run
sudo dnf update
to update them.
What we learned
You've now learned multiple ways to install Node.js on Fedora, how to manage Node.js versions using NVM, and how to set up a new Node.js project. Remember to keep your Node.js environment secure and up to date.
Further Reading
Appendix: Uninstalling Node.js
If you need to uninstall Node.js, use the following commands based on your installation method:.
- Using NodeSource or Official Repositories:.
```bash
sudo dnf remove nodejs npm
```
- Using Snap:.
```bash
sudo snap remove node
```
- Using NVM:.
```bash
nvm uninstall node
```
Remember to remove any global npm packages you no longer need.
By following this guide, you should have a fully functional Node.js environment on your Fedora system. If you encounter any issues or have suggestions, feel free to leave a comment or reach out to the Fedora community for support.