Cosmopolitan Libc
Justine Tunney's Cosmopolitan Libc produces 'Actually Portable Executables' (APE) — a single binary that runs natively on Linux, macOS, Windows, FreeBSD, OpenBSD, NetBSD, and BIOS via clever polyglot headers. No VM, no runtime, native performance. Used by Mozilla's llamafile.
**Cosmopolitan Libc** is a C standard library written by Justine Tunney that produces **Actually Portable Executables (APE)** — single binary files that run natively on Linux, macOS, Windows, FreeBSD, OpenBSD, NetBSD, and even via BIOS boot, with no virtual machine, interpreter, or runtime layer. ## How it works The APE format uses a clever polyglot header trick: - The binary starts with a shell script (`#!/bin/sh`) whose bytes also form a valid DOS/PE header. - Linux/BSD loaders parse it as a shell script that re-executes the file; the script also serves as a valid ELF shim. - Windows sees the MZ/PE header at the start and runs it as a native Windows executable. - macOS sees the embedded Mach-O header and runs it as a native Mach-O binary. - All sharing the same code bytes. No interpreter. No JIT. No runtime layer. Native instructions executed directly. The trade-off is that the binary must be built to a portable x86-64 (or lately ARM64) base instruction set, and OS-specific operations are dispatched via libc shims Cosmopolitan provides. ## Size and performance A 'hello world' APE is ~50-70 KB. A fully-featured Python interpreter built with Cosmopolitan (Python.com, called Redbean / Actually Portable Python) is ~8-12 MB for a complete Python + tons of bundled libraries, running everywhere at native speed. ## Notable users - **Mozilla's llamafile**: wraps a LLM weights file with a Cosmopolitan-compiled llama.cpp binary into a single file that runs any LLM locally on any OS. A 4-7 GB file you double-click. Killer demonstration of the 'one binary, any OS' value proposition. - **Redbean**: single-file web server with SQLite, Lua, mbedTLS, and ZIP support built in. ~2 MB, runs anywhere. - **APE-cross**: cross-compile tools that produce APE output. ## Contrast with WebAssembly and WASI Cosmopolitan answers 'one binary, native speed, no runtime' at the executable-format layer. Wasm answers 'one bytecode, any hardware, sandboxed, small runtime' at the virtual-ISA layer. Cosmopolitan is faster with zero sandbox; Wasm is safer with a tiny runtime tax. They aren't competitors — they solve different parts of the portability problem. The shared lesson: the historical trade-off between portability and performance was overstated. Clever engineering can get both. ## Justine Tunney Justine is a long-time Google engineer (ex) and prolific systems hacker. Other notable work: blink (the tiniest x86-64 Linux emulator), sectorlisp (Lisp in 512 bytes), contributions to llama.cpp performance on CPUs. Her work consistently demonstrates what older unix-philosophy engineering looks like applied to modern problems.