How To Install Node.js on CentOS Stream 10
Node.js is a powerful JavaScript runtime built on Chrome’s V8 engine, enabling developers to build scalable and high-performance applications. As web development continues to evolve, utilizing the latest version of Node.js, specifically Node.js 22, is crucial for enhancing performance and ensuring security. This article provides a comprehensive guide on how to install Node.js 22 on CentOS Stream 10, covering various installation methods, troubleshooting tips, and additional resources.
Prerequisites
Before diving into the installation process, ensure that your system meets the following prerequisites:
- System Requirements: A machine running CentOS Stream 10.
- Sudo Access: You must have sudo privileges to install packages.
- Command Line Familiarity: Basic knowledge of using the terminal is beneficial.
Preparing Your System
The first step in installing Node.js is to prepare your CentOS Stream 10 system. This involves updating the system and installing necessary dependencies.
Updating the System
Keeping your system updated is essential for security and stability. Run the following command to update your package index:
sudo dnf update -y
This command ensures that all installed packages are up-to-date with the latest versions available in the repository.
Installing Required Dependencies
Node.js requires certain development tools and libraries to function correctly. Install these dependencies using the following commands:
sudo dnf groupinstall "Development Tools" -y
sudo dnf install python3 -y
The first command installs essential development tools like GCC and make, while the second command installs Python, which may be required for building certain packages from source.
Installing Node.js via NodeSource Repository
One of the most straightforward methods to install Node.js is through the NodeSource repository. This method ensures you get the latest version with minimal hassle.
Adding NodeSource Repository
NodeSource provides an easy way to install Node.js via a dedicated repository. To add the NodeSource repository for Node.js 22, execute the following command:
curl -fsSL https://rpm.nodesource.com/setup_22.x | sudo bash -
This command downloads and executes a script that configures your system to use the NodeSource repository.
Installing Node.js and npm
After adding the repository, you can install Node.js along with npm (Node Package Manager) using this command:
sudo dnf install nodejs -y
This command installs both Node.js and npm in one go. Once installed, verify that both are working correctly by checking their versions:
node -v
npm -v
You should see output indicating the installed versions of Node.js and npm, confirming a successful installation.
Alternative Installation Methods
If you prefer more flexibility or need to manage multiple versions of Node.js, consider using nvm (Node Version Manager).
Installing with nvm (Node Version Manager)
nvm allows you to easily install and switch between different versions of Node.js. Here’s how to set it up:
Steps to Install nvm
-
- Run this command to download and install nvm:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.1/install.sh | bash
-
- After installation, activate nvm by adding it to your shell profile. Use these commands:
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
-
- Check if nvm was installed successfully by running:
nvm --version
Installing Node.js via nvm
You can now install Node.js version 22 using nvm with this command:
nvm install 22
This command downloads and installs the specified version of Node.js. To verify that it was installed correctly, check its version again:
node -v
npm -v
Troubleshooting nvm Installation Issues
If you encounter issues during nvm installation or usage, consider these troubleshooting tips:
- Sourcing Your Profile: Ensure that your shell profile is sourced correctly. You can do this by running
source ~/.bashrc
. - NVM_DIR Not Set: If you receive errors related to NVM_DIR not being set, double-check that you’ve added the export line correctly in your profile.
- NVM Command Not Found: If nvm commands are not recognized, make sure you’ve activated it correctly in your terminal session.
Building Node.js from Source
If you require specific configurations or optimizations, building Node.js from source might be necessary. Here’s how to do it:
When to Build from Source?
You may want to build from source if you need custom features or optimizations not available in pre-built binaries. This method also allows you to understand better how Node.js operates under the hood.
Steps to Build from Source
-
- Download Source Code:
curl -O https://nodejs.org/dist/v22.12.0/node-v22.12.0.tar.gz
tar -xzf node-v22.12.0.tar.gz
cd node-v22.12.0
-
- Configure and Compile:
./configure
make -j4
sudo make install
-
- Verify Installation from Source:
node -v
npm -v
Post-installation Steps
Setting Up Environment Variables (if necessary)
If you need specific environment variables for your applications or development workflow, consider adding them to your shell profile (e.g., .bashrc or .bash_profile). For example, setting up a variable for your project directory can streamline development tasks.
export PROJECT_DIR="/path/to/your/project"
export PATH="$PROJECT_DIR/node_modules/.bin:$PATH"
Global npm Packages Installation Directory Configuration
If you’re planning on installing global npm packages but want to avoid permission issues, configure npm’s global installation directory as follows:
- Create a directory for global installations:
mkdir ~/.npm-global
- Edit your npm configuration:
npm config set prefix '~/.npm-global'
- Add this directory to your PATH:
echo 'export PATH=$HOME/.npm-global/bin:$PATH' >> ~/.bashrc source ~/.bashrc
- You can now install global packages without sudo:
npm install -g package-name
- This method helps avoid permission issues when installing global npm packages.
- Troubleshooting Global Package Installation Issues: If you encounter errors while installing global packages after configuring npm’s prefix, ensure that your PATH is set correctly by echoing it in your terminal:
$ echo $PATH
If your new path isn’t listed, revisit your .bashrc file and ensure it’s sourced properly.
Troubleshooting Common Installation Issues
If you run into problems during installation or configuration, here are some common issues and their solutions:
- Error: Command not found after installation: If you receive a “command not found” error after installing node or npm, ensure that they are included in your PATH variable. You can check this by running:
$ echo $PATH
If they are not included, revisit your profile settings.
- Error: Permission denied when installing packages globally: This often occurs when trying to install global npm packages without proper permissions. Consider using the method described above for configuring npm’s global directory.
- Error: Version mismatch between node and npm: If you notice discrepancies between node and npm versions after installation via nvm or other methods, ensure that you’re using compatible versions by checking their documentation or release notes.
- Error: Failed dependencies during installation: If you encounter dependency errors while trying to install node or other packages, ensure that all required dependencies are installed properly as outlined in previous sections.
Congratulations! You have successfully installed Node.js. Thanks for using this tutorial for installing Node.js on CentOS Stream 10 system. For additional help or useful information, we recommend you check the official Node.js website.