enadabuzaid's avatar

Laravel PDF (Spatie) + Puppeteer + Forge: “No usable sandbox” error in production

Hey folks 👋

I’m using the spatie/laravel-pdf package (which wraps Browsershot + Puppeteer) to generate PDFs in my Laravel 12 project.

It works perfectly on my local machine, but when I deploy it to my Forge provisioned Ubuntu 24.04 server, I get this error when trying to generate a PDF:

Error: Failed to launch the browser process!
[xxxx/FATAL:zygote_host_impl_linux.cc(132)] No usable sandbox!

0 likes
5 replies
LaryAI's avatar
Level 58

The error you're encountering is related to Puppeteer trying to launch a headless browser on a server without the necessary sandboxing capabilities. This is a common issue when running Puppeteer on Linux servers, especially when using a minimal setup like those often found on cloud hosting services.

Here’s how you can resolve the "No usable sandbox" error:

  1. Install Necessary Dependencies:

    Puppeteer requires certain system dependencies to run properly. You can install these on your Ubuntu server using the following command:

  2. Disable the Sandbox:

    If installing the dependencies does not resolve the issue, you can try running Puppeteer without the sandbox. This is generally not recommended for production environments due to security concerns, but it can be a temporary workaround.

    You can disable the sandbox by adding the --no-sandbox and --disable-setuid-sandbox flags to the Puppeteer launch options. In the context of the spatie/laravel-pdf package, you can configure this in your Laravel code:

    use Spatie\Browsershot\Browsershot;
    
    Browsershot::html('<h1>Hello world!</h1>')
        ->noSandbox()
        ->save('example.pdf');
    

    The noSandbox() method is a convenience method provided by Browsershot to add these flags.

  3. Consider Security Implications:

    If you choose to disable the sandbox, be aware of the security implications. Running without a sandbox can expose your server to potential vulnerabilities. If possible, consider running Puppeteer in a more secure environment or using a different approach to generate PDFs.

By following these steps, you should be able to resolve the "No usable sandbox" error and successfully generate PDFs on your Forge provisioned server.

1 like
simotion's avatar

For anyone encountering the same issue, and for future reference:

I ran into this problem on an Ubuntu 24.04 server provisioned through Forge.

I was able to resolve it by loosening the AppArmor restrictions using the following commands:

sudo sysctl -w kernel.apparmor_restrict_unprivileged_userns=0
sudo sysctl -w kernel.apparmor_restrict_unprivileged_unconfined=0
2 likes
Tor2r's avatar

Yup, that one did it for me. After installing sh*#¤ loads of packages manually.

God knows what all this does to the server :D

cmackinlay's avatar

WOrth noting that those changes don't persist after a system restart but you can fix that by adding them to /etc/syscntl.conf:

kernel.apparmor_restrict_unprivileged_userns=0
kernel.apparmor_restrict_unprivileged_unconfined=0
amitgupta's avatar

If you generate PDFs often then another option is to use Gotenberg. It can be run as a docker container which the official project supplies. Generating PDFs is then as simple as making a REST API call to your Gotenberg instance and passing the data to it.

I created igeek/pdfservice package to work with Gotenberg. It has API similar to Spatie's Laravel PDF.

Please or to participate in this conversation.