Manoj Damor

Unlock the World of Coding with Coding Funda

How to Install Forever on Ubuntu 20.04/18.04/18.04

3 min read
How to install forever ubuntu 20.04/20.04/18

How to install forever ubuntu 20.04/20.04/18 manoj damor

How to Install Forever on Ubuntu 20.04/18.04/18.04

Introduction: Forever is a powerful command-line tool used to ensure that a Node.js script runs continuously, even after unexpected errors or crashes. It can be especially useful for running applications or scripts in a production environment. In this blog, we will guide you through the step-by-step process of installing Forever on Ubuntu 20.04, 18.04, and 16.04, enabling you to manage and monitor your Node.js applications effectively.

Table of Contents:

  1. Understanding Forever and its Benefits
  2. Prerequisites
  3. Installing Node.js
  4. Installing Forever using npm
  5. Running Scripts with Forever
  6. Using Forever with Systemd (Optional)
  7. Conclusion

1. Understanding Forever and its Benefits

Forever is a tool that ensures your Node.js applications run continuously, automatically restarting them in case of errors or crashes. Its key benefits include:

  • Automatic restarts: Forever monitors your script and restarts it if it crashes, encounters an error, or terminates unexpectedly.
  • Persistent execution: It keeps your Node.js script running indefinitely, reducing downtime and ensuring continuous availability.
  • Log management: Forever provides log files that capture the output and errors generated by your application, making troubleshooting and debugging easier.
  • Easy management: It offers a simple command-line interface for starting, stopping, and monitoring your Node.js applications.

2. Prerequisites

Before installing Forever, make sure you have the following prerequisites in place:

  • An Ubuntu 20.04, 18.04, or 16.04 server or desktop environment.
  • A user account with sudo privileges to perform administrative tasks.
  • A working internet connection to download and install the required packages.

3. Installing Node.js

Before installing Forever, you need to have Node.js installed on your Ubuntu system. If Node.js is already installed, you can skip this step. Otherwise, follow these steps to install Node.js:

  1. Update the package lists on your system:
sqlCopy codesudo apt update
  1. Install Node.js and npm (Node Package Manager): For Ubuntu 20.04/18.04:
Copy codesudo apt install nodejs npm

For Ubuntu 16.04:

arduinoCopy codecurl -sL https://deb.nodesource.com/setup_14.x | sudo -E bash -
sudo apt install nodejs
  1. Verify the installation by checking the Node.js and npm versions:
cssCopy codenode --version
npm --version

4. Installing Forever using npm

Once Node.js is installed, you can proceed with installing Forever using npm:

  1. Open your terminal and run the following command to install Forever globally:
Copy codesudo npm install -g forever

The “-g” flag installs Forever as a global package, making it accessible from anywhere on your system.

  1. Verify the installation by checking the Forever version:
cssCopy codeforever --version

5. Running Scripts with Forever

Now that Forever is installed, you can use it to run your Node.js scripts or applications:

  1. Navigate to the directory where your Node.js script is located using the cd command:
bashCopy codecd /path/to/your/nodejs/script
  1. Start your script with Forever using the following command:
sqlCopy codeforever start script.js

Replace “script.js” with the name of your Node.js script.

  1. Monitor the status of your script by running:
Copy codeforever list

This command displays a list of running Forever processes along with their details.

  1. Stop a running script with Forever using the following command:
arduinoCopy codeforever stop script.js

Replace “script.js” with the name of your script or the process ID displayed in the forever list command.

6. Using Forever with Systemd (Optional)

Optionally, you can configure Forever as a Systemd service to automatically start your Node.js script on system boot and manage it as a background service. Here’s a brief overview of the steps involved:

  1. Create a Systemd service file.
  2. Configure the service file with the necessary settings.
  3. Enable and start the Forever service.

For detailed instructions on using Forever with Systemd, please refer to the official Forever documentation or relevant online resources.

7. Conclusion

By following this guide, you have successfully installed Forever on Ubuntu 20.04, 18.04, or 16.04, enabling you to run your Node.js scripts continuously and manage them with ease. Forever’s ability to automatically restart your scripts after crashes or errors ensures the smooth operation of your applications. Experiment with Forever and explore its additional features to further enhance your Node.js development and deployment process.

Check My Social Profile Links

Instagram

Youtube

Website

About The Author

Leave a Reply

Your email address will not be published. Required fields are marked *

Follow by Email
fb-share-icon
Share