ProgFu

programming tips & tricks

Dec 22

Ruby on 64MB RAM

This week I got my hands on 64MB RAM and very little CPU VPS. At first I thought it’s useless since there’s no way I can do anything with this, even the smallest Rails app barely runs on 256MB with nginx and MySQL.

But what about Sinatra? Could we run a small database driven application on such a tiny server? Let’s give it a try!

Let’s try to do this from the begining. First we will install VirtualBox, so nobody will get hurt while playing. I’m not going to cover VirtualBox installation, since it’s pretty straightforward.

After installing VirtualBox, we need to download Ubuntu Server 10.04, we’re going to use the 32bit version.

There are hundreds of tutorials on how to install Ubuntu inside VirtualBox, so I’m not going to cover it here. Only make sure you set 64MB RAM and install Ubuntu Server, not desktop edition.

Feel free to skip this if you want to work with your virtual machine directly, instead of using SSH

The only tricky thing in this part is to set the connection type to Bridged, so that we can easily connect via SSH from our host machine.

After you’re done installing Ubuntu, log in and install openssh-server package

sudo apt-get install openssh-server

find out your ip

ifconfig | grep 'inet addr'

and connect to the virtual machine via SSH from your host system to avoid VirtualBox catching your mouse every time you want to do something in there.

One last note for VirtualBox, and that is snapshots. Creating a snapshot is basically the same thing as doing a commit in a git repository. That means you can try anything and if it breaks, just go back to the last working snapshot.

Setting up Ruby environment

Now that our virutal machine is running, it’s time to install some packages. We’re going to use RVM to install Ruby, so we’ll only need to install some libraries.

We have roughly about 25MB free RAM and 128MB swap to use

$ cat /proc/meminfo  | grep -i free
MemFree:           24656 kB
SwapFree:         125024 kB

so we will have to use our resources wisely.

To make this process extra simple, I’m going to use my own dotfiles with some installation scripts. First we need to install git

sudo apt-get install git-core

next we create ~/bin directory

mkdir ~/bin && cd ~/bin

and download the dotfiles

git clone git://github.com/darthdeus/dotfiles.git
cd dotfiles

I’ve created dead simple installation script, so all you need to do now is run it, and you will get nice bash with configured RVM.

./install.sh

If you’re lucky, you should see the success message. Now check if RVM is installed in your path

$ rvm -v
rvm 1.1.6 by Wayne E. Seguin (wayneeseguin@gmail.com)
[http://rvm.beginrescueend.com/]

Now that RVM is installed, it’s time to get ourselves some rubies. Let’s start with 1.8.7. This might take couple minutes since our virtual machine is so tiny.

$ rvm install 1.8.7
...
Install of ruby-1.8.7-p302 - #complete 

$ rvm use 1.8.7 --default
$ ruby -v
ruby 1.8.7 (2010-08-16 patchlevel 302) [i686-linux]
$ gem -v
1.3.7

Thats it, Ruby is now installed and we still have the same amount of RAM left

Time to install some gems. I have a little warning here though. Installing gems will take up all your ram and around 20-30MB from swap, so it will get VERY slow. Don’t get scared if it takes you 10 minutes to run the following command.

$ gem install sinatra

Background gem install

Feel free to skip this step if you’re using virtual machine instead of live VPS.

For live VPS, you might consider running install on the background and disowning the process, so that you can disconnect from SSH and let the installation run on it’s own.

$ gem install sinatra > gem.log &
[1] 1101
$ disown %1

After you log back in, you can just check if the process is still running

Assuming there is no other ruby process, which there shouldn’t be if you followed the instructions

$ ps -C ruby
PID TTY          TIME CMD
1191 pts/0    00:00:08 ruby

$ cat gem.log
Successfully installed sinatra-1.1.0
1 gem installed

Sinatra Application

What a tutorial would this be if we didn’t create a hello world application

$ cat > app.rb
require 'rubygems'
require 'sinatra'

get '/' do
  "Hello, world!"
end
<ctrl-D>

$ ruby app.rb
== Sinatra/1.1.0 has taken the stage on 4567 for development with backup from
WEBrick
[2010-12-18 13:47:53] INFO  WEBrick 1.3.1
[2010-12-18 13:47:53] INFO  ruby 1.8.7 (2010-08-16) [i686-linux]
[2010-12-18 13:47:53] INFO  WEBrick::HTTPServer#start: pid=1299 port=4567

Now just open new terminal, connect with SSH and test the server out.

$ curl localhost:4567
Hello, world!

And we’re done. We have successfuly created our very own web application running under 28MB RAM (second SSH connection is around 3MB).

Don’t hang up on me yet. To make this little more interesting, let’s try to nuke the application with Apache Benchmark and see what happens.

First check if you have Apache Benchmkark installed

$ ab -V
This is ApacheBench, Version 2.3 <$Revision: 655654 $>
Copyright 1996 Adam Twiss, Zeus Technology Ltd, http://www.zeustech.net/
Licensed to The Apache Software Foundation, http://www.apache.org/

If you don’t, you can get it from the apache2-utils package.

You should do all the benchmarking from your host machine to avoid consuming additional resources from the VM.

# Change 10.0.0.4 to your VM local ip
$ ab -n 50 -c 5 http://10.0.0.4:4567/ | grep Requests
Requests per second:    7.50 [#/sec] (mean)

Looks like we’re doing around 7.5 requests per second, which isn’t that bad considering we’re not using any web server.

In the second part of this tutorial, we’ll try to add database, production web server and also take a look at some more detailed benchmarking using Apache Benchmark


  1. theeasytech reblogged this from progfu
  2. theeasytech reblogged this from progfu
  3. progfu posted this
Page 1 of 1
Bloggers - Meet Millions of Bloggers
Web Analytics