The other day I needed some kind of Rails hosting in Sweden. There are plenty of good VPS providers (like Glesys or iPeer) but this was for a little experiment and I didn’t want to pay for hosting when I have a perfectly capable 100/100 line at home. So I pressed my Raspberry Pi into service! The default ruby version on Raspbian is 1.9.3 and there are precompiled packages available if you want to run Rails 3.2, but my little app was built using more recent software (Ruby 2.0.0 and Rails 4.1) so I wanted my Pi to use those as well. Turns out it wasn’t that hard!
I grabbed the latest image of Raspbian and installed it to my Pi. This is well documented elsewhere so I won’t go into detail. I made sure to enable sshd in the configuration tool so I could access it from my MacBook.
Update apt-get and grab some packages
First thing I did was to SSH into my Pi and update apt-get:
sudo apt-get update
Then I installed a bunch of dependencies I was going to need for the compilation:
sudo apt-get install screen mysql-server libmysqlclient-dev libssl-dev libreadline-dev libncurses-dev
They’re mostly just dependencies for building Ruby - since I’m going to use mysql as the database, I need the dev libraries for Ruby to link to. Since the Pi compiles so slowly I also installed GNU Screen but more on that later.
My preferred version manager is rbenv so I cloned that to my pi user’s directory:
git clone https://github.com/sstephenson/rbenv.git ~/.rbenv
Then I added the necessary paths and shims:
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.profile
echo 'eval "$(rbenv init -)"' >> ~/.profile
…and installed ruby-build:
git clone https://github.com/sstephenson/ruby-build.git ~/.rbenv/plugins/ruby-build
Building Ruby takes a long time on a Raspberry Pi. That’s where Screen comes in handy:
screen -dmSL rubybuild rbenv install 2.0.0-p576
I let the build process run in a detached screen so I could go do other things and not have to keep tabs on it. I really have no idea how long it took but I’m assuming at least an hour. It was done when I checked on it after a good night’s sleep. If you want to check on its progress you can do
tail -f ~/screenlog.0 or reattach to the screen with
screen -r although ruby-build is fairly quiet and won’t say much, unless it fails. There’s a good tutorial for using screen here if you’re interested.
Enabling the new Ruby version and installing Rails
There are many ways of versioning and using Ruby and I’m lazy, so I just did
rbenv global 2.0.0-p576
That made my shiny new ruby version the default. Time to install Rails! Again, this takes forever to build so off into a detached screen it goes:
screen -dmSL geminstall gem install rails --no-ri --no-rdoc
rbenv rehash later and I had a working Rails install!
The Pi performs surprisingly well. I’m running nginx as a reverse proxy in front of Unicorn (which perhaps would merit another blog post), and the site is a little slower than your average off-the-shelf VPS server but for low-traffic applications it does the job admirably and cheaply. I’m not sure what class my SD card is, it might be worth upgrading to a class 10 for a little speed boost. The downside so far is that deploying (and especially bundling or precompiling assets) takes a lot of time. But it’s a very impressive little machine and you can apt-get or compile pretty much all the packages you’d expect to see in a commercial cloud server. And now it’s quietly and happily serving Rails pages from my living room!