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;Handler::Thin.run Builder.new{run Directory.new''}"

And, drbrain on EFNet IRC suggested:

# Needs sudo since it defaults to port 80
sudo ruby -rwebrick -e 'WEBrick::HTTPServer.new.start'

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.

Harold
09:39PM ET
2012-Mar-21

Does this work on windows?

Gavin Kistner
09:41PM ET
2012-Mar-21

@Harold Sure does!

Eric hodel
11:21PM ET
2012-Mar-21

ruby -rwebrick -e 'WEBrick::HTTPServer.new(DocumentRoot: ".").start'

Benjamin Oakes
01:50PM ET
2013-Sep-20

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

ruby -run -e httpd . -p 5000

I wrote up a blog post explaining how it works.

Harold
12:43AM ET
2014-Mar-18

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 contact résumé other
Phrogz.net