Here’s a step-by-step tutorial for installing Node.js on your AlmaLinux 8 VPS:
1. Update Your System
Before installing any software, it's a good practice to update your server:
sudo dnf update -y
2. Install Development Tools
Install a set of development tools, including GCC (GNU Compiler Collection), which will be useful for compiling and building native add-ons:
sudo dnf groupinstall "Development Tools" -y
3. Install Node.js
You can install Node.js via the NodeSource repository. This ensures you're getting the latest LTS (Long Term Support) version.
-
Add the NodeSource repository by running the following command:
curl -sL https://rpm.nodesource.com/setup_20.x | sudo bash -
. 2.Install Node.js using:
sudo dnf install nodejs -y
4. Verify Installation
After installation, verify that Node.js and npm (Node package manager) are successfully installed:
node -v
npm -v
This should return the installed version of Node.js and npm.
5. Enable npm Global Installations
If you plan to install global npm packages without needing to use sudo
, create a directory for global installations and configure npm to use it.
-
Create a directory for globally installed packages:
mkdir ~/.npm-global
- Configure npm to use this directory for global installations:
npm config set prefix '~/.npm-global'
- Add this directory to your system's PATH by editing your shell profile:
echo 'export PATH=~/.npm-global/bin:$PATH' >> ~/.bashrc
source ~/.bashrc
6. Test Node.js by Creating a Simple Server
To confirm Node.js is working, create a simple test server:
-
Create a new file called
app.js
:
nano app.js
- Add the following content:
const http = require('http');
const hostname = '0.0.0.0';
const port = 3000;const server = http.createServer((req, res) => {
res.statusCode = 200;
res.setHeader('Content-Type', 'text/plain');
res.end('Hello World\n');
});server.listen(port, hostname, () => {
console.log(`Server running at http://${hostname}:${port}/`);
});
- Run the app using Node.js:
node app.js
Now, in your browser, go to http://your_vps_ip:3000
and you should see "Hello World" displayed.
7. Install PM2 to Manage Node.js Application
PM2 is a process manager for Node.js that ensures your app stays running. Install it with:
sudo npm install pm2 -g
To start your Node.js app with PM2:
pm2 start app.js
That's it! You've successfully installed Node.js on your AlmaLinux 8 VPS. You can now deploy your Node.js apps and manage them with tools like PM2.