Is it possible to run Puppeteer-Sharp on Linux and MacOS?

Yes, Puppeteer-Sharp runs natively on both Linux and macOS platforms. As a .NET Standard 2.0 library, Puppeteer-Sharp provides full cross-platform compatibility for browser automation tasks including web scraping, testing, and PDF generation.

Prerequisites

Before running Puppeteer-Sharp on Linux or macOS, ensure you have:

  1. .NET Runtime/SDK: Puppeteer-Sharp supports .NET Core 2.0+, .NET 5/6/7/8, and .NET Framework 4.6.1+
  2. Chromium Browser: Automatically downloaded by Puppeteer-Sharp during first run

Platform-Specific Setup

Linux Installation

Ubuntu/Debian Dependencies

Install required system libraries for Chromium:

sudo apt-get update
sudo apt-get install -y \
    libglib2.0-0 libnss3 libnspr4 libatk1.0-0 libatk-bridge2.0-0 \
    libcups2 libdrm2 libdbus-1-3 libxkbcommon0 libxcomposite1 \
    libxdamage1 libxrandr2 libgbm1 libasound2 libxss1 libgconf-2-4

CentOS/RHEL Dependencies

sudo yum install -y \
    glib2 nss nspr atk at-spi2-atk cups-libs drm libXdamage \
    libXrandr gtk3 alsa-lib

macOS Installation

macOS typically includes all necessary dependencies. Simply install .NET:

# Using Homebrew
brew install dotnet

# Verify installation
dotnet --version

Getting Started

1. Create a New Project

# Create console application
dotnet new console -n PuppeteerSharpCrossPlatform
cd PuppeteerSharpCrossPlatform

# Add Puppeteer-Sharp package
dotnet add package PuppeteerSharp

2. Basic Web Scraping Example

using System;
using System.Threading.Tasks;
using PuppeteerSharp;

class Program
{
    public static async Task Main(string[] args)
    {
        // Download Chromium if not already present
        await new BrowserFetcher().DownloadAsync();

        // Launch browser with cross-platform options
        var browser = await Puppeteer.LaunchAsync(new LaunchOptions
        {
            Headless = true,
            Args = new[] { "--no-sandbox", "--disable-setuid-sandbox" } // Linux compatibility
        });

        try
        {
            var page = await browser.NewPageAsync();

            // Navigate and scrape content
            await page.GoToAsync("https://example.com");

            var title = await page.GetTitleAsync();
            var content = await page.EvaluateExpressionAsync<string>(
                "document.querySelector('h1').textContent"
            );

            Console.WriteLine($"Title: {title}");
            Console.WriteLine($"Heading: {content}");

            // Take screenshot
            await page.ScreenshotAsync("example.png");
        }
        finally
        {
            await browser.CloseAsync();
        }
    }
}

3. Advanced Cross-Platform Configuration

using PuppeteerSharp;
using System;
using System.Runtime.InteropServices;

public class CrossPlatformPuppeteer
{
    public static async Task<LaunchOptions> GetPlatformLaunchOptions()
    {
        var options = new LaunchOptions
        {
            Headless = true,
            DefaultViewport = new ViewPortOptions { Width = 1920, Height = 1080 }
        };

        // Platform-specific arguments
        if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
        {
            options.Args = new[]
            {
                "--no-sandbox",
                "--disable-setuid-sandbox",
                "--disable-dev-shm-usage",
                "--disable-gpu",
                "--remote-debugging-port=9222"
            };
        }
        else if (RuntimeInformation.IsOSPlatform(OSPlatform.OSX))
        {
            options.Args = new[]
            {
                "--disable-gpu",
                "--disable-dev-shm-usage"
            };
        }

        return options;
    }
}

Docker Deployment

Dockerfile for Linux Containers

FROM mcr.microsoft.com/dotnet/runtime:8.0

# Install Chromium dependencies
RUN apt-get update && apt-get install -y \
    libglib2.0-0 libnss3 libnspr4 libatk1.0-0 libatk-bridge2.0-0 \
    libcups2 libdrm2 libdbus-1-3 libxkbcomposite1 libxrandr2 \
    libgbm1 libasound2 && rm -rf /var/lib/apt/lists/*

WORKDIR /app
COPY . .

# Run with non-root user for security
RUN useradd -ms /bin/bash puppeteer
USER puppeteer

ENTRYPOINT ["dotnet", "YourApp.dll"]

Common Issues and Solutions

Linux Permission Issues

# If running as root (not recommended)
export PUPPETEER_SKIP_CHROMIUM_DOWNLOAD=false
export PUPPETEER_CHROMIUM_REVISION=1095492

# Better: run as non-root user
sudo useradd -m puppeteer
sudo -u puppeteer dotnet run

macOS Quarantine Issues

# Remove quarantine attribute from downloaded Chromium
sudo xattr -dr com.apple.quarantine ~/.local/share/puppeteer

Memory Optimization

var options = new LaunchOptions
{
    Args = new[]
    {
        "--memory-pressure-off",
        "--max_old_space_size=4096",
        "--disable-dev-shm-usage" // Use /tmp instead of /dev/shm
    }
};

Performance Considerations

  • Resource Management: Always dispose browsers properly using using statements
  • Parallel Execution: Limit concurrent browser instances based on system resources
  • Memory Usage: Monitor memory consumption, especially in containerized environments
  • Process Cleanup: Ensure Chrome processes are terminated on application exit
// Proper resource management
await using var browser = await Puppeteer.LaunchAsync(options);
await using var page = await browser.NewPageAsync();
// Browser automatically disposed at end of scope

Puppeteer-Sharp's cross-platform support makes it an excellent choice for .NET developers needing browser automation capabilities across different operating systems.

Related Questions

Get Started Now

WebScraping.AI provides rotating proxies, Chromium rendering and built-in HTML parser for web scraping
Icon