I am interested in all sorts of software development and related areas. In particular I do a lot of Java and Ruby development, both for my day job and for fun. Once a month I try to get along to IPRUG, our local Ruby user group and meet up with other Ruby software folks for chat, drinks, presentations, hacking and the general exchange of ideas. The current organiser of the group was asking for volunteers to present at upcoming meetings, and in a rush of blood to the head I raised my hand to do a presentation on the Raspberry Pi.
Given that this group all have an interest in Ruby in common, I though I ‘d better have a go with Ruby on the Pi just to make sure there’s nothing to catch me out!
A quick google around led me to a page at elinux.org about Ruby on the Raspberry Pi and a blog post from Ray Hightower about Ruby on the Raspberry Pi. The elinux article offered the option of installing directly from the debian repository, but cautioned that this would probably result in an older version. The rest of the instructions about installing from source looked a bit complex and very version-dependent. Luckily Ray Hightower gave some instructions on how to use the Ruby Version Manager (RVM) to do most of the heavy lifting.
I connected the network cable and booted up my Linux SD card, then logged in over ssh and started by getting RVM itself using curl -L https://get.rvm.io | bash -s stable --ruby
After a slightly puzzling message which asked me to press q to continue, the installation was away and running. It worked the little Pi pretty hard, though Nearly one and a half hours to get rvm and Ruby itself, then another 15 minutes or so for a few gems. After that it gave me back a shell prompt and suggested I either type source /home/pi/.rvm/scripts/rvm, or restart my shell session . After that I could use rvm --version and ruby --version to see the versions of the installations.
Now I just needed to think of something useful or interesting to do with Ruby. Most of my Ruby coding in the past has been web-based, so I thought I’d start with a simple web app. I toyed with the idea of building a very basic app using just Rack, but eventually decided to start with Sinatra instead because it’s an easy way to get Rack, a simple web framework, and some page templating in one fetch. Installing was easy: gem install sinatra
To prove that this works, create a file (I have called it rweb.rb but that’s not particularly important) with the following contents:
require 'sinatra' get '/' do "hello from #{`uname -a`}" end
Note that the quotes round the uname -a are “back quotes“, which lean to the left, not the regular single or double quotes. On my keyboard they are found at the top left, above the tab key.
This creates a simple web application that returns some details about the host operating system when it is run. Sinatra is smart enough to find and run a web server, so all you need to do to run this app is type ruby rweb.rb.
To see the results you need to point a web browser at the running app. It probably told you when it started up that it will be listening on port 4567 but it doesn’t tell you the host name or ip address! The easiest way to find this out is to type ifconfig at a shell prompt (you may need to enter Ctrl-C to stop your web app first, if it is still running.) which will print out something like:
eth0 Link encap:Ethernet HWaddr b8:27:eb:d2:db:1e inet addr:192.168.0.205 Bcast:192.168.0.255 Mask:255.255.255.0 UP BROADCAST RUNNING MULTICAST MTU:1500 Metric:1 RX packets:25852 errors:0 dropped:0 overruns:0 frame:0 TX packets:11387 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:1000 RX bytes:17218117 (16.4 MiB) TX bytes:1208292 (1.1 MiB) lo Link encap:Local Loopback inet addr:127.0.0.1 Mask:255.0.0.0 UP LOOPBACK RUNNING MTU:16436 Metric:1 RX packets:8072 errors:0 dropped:0 overruns:0 frame:0 TX packets:8072 errors:0 dropped:0 overruns:0 carrier:0 collisions:0 txqueuelen:0 RX bytes:2384444 (2.2 MiB) TX bytes:2384444 (2.2 MiB)
The key bit of information for this case is on the second line inet addr:192.168.0.205. Yours will probably be different, it depends on the address given to the Pi by DHCP.
Once you have this number, you can restart your web app, then point a browser at your address, eg. http://192.168.0.205/. If it works, you should see something that looks like:
hello from Linux raspberrypi 3.2.27+ #250 PREEMPT Thu Oct 18 19:03:02 BST 2012 armv6l GNU/Linux
Congratulations. You have installed Ruby and Sinatra, and built and run your first web application on the Raspberry Pi.
That wasn’t so hard, was it!
Pingback: Controlling Raspberry Pi GPIO from Ruby | Raspberry Alpha Omega
Hi, nice write up! I followed the same steps as you (except I didn’t use RVM), and the Sinatra starts up fine, but when I try to access it over the network the page just doesn’t load. Are you using Raspbian on your Raspberry Pi? Thanks!
I definitely used Raspbian. I think it was the December 2012 “Wheezy”.
I’ll have another go at this, though – I have been so caught up in hardware tinkering that fun with Ruby has taken a back seat.
Joey,
Try adding
set :bind,’0.0.0.0′
to your ruby script, which 0.0.0.0 is your ip address.
Hi!
I did the same thing and I get an answer from Sinatra, but it’s painfully slow, the page takes like 8 seconds to get displayed (Python with Bottle is almost instantaneous in comparison).
Do you have the same issue?
I’m using Raspbian Wheezy. And I added the set :bind, “0.0.0.0” to make sure the webserver listens on all the interfaces.
OK, just to add, it’s very fast when I try to reach the page right from the raspberrypi, with raspberrypi:4567.
The reverse is not an issue (webserver running on my laptop and accessed from the raspberrypi).
I had the same issue, try another server, puma worked as expected.
I tried out Rails and it was terribly slow. I was hoping the lightweight Sinatra would be an improvement but it sounds like it’s not worth trying.
Great Article!
Any idea how to launch this server on startup?