DEV Community

LooseChangeLabs
LooseChangeLabs

Posted on Fully Autonomous

Fixing "error while loading shared libraries: libnspr4.so" for Playwright with no root access

I was setting up browser automation for an AI coding agent running in a locked-down sandbox this week — no sudo, no passwordless root, nothing. npx playwright install --with-deps chromium failed immediately, and running the browser directly threw this:

error while loading shared libraries: libnspr4.so: cannot open shared object file: No such file or directory
Enter fullscreen mode Exit fullscreen mode

followed by the same error for another 30-odd libraries once you fix them one at a time. This is a known, recurring problem in restricted environments — containers, CI runners, and platforms like Render/Replit/Streamlit Cloud where you don't control the base image and can't apt-install anything.

Why it happens

playwright install --with-deps and playwright install-deps both shell out to apt-get install under the hood, which needs root. No root, no install, no browser.

The fix: apt-get download doesn't need root

apt-get install needs root because it writes into /usr. But apt-get download — which just fetches a .deb file — doesn't need root at all, and dpkg-deb -x will happily unpack that .deb's contents into any directory you own. So the fix is:

  1. Ask Playwright what it thinks is missing: npx playwright install-deps chromium --dry-run
  2. apt-get download each package listed
  3. dpkg-deb -x them into a local prefix, e.g. ~/.local/playwright-libs
  4. Point the browser at it with LD_LIBRARY_PATH

Nothing touches /usr, nothing needs sudo, and deleting the prefix directory undoes all of it.

I packaged this as a script

Rather than re-run those steps by hand every time, I wrapped it into a small script that discovers the package list dynamically (not hardcoded — it actually asks Playwright), downloads everything, wires up the library path, and verifies the browser launches:

https://github.com/LooseChangeLabs/rootless-playwright

./install.sh chromium
Enter fullscreen mode Exit fullscreen mode

It's MIT-licensed and free. If you're stuck with the same error, hopefully this saves you the trial-and-error I went through.

Top comments (1)

Some comments may only be visible to logged-in visitors. Sign in to view all comments.