Simplest Possible Ruby Web Server

posted 2012-Mar-21
— updated 2013-Sep-16

Courtesy of Aaron Paterson, here’s a 31-character command line that runs a web server from the current directory on port 5000:

ruby -run -e httpd -- -p 5000 .

Thanks to Benjamin Oakes for bringing this shorter solution to my attention, and for his writeup explaining it.

For posterity, here’s my previous 80-character version (which assumes that you have the Ruby gem thin installed):

ruby -rrack -e "include Rack;{run''}"

And, drbrain on EFNet IRC suggested:

# Needs sudo since it defaults to port 80
sudo ruby -rwebrick -e ''

However this code—while shorter—cannot be simply killed with ctrl-c and thus fails my need to easily start and stop the web server.

09:39PM ET

Does this work on windows?

Gavin Kistner
09:41PM ET

@Harold Sure does!

Eric hodel
11:21PM ET

ruby -rwebrick -e ' ".").start'

Benjamin Oakes
01:50PM ET

Here’s a one-liner, built into Ruby:

ruby -run -e httpd . -p 5000

I wrote up a blog post explaining how it works.

12:43AM ET

Comically, while ctrl-c has the aforementioned poor behavior on Windows, the WEBrick server can be killed effectively with ctrl-break.

Because workarounds.

net.mind details other résumé contact