Skip to main content

pybubble

A simple wrapper around bubblewrap to create sandboxed environments for executing code. It works without Docker or other daemon-based container runtimes, using shared read-only root filesystems for quick (1-2ms) setup times.

While these environments are sandboxed and provide protection from accidental modification of your host system by overzealous LLMs, pybubble is not sufficient to protect you against actively malicious code. In general, while containerization solutions like pybubble or Docker offer a reasonable degree of protection from accidental damage, when accepting input from the public you should consider using virtualization in place of or in addition to containers.

Feel free to submit bug reports and pull requests via GitHub, but note that Arcee is not committing to long-term support of this software. I wrote this library in my spare time to solve an irritating problem with building code execution environments, so expect a pace of updates consistent with "time I have while waiting for a debug run to finish".

Due to relying on Linux kernel features to operate, pybubble is not compatible with macOS or Windows.

Setup

Install bwrap. On Ubuntu, do:

$ sudo apt-get install bubblewrap

Optionally, for overlay filesystem support (writable rootfs without modifying the original):

$ sudo apt-get install fuse-overlayfs

For outbound internet access (or port forwarding via enable_outbound=True), install slirp4netns:

$ sudo apt-get install slirp4netns

Basic internal networking (enable_network=True) does not require slirp4netns.

Then, add pybubble to your project.

$ uv add pybubble

Root filesystem archives

Prebuilt wheels for pybubble come bundled with an x86 Alpine Linux root filesystem archive based on default-rootfs.dockerfile. It comes with:

  • Python
  • uv
  • bash
  • ripgrep
  • cURL & wget
  • numpy
  • pandas
  • httpx & requests
  • pillow
  • ImageMagick

If you need more tools or want to run a leaner environment, follow this guide to build one yourself.

Run sandboxed code

from pybubble import Sandbox
import asyncio

async def main():
    with Sandbox(enable_outbound=True) as sbox:
        process = await sbox.run("ping -c 1 google.com")
        stdout, stderr = await process.communicate()
        print(stdout.decode())

        process = await sbox.run_script("print('hello, world')", timeout=5.0)
        stdout, stderr = await process.communicate()
        print(stdout.decode())

if __name__ == "__main__":
    asyncio.run(main())

PTY mode

For interactive programs, pass use_pty=True to get a real pseudoterminal. Ctrl+C, colors, job control, and curses apps all work.

async def main():
    with Sandbox() as sbox:
        proc = await sbox.run("bash", use_pty=True)
        await proc.send(b"echo hello\n")

        async for chunk in proc.stream(decode=True):
            print(chunk, end="")

        await proc.wait()
        proc.close_pty()

Overlay filesystem

With fuse-overlayfs installed, you can make the rootfs writable without modifying the cached original:

with Sandbox(rootfs_overlay=True, enable_outbound=True) as sbox:
    proc = await sbox.run("apk add git")
    await proc.communicate()

Networking

Sandbox networking is configured on construction:

  • enable_network=True enables an isolated internal network namespace.
  • enable_outbound=True adds outbound internet access via slirp4netns.
  • allow_host_loopback=True allows access to host loopback services.

If you only need internal networking between sandboxed processes, leave outbound disabled and slirp4netns is not required.

Port forwarding is available via forward_port(...):

with Sandbox(enable_outbound=True) as sbox:
    sbox.forward_port(8080, 18080)  # sandbox:8080 -> host:18080

Use the CLI

You can also run programs interactively via the CLI.

uv run pybubble run bash
sandbox:~$ echo "Hello, world!"
Hello, world!

With an overlay filesystem:

uv run pybubble run --rootfs-overlay bash
sandbox:~$ apk add nodejs

To learn more about the features available in Sandbox, see this page.

Metadata

Release files for pybubble 0.4.0

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for pybubble 0.4.0
File Size Uploaded
pybubble-0.4.0.tar.gz 75.5 MB Details

Built distribution (wheel)

Table of built distributions (wheels) for pybubble 0.4.0
File Interpreter ABI Platform
pybubble-0.4.0-py3-none-any.whl Python 3 none any Details

Total release size: 151.0 MB

Release files / pybubble-0.4.0.tar.gz

Download URL pybubble-0.4.0.tar.gz
Size 75.5 MB
Tags Source
SHA-256 checksum
How to use checksums
470fcd42c924ac62a28fdae77e34ea057a987746c57c17cf39ecba5e279ee1d9
BLAKE2b-256 checksum
How to use checksums
663ab3ce4d285b48ce406a6493d136511f59e5e93b05e475dbc9ffd39886c4d0
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.9.10 {"installer":{"name":"uv","version":"0.9.10"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"22.04","id":"jammy","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release files / pybubble-0.4.0-py3-none-any.whl

Download URL pybubble-0.4.0-py3-none-any.whl
Size 75.5 MB
Tags Python 3
SHA-256 checksum
How to use checksums
9ceef88f6f0f7a138fa9a4939c52872e229e73cc77d1a73487dad8314cefdd3a
BLAKE2b-256 checksum
How to use checksums
f4845f74fb6121bcee751a93be55dfbd502146160a0d74578a731d97633ab87b
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.9.10 {"installer":{"name":"uv","version":"0.9.10"},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"22.04","id":"jammy","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}

Release history Release notifications | RSS feed

This release

0.4.0 This release

2 release files

0.3.1

2 release files

0.3.0

2 release files

0.2.0

2 release files

0.1.1

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page