Issues with Configuring Laravel Spatie/Browsershot and Puppeteer on Ubuntu 24.04 with PHP 8.3, Chrome, and Nginx

Environment Setup:

  • PHP Version: 8.3
  • Laravel Version: 11
  • OS: Ubuntu 24.04 LTS
  • Web Server: NGINX
  • Node Version: v18.19.1 (most up-to-date)
  • Dependency: Spatie/Browsershot utilizing Puppeteer and Chrome version 128.0.6613.119

Problems Encountered:

  1. Spatie requires installation of older versions of Node and Puppeteer, which is not ideal as users prefer the latest releases.
  2. Following Spatie’s guidance leads to permission-related issues; specifically, the www-data user cannot execute Chrome or create temporary directories.
  3. Attempts to use other versions of Chrome (like chromium-browser or traditional .deb installations) have failed due to php8.3-fpm being outside the Snap cgroup or facing additional permission problems.
  4. We desire the latest software versions, but some packages are not found.

Solution to Configuring Laravel Spatie/Browsershot with Puppeteer on Ubuntu 24.04

Ensuring a smooth setup for Laravel Spatie/Browsershot and Puppeteer on Ubuntu 24.04 can certainly pose challenges, particularly with keeping dependencies up to date. Here’s a direct approach to address the issues you’ve encountered:

1. Node and Puppeteer Versions

To use the latest Node and Puppeteer versions, ensure that you have the latest Node.js version compatible with your package requirements. You can manage and switch between Node versions using nvm (Node Version Manager), which allows more flexibility.
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.3/install.sh | bash
source ~/.bashrc
nvm install node # Install latest Node version
nvm use node    # Use the latest version

For Puppeteer, run:

npm install puppeteer@latest

2. Permission Issues with www-data User

To resolve permission issues, ensure that www-data has necessary permissions to run Chrome:
  • Use sudo to grant access:
sudo chown -R www-data:www-data /path-to-your-project
  • Grant execute permissions on Chrome:
sudo setfacl -m u:www-data:x /path/to/chrome

3. Chrome Installation

Installing Google Chrome instead of using the Snap package may help avoid some cgroup or permission issues:
wget https://dl.google.com/linux/direct/google-chrome-stable_current_amd64.deb
sudo dpkg -i google-chrome-stable_current_amd64.deb
sudo apt --fix-broken install # Resolve any dependency issues

4. Verification

Finally, verify the installation by running a basic Puppeteer script to ensure Chrome runs correctly:
node -e "require('puppeteer').launch().then(browser => browser.newPage()).then(page => page.goto('https://example.com')).then(() => console.log('Success!')).catch(err => console.error(err));"

This approach should streamline your setup, maintaining up-to-date software while resolving permission issues efficiently. Let me know if you need further assistance!