microsandbox-rb
Lightweight microVM sandboxes for Ruby. Run AI agents and untrusted code with hardware-level isolation from an idiomatic, synchronous Ruby API. The gem embeds the microsandbox runtime through a Rust (magnus) native extension: it boots real microVMs, not containers, in under 100 ms, runs standard OCI images, and needs no daemon or server.
Unofficial and community-maintained. This gem is an independent project, not built, endorsed, or supported by Super Rad Company or the microsandbox team. All the hard parts (the microVM engine, the guest
agentd, the networking stack) are theirs; this gem is a Ruby skin over them, tracked release by release. Please open issues here, not upstream. Upstream links: website · docs · official SDKs · Agent Skills · MCP server · Discord.
Highlights
- Real VMs, not containers. Every sandbox has its own Linux kernel under KVM (Linux) or the Hypervisor framework (Apple Silicon). Sub-100 ms boot; the runtime lives in your process.
-
Synchronous, idiomatic Ruby. Keyword arguments, block-scoped lifecycle, a flat typed error hierarchy with stable
#codes, and hand-maintained RBS signatures. The GVL is released during native calls, so other threads keep running. -
The whole core surface. Collected and streaming exec, logs, metrics, and filesystem handles; live
modifywith dry-run plans; snapshots andSandbox.restore; named volumes with host-side access; image cache management; interactiveattach; in-process SSH/SFTP; a rawagentdclient. - Network policy as data. Composable profiles plus per-rule CIDR/domain allow-deny policies, TLS interception, outbound SOCKS4/SOCKS5 proxies, fail-closed strict hostname mode, and secret substitution with per-host allow-lists.
-
Convergent lifecycle.
connect_or_create,restart,destroy, andwait_for_statustake a name to the state you want and refuse to touch a sandbox someone recreated under the same name. -
Hermetic runtime. The optional
microsandbox-rb-binariescompanion gem vendors a checksum-verifiedmsb+libkrunfwin lockstep with the SDK, so production never downloads on first use. Without it, the runtime is provisioned into~/.microsandboxon demand. - Local or cloud. The same API runs against the local libkrun backend or the microsandbox cloud, selected per process or per block.
Installation
The gem is published as microsandbox-rb but is required as microsandbox
(the microsandbox name was already taken on RubyGems):
# Gemfile
gem "microsandbox-rb", require: "microsandbox"
gem "microsandbox-rb-binaries" # optional: vendored msb runtime + libkrunfw firmwareThen bundle install, or gem install microsandbox-rb. Installing the source
gem compiles the Rust extension, so the first install takes a few minutes.
Prerequisites
- Ruby 3.3 or newer.
- Linux with KVM enabled, or macOS on Apple Silicon. Cloud-only users
(
MSB_BACKEND=cloud) need neither. - A stable Rust toolchain (1.94 or newer) on
PATHto compile the source gem. Precompiled extension gems are not published; see docs/releasing.md. - The
msbruntime andlibkrunfwfirmware, from the companion gem above or downloaded on first use. The runtime must be exactly the version the SDK embeds (Microsandbox::RUNTIME_VERSION); an older one left in~/.microsandboxis replaced on first use. Provision ahead of time (a Docker layer, an air-gapped host) with:
Microsandbox.install # no-op when the home already holds this runtime
Microsandbox.runtime_info # => which msb + libkrunfw, from where, which versionThe companion gem ships one platform gem each for arm64-darwin,
x86_64-linux-gnu, and aarch64-linux-gnu, must match the SDK version, and is
found automatically at require "microsandbox". See
docs/runtime.md for the resolution order, the exact-version
rule, MSB_PATH, and disabling the first-use download.
Quick start
require "microsandbox"
Microsandbox::Sandbox.create("hello", image: "public.ecr.aws/docker/library/python:3-slim",
ephemeral: true) do |sb|
output = sb.exec("python", ["-c", "print('Hello, World!')"])
puts output.stdout # => "Hello, World!\n"
puts output.success? # => true
end
# the block form stops the sandbox when it returns; `ephemeral: true` also
# removes it, so the name is free the next time this runsWithout ephemeral: true a stopped sandbox stays in the catalog under its
name (start it again with Sandbox.start, or delete it with Sandbox.remove),
and creating another one with the same name raises SandboxAlreadyExistsError.
See docs/lifecycle.md.
Outside Bundler, write require "microsandbox-rb": the separate microsandbox
gem on RubyGems also ships a microsandbox.rb, and plain
require "microsandbox" loads whichever is found first. Both define a
top-level Microsandbox module, so load only one of them in a process.
The examples pull from AWS's public Docker Library mirror because anonymous
Docker Hub pulls are rate-limited; image: "python" works too when you aren't.
See docs/images.md for authenticated and private registries.
Lifecycle
# Create it, or connect to (and start) the one that already exists.
sb = Microsandbox::Sandbox.connect_or_create("box", image: "public.ecr.aws/docker/library/alpine:latest")
sb.status # => :running
sb = sb.restart # stop + start
sb.destroy # stop + remove
# Handles for existing sandboxes (cursor-paginated list, fine-grained control)
Microsandbox::Sandbox.list.map(&:name)
h = Microsandbox::Sandbox.get("box") # => Microsandbox::SandboxHandle
h.request_stop
h.wait_until_stopped # => Microsandbox::SandboxStopResultSee docs/lifecycle.md for the live Sandbox /
SandboxHandle split, detach, and the identity checks behind
SandboxReplacedError.
Running commands
Microsandbox::Sandbox.create("work", image: "public.ecr.aws/docker/library/python:3-slim") do |sb|
out = sb.exec("ls", ["-la", "/etc"], cwd: "/", timeout: 30)
out.exit_code # non-zero exit is data, not an exception
sb.shell("cat /etc/os-release | grep VERSION").stdout
# Stream events as they arrive; ExecHandle is a single-pass Enumerable
sb.exec_stream("python", ["-u", "-c", "for i in range(3): print(i)"]).each do |event|
print event.text if event.stdout?
end
sb.fs.write("/tmp/data.txt", "hello")
sb.fs.read_text("/tmp/data.txt") # => "hello"
sb.fs.copy_to_host("/tmp/data.txt", "./data.txt")
endSee docs/execution.md for stdin pipes, signals, the image's default command, SSH, and the threading and timeout caveats, and docs/filesystem.md for the guest filesystem API.
Networking and secrets
Microsandbox::Sandbox.create("agent", image: "public.ecr.aws/docker/library/python:3-slim",
network: Microsandbox::NetworkPolicy.custom(default_egress: :deny,
rules: [{ action: :allow, direction: :egress, protocol: :tcp, port: 443,
destination: Microsandbox::Destination.domain("api.example.com") }]),
proxy: Microsandbox::OutboundProxy.socks5("127.0.0.1:1080"),
secrets: [{ env: "API_KEY", value: ENV.fetch("HOST_API_KEY"), allow: ["api.example.com"] }]
) do |sb|
# ...
endStrict hostname enforcement is on by default since 0.18.0, and a denied HTTP
request gets a JSON 403. See docs/configuration.md
for every create option, including resources, ports, volumes, root-disk
layout, the secrets keys, and the HTTP deny response.
Observability and live modification
sb.metrics.cpu_percent # metrics slot goes live a beat after boot
sb.logs(tail: 100).each { |e| puts e.text }
sb.ping.latency_ms
sb.modify(cpus: 2, memory: 1024, dry_run: true).changes # preview, then applyDocumentation
| Topic | Guide |
|---|---|
| Create, stop, handles, convergent lifecycle, identity checks | docs/lifecycle.md |
| Create options: resources, network policy, strict mode, proxies, secrets, volumes | docs/configuration.md |
exec/shell, default workload, streaming, single-pass streams, threads and timeouts, SSH, raw agent client |
docs/execution.md |
| Guest filesystem, streaming reads/writes, host copies | docs/filesystem.md |
Metrics, logs, ping/touch, live modify plans |
docs/observability.md |
| Image cache, private registries, snapshots | docs/images.md |
Runtime binaries gem, msb resolution order, backends, forking, environment variables |
docs/runtime.md |
| Error classes and stable codes | docs/errors.md |
| Complete supported surface and what is not yet exposed | docs/surface.md |
| Release process, release preflight, binaries gems | docs/releasing.md |
| Architecture: native extension, sync bridge, GVL, error mapping | DESIGN.md |
| Companion runtime gem internals | binaries/README.md |
API reference: rubydoc.info/gems/microsandbox-rb.
Type signatures: sig/microsandbox.rbs.
Comparison with the official Ruby gem
Since upstream v0.6.9 the microsandbox repository ships an official
microsandbox gem built from
sdk/ruby,
a compact veneer over the same Rust SDK. Both gems define the Microsandbox
module, so use one or the other in a process, not both. Differences below are
taken from the official gem's own README as of upstream v0.7.14.
| Capability |
microsandbox-rb (this gem) |
official microsandbox gem |
|---|---|---|
Lifecycle, convergent connect_or_create, local/cloud backends |
✅ | ✅ |
Collected exec/shell, logs, metrics, guest filesystem |
✅ | ✅ |
| Image, volume, and snapshot management | ✅ | ✅ |
Booting a sandbox from a snapshot (Sandbox.restore) |
✅ | not exposed |
| Streaming exec, logs, metrics, filesystem handles | ✅ | not exposed |
| Interactive SSH/SFTP client and server | ✅ |
ssh_exec only |
Live modification plans (modify, dry-run) |
✅ | not exposed |
Outbound proxy (proxy:) |
SOCKS4/SOCKS5 | HTTP CONNECT |
| Full network policy and mount builders, strict hostname mode | ✅ | not exposed |
Rootfs patches, structured root disk, raw agentd client |
✅ | — |
| RBS type signatures | ✅ | — |
| Runtime binaries | companion gem or first-use download | first-use download |
| Version numbering | own semver; runtime tag via Microsandbox.runtime_version
|
mirrors the upstream tag |
Versioning
The gem follows its own semantic version, independent
of the upstream runtime it embeds: early releases (0.5.7–0.5.9) happened to
share the upstream tag, but the numbers have diverged since and the gem version
is not an indicator of the embedded runtime. Ask the build instead:
Microsandbox::VERSION # => "0.18.0" (the gem's own version)
Microsandbox.runtime_version # => "v0.7.14" (the embedded upstream runtime tag)While the gem is 0.x, a breaking API change bumps the minor and a fix bumps
the patch. Adopting a new upstream runtime bumps the gem version, the pinned
git tag, Microsandbox::RUNTIME_VERSION, and the table below together. The
companion microsandbox-rb-binaries gem is versioned in lockstep (same number,
released together) and pins the same runtime; the test suite asserts both
constants against this gem's. Every release records its runtime in
CHANGELOG.md.
| Gem version | Upstream runtime | Notes |
|---|---|---|
0.5.7 |
v0.5.7 |
initial release |
0.5.8 |
v0.5.7 |
gem-only revision |
0.5.9 |
v0.5.7 |
gem-only revision |
0.5.10 |
v0.5.8 |
adopts upstream v0.5.8; breaking lifecycle split |
0.5.11 |
v0.5.8 |
gem-only revision |
0.5.12 |
v0.5.8 |
gem-only revision |
0.6.0 |
v0.5.8 |
gem version decoupled from the upstream tag; adds runtime_version
|
0.7.0 |
v0.5.8 |
SDK parity release (large binding-gap closure) |
0.8.0 |
v0.5.10 |
adopts upstream v0.5.10 (idle-only heartbeat, config-fd hardening, 4 GiB default bind-mount quota); supersedes the reverted v0.5.9 attempt |
0.8.1 |
v0.5.10 |
gem-only: re-provision a stale local runtime; per-bind-mount quota_mib: override |
0.8.2 |
v0.5.10 |
gem-only: redact secrets from errors, typed snapshot errors, panic-free durations, fat-gem loader + extconf preflight fixes, threading/streaming docs |
0.9.0 |
v0.6.1 |
adopts upstream v0.6.0+v0.6.1 (zombie-runtime wait fix, secret substitution through CONNECT proxies, stale-sandbox cleanup); upstream public API is additive — no Ruby surface change |
0.9.1 |
v0.6.2 |
adopts upstream v0.6.2 (faster image loads/pulls via early cache gate + zlib-rs); upstream API unchanged — no Ruby surface change |
0.9.2 |
v0.6.3 |
adopts upstream v0.6.3 (v4-only sandboxes stop advertising AAAA DNS answers — fixes guest gRPC/c-ares preferring unreachable IPv6; scoped upstream TLS verification); glue moves to *_local SDK variants — no Ruby surface change |
0.10.0 |
v0.6.6 |
adopts upstream v0.6.4+v0.6.6 (v0.6.5 was yanked upstream): snapshot restore by pinned digest — fixes fatal restore-after-tag-republish bug, fragmented-UDP/PMTU relay fixes, exec kills the whole process group, ephemeral stop-wait tolerance, readdir RSS-leak fix; plus v0.6.6 API parity: live modify/resize, ping/touch, create max_cpus/max_memory
|
0.11.0 |
v0.6.7 |
adopts upstream v0.6.7 (breaking): network profiles replace public_only/non_local, structured root_disk: replaces oci_upper_size: (deprecated alias kept), snapshot descriptor contract (create re-keyed by name, save/load rename, snapshot_to removed, on-disk auto-migration), Image.load/Image.save, follow_root_symlinks:; runtime carries the GHSA-4vq3-cjpp-v7fg msb copy fix |
0.12.0 |
v0.6.8 |
adopts upstream v0.6.8 (breaking): Sandbox.list/.list_with return a cursor-paginated SandboxPage (limit:/cursor: keywords), UnsupportedError re-keyed by structured operations with #operation/#hint; runtime adds a shared log registry for followed streams and cloud exec/ssh reconnects |
0.13.0 |
v0.6.9 |
adopts upstream v0.6.9 (breaking): a bare MSB_API_KEY no longer selects the cloud backend (explicit MSB_BACKEND=cloud or a cloud profile required; invalid cloud config fails closed with InvalidConfigError); snapshot payload integrity becomes opt-in (record_integrity:, verify can report :not_recorded). Parity: default-workload execution (exec_default/exec_default_stream/attach_default, cmd:), flat root disks (RootDisk.flat), modify(root_disk_size:), rate_limiter:, vsock:, default_backend_info, Volume.get_default
|
0.14.0 |
v0.6.9 |
two-gem split: SDK-only gem (no build-time runtime download) + companion microsandbox-rb-binaries platform gems |
0.15.0 |
v0.6.14 |
adopts upstream v0.6.10–v0.6.14 step by step: bind-mount correctness, guest bootstrap off the kernel command line, DNS pins for deferred domain allows, Linux glibc 2.28 baseline for the prebuilt runtime, legacy ext4 upper-disk resize, msb_krun 0.1.32. Parity: ssh.open_client/prepare_server accept inactivity_timeout: (seconds; 0 disables, nil inherits the 600s global default) |
0.16.0 |
v0.6.16 |
adopts upstream v0.6.15+v0.6.16 step by step: mount fallback ownership, readonly-mount write-probe fix, log retrieval rerouted through the SDK backends, config overlaid by field presence, network-slot recycling. Parity: per-mount uid:/gid:, and the convergent lifecycle — Sandbox.connect_or_create, #id, #wait_for_status, #restart, #destroy, SandboxHandle#connect_or_start, SandboxReplacedError
|
0.17.0 |
v0.6.18 |
adopts upstream v0.6.17+v0.6.18 step by step: outbound SOCKS4/SOCKS5 proxies (SOCKS5 UDP + credentials), migration-order fix for databases last opened by a v0.6.15 msb, security hardening — plain-HTTP Host/:authority now checked against domain-rule policies even in non-strict mode, plus fail-closed strict hostname mode. Parity: proxy: (Microsandbox::OutboundProxy / SecretSource), strict:
|
0.17.1 |
v0.6.18 |
gem-only security and hardening release: relative host paths pinned at create time (GHSA-mcw9-9rf6-p4rm), Volume.fs path validation (mitigates GHSA-rh2c-vm8j-5gg6), Cargo.lock advisory updates, argument-guard and cleanup fixes, SECURITY.md
|
0.17.2 |
v0.6.18 |
gem-only fix and release-pipeline hardening release: an interrupted native call no longer loses its result (a sandbox the call owns is stopped on Timeout/Thread#kill), release preflight, SHA-pinned workflow actions and cargo deny advisory checks, RBS and docs contract specs |
0.17.3 |
v0.6.18 |
gem-only fix release, fork safety: a forked child (Puma, Resque, Solid Queue) rebuilds its backend and runtime on its first SDK call instead of hanging or crashing, a handle created before the fork raises ForkedHandleError in the child, and nothing inherited from the parent is dropped there |
0.17.4 |
v0.6.18 |
gem-only fix release: fork safety identifies a process by pid and fork generation (survives pid reuse), install/setup leave the backend alone in a forked child, a Sandbox subclass's initialize runs outside the interrupt mask, and the macOS limit that a forked child must not build network clients is documented; rustls 0.23.46 fixes four TLS advisories (RUSTSEC-2026-0346 to -0349) |
0.17.5 |
v0.6.18 |
gem-only security release: dependency updates clear RUSTSEC-2026-0381 to -0410, including the zstd used for image-layer decompression (three zstd advisories remain only on an external-kernel path the runtime never takes, see CHANGELOG.md); SECURITY.md maps more upstream advisories to gem versions |
0.18.0 |
v0.7.14 |
adopts upstream v0.7.14 (breaking): the msb runtime must be exactly the SDK's version (a stale home runtime is replaced automatically, other sources are refused); secrets vocabulary (allow:/allow_placeholder_for:/substitution:/violation_action:), secret_violation_action:; Sandbox.restore replaces create(from_snapshot:), full: replaces resumable:; HTTP denials default to a JSON 403 (http: option); one-way catalog migrations and macOS storage recovery for existing sandboxes; Ruby >= 3.3 |
Development
bin/setup # bundle install + compile the native extension
bundle exec rake compile # rebuild the extension (debug); compile:release for optimized
bundle exec rake spec # unit + fake-runtime specs (no runtime needed)
MICROSANDBOX_INTEGRATION=1 bundle exec rspec spec/integration # boot real microVMs (KVM / Apple Silicon)
bundle exec standardrb # Ruby lint
cargo fmt --check --manifest-path ext/microsandbox/Cargo.toml
cargo clippy --manifest-path ext/microsandbox/Cargo.toml -- -D warningsCI compiles and runs the unit suite on Ruby 3.3 and 3.4 on Linux and macOS,
lints Rust and Ruby, packages and installs the built gems, builds every
microsandbox-rb-binaries platform gem, and boots real microVMs in a KVM
integration job against both the provisioned and the bundled runtime.
The native extension pins the upstream core crate by git tag, so it builds
without an adjacent checkout. To develop against a sibling microsandbox/
checkout instead:
cp .cargo/config.toml.example .cargo/config.toml # gitignored path override
bundle exec rake compileContributing
Bug reports and pull requests are welcome on
GitHub. Report security
problems privately — see SECURITY.md, which also maps upstream
advisories to the gem versions they affect. Please include a
failing spec with bug reports where possible and keep pull requests focused on
one change; PRs target main. When the public API changes, update
sig/microsandbox.rbs and CHANGELOG.md in the same change, and run the local
gate (cargo fmt/clippy, standardrb, unit specs) before pushing.
License
Released under the Apache License 2.0.