DEV Community

Cover image for Running Puppeteer on a Server: A Complete Tutorial
Arham Rumi
Arham Rumi

Posted on

Running Puppeteer on a Server: A Complete Tutorial

Puppeteer is a Node.js library that provides a high-level API to control Chrome or Chromium browsers over the DevTools Protocol. It's a powerful tool for web scraping, automated testing, capturing screenshots etc. While using Puppeteer locally is straightforward, running it on a server requires additional considerations. This guide will walk you through the steps to get Puppeteer up and running on a server.


Preparing Server for Puppeteer

  • Update Server

This step is crucial for the successful execution of Puppeteer. Execute the following commands.

sudo apt update -y
sudo apt upgrade -y
Enter fullscreen mode Exit fullscreen mode
  • Install Dependencies

Install the following dependencies to ensure Puppeteer runs smoothly.

sudo apt-get install libpangocairo-1.0-0 libx11-xcb1 libxcomposite1 libxcursor1 libxdamage1 libxi6 libxtst6 libnss3 libcups2 libxss1 libxrandr2 libatk1.0-0 libgtk-3-0 libasound2t64
Enter fullscreen mode Exit fullscreen mode
  • Install Puppeteer

Execute the following command to install the latest version of Puppeteer, which is always recommended for optimal performance.

npm i puppeteer
Enter fullscreen mode Exit fullscreen mode

Using Puppeteer

You can use the following code snippet to verify that Puppeteer is functioning correctly by invoking this function at your desired route.

const puppeteer = require("puppeteer");

/**
 * Launches a Puppeteer browser, navigates to a webpage, and then closes the browser.
 *
 * Launch Options:
 * - headless: Run the browser in headless mode (no GUI).
 * - args:
 *   - "--no-sandbox": Required if running as the root user.
 *   - "--disable-setuid-sandbox": Optional, try if you encounter sandbox errors.
 */

const runPuppeteer = async () => {
  try {
    // Launch a Puppeteer browser instance with custom arguments
    const browser = await puppeteer.launch({
      headless: true,
      args: [
        "--no-sandbox",
        "--disable-setuid-sandbox",
      ],
    });

    // Open a new page in the browser
    const page = await browser.newPage();

    // Navigate to the specified URL
    await page.goto("https://www.google.com");

    console.log("Navigation to Google completed.");

    // Close the browser
    await browser.close();
    console.log("Browser closed successfully.");
  } catch (error) {
    console.error("An error occurred:", error);
  }
};

// Execute the function
runPuppeteer();
Enter fullscreen mode Exit fullscreen mode

Conclusion

Running Puppeteer on a server requires careful setup to handle dependencies, permissions, and resources. By following this guide, you can effectively deploy Puppeteer for tasks such as web scraping or automated testing in a server environment. For more advanced use cases, consider using tools like PM2 for process management and Docker for containerization.

Feel free to share this guide with others, and let us know in the comments if you encounter any issues after following the instructions.

Top comments (0)