Unbelievably clever: Redbean 2 – a single-file web server that runs on six OSes
'Write once, run anywhere' finally came true, thanks to APE and the Cosmopolitan libc
A bunch of almost unbelievably clever tech tricks come together into something practical with redbean 2: a webserver plus content in a single file that runs on any x86-64 operating system.
The project is the culmination – so far – of a series of remarkable, inspired hacks by programmer Justine Tunney: αcτµαlly pδrταblε εxεcµταblε, Cosmopolitan libc, and the original redbean. It may take a little time to explain what it does, so bear with us. We promise, you will be impressed.
To begin with, redbean uses a remarkable hack known as APE, which stands for Actually Portable Executable – which its author styles αcτµαlly pδrταblε εxεcµταblε. (If you know the Greek alphabet, this reads as "actmally pdrtable execmtable", but hey, it looks cool.)
The gist of the idea of APE is this: whatever programming language you use, if you compile your code to a native binary, it produces x86-64 machine code – and any x86-64 processor runs the same machine code. If you wrote a program that didn't call on any external code at all, then in principle, the only difference between a Windows binary and (say) a Linux binary is the format of the file that contains the binary. So if you could only get around that, your program would be able to run on any OS.
Linux uses a format called Executable and Linkable Format (ELF). In the old days, proprietary x86 Unixes used Common Object File Format or COFF. That's why the recent conversion of Lotus 1-2-3 for Unix to Linux used a tool called
coffsyrup to convert COFF to ELF.
The joke in the name APE comes from the fact that Windows NT still uses a format derived from the old COFF standard, called Portable Executable (PE). The PE format is "portable" only inasmuch as if you run a Windows
.EXE file under DOS, it will in fact execute: PE files contain a tiny DOS stub which displays the message "This program cannot be run in DOS mode." But "Portable Executables" aren't really portable at all – unlike, say, family applications, which ran natively under both DOS and OS/2.
APE files are, as the name says, Actually Portable Executables. A single APE file executes correctly on any x86-64 operating system. Take the file and run it on Windows, then it acts as a Windows executable. Run the same file on macOS, and it's a macOS executable. Run it on Linux, and it's a Linux binary… and the same on FreeBSD, and OpenBSD, and NetBSD, even though they all have their own, different, executable formats.
The snag is that almost any real program has to call out to some library, even if only the C standard library… which is where the Cosmopolitan libc comes in. "Cosmo" is a libc for APE, so that C programs can be compiled and run on any OS that APE supports.
With Cosmo and the APE format, you can write a C program and compile it to a single file that will load and run on six totally different operating systems. Oh, and if that wasn't enough, the same binary can be booted directly from the PC BIOS, as well. Which means you can run it under Tunney's Blinkenlights, and watch the PC's registers changing live, like an old-time mainframe.
At this point, many techies' minds may well be reeling in astonishment. So, to give you something pretty to look at, there are some demo programs, too. The demo video on the Blinkenlights page shows printimage.com, which can print JPG files on the text console. If you are still not impressed, printvideo.com does the same for video.
The original redbean made the combination of APE and Cosmo into something slightly more useful. After all, amazing as the combination is, having just a libc is quite limiting in the 21st century. You can't build any kind of graphical application, for instance. But every OS that APE supports can run a web browser, so redbean embedded a web server (and SQLite database, and Lua scripting) into a single binary file too. All you need to do is change the file extension to
.ZIP, add in your static web content using an archive manager, save it, rename it again, and bingo: self-contained web app.
Think of it as being a little like an Electron app, except that instead of embedding the entire browser into your innocent little program, you use your existing browser.
Now, redbean 2.o (yes, that does say two-dot-oh, not two-dot-zero) takes the original redbean and adds in a REPL – for non-Lispy types, an interactive command line – and a Unix SysV API (yes, even on Windows), plus logging and more.
Talking about Lisp, another of Tunney's astonishing projects is SectorLisp, which embeds a Lisp interpreter into a PC boot sector. SectorLisp 2 adds garbage-collection too, making it the smallest GC language in the world. Given this interest in programs that can be booted directly, without an OS at all, it may not surprise you that a future goal for redbean is to make it bootable: to embed a TCP/IP stack and network-card drivers, for a completely standalone tool. ®
If you don't just take our word for it, and want to try redbean for yourself, then it's worth knowing that there are issues on some operating systems. Recent versions of macOS (11 and 12) break some of the trickery that allows APE to work, as does Ubuntu. The Hacker News discussion about the project and its antecedents contain some workarounds.