Advertisement

How to Install Ruby on Rails on Raspberry Pi

by

Ruby on Rails is quickly becoming more and more popular every day. Learning to develop in the Ruby language is a very valuable skill. In this tutorial I’ll show you how to install Rails onto a Raspberry Pi.

Requirements

  • Raspberry Pi (Model B)
  • SD Card (running Debian Squeeze)
  • Micro-USB Power Cable
  • Ethernet Cable or USB Wi-Fi Adapter
  • USB Keyboard
  • USB Mouse (preferable, but not required)

Setting Up SSH

Step 1

Using SSH, or Secure SHell, will allow control the Raspberry Pi from another computer. In order to enable SSH, you must obtain the IP address of the Pi. To do so, type the following command into the LXTerminal application:

ifconfig

You'll then see something similar to this:

If you're using a wireless connection, in the wlan0 section, the IP address can be found on the second line, labelled inet addr. In this case, my IP address is 192.168.1.80.

If you're using an ethernet connection, the IP address of the Pi can be found in the eth0 section.

Step 2

Enable SSH and then restart the Pi by typing the following commands into the LXTerminal window:

sudo /etc/init.d/ssh start
sudo apt-get install ssh
sudo shutdown -r now

You should enter each line one at a time, and press the Return key after each line has been entered.

Step 3

On a Mac, open up Terminal, which can be found within the Applications > Utilities folder. Type the following command into the window:

ssh pi@192.168.1.80

You should replace 192.168.1.60 with your own IP address. You'll then be asked for the password. Unless you've previously changed it, the password will be raspberry. Whilst you won't see each character being typed on the screen what you're typing is being entered.

If a security warning is displayed, double check that everything is ok, and then type either y or yes to continue the connection.

If you've connected successfully to the Pi, you'll see pi@raspberrypi ~ $ being displayed at the bottom of the Terminal window, similar to this:

Installing the Essentials

Step 1

You should begin by installing the prerequisites, to prepare for the installation of Rails. Type the following command into the Terminal window, once you've connected via SSH.

sudo apt-get install -y git curl zlib1g-dev subversion

Step 2

If you come across a 404 Error, you'll likely need to update the package index, and this can be done using the following command.

sudo apt-get update

Step 3

Whilst you're already getting the required packages, you'll need to retrieve the SSL package, SQL database package, and more.

sudo apt-get install -y openssl libreadline6-dev git-core zlib1g libssl-dev
sudo apt-get install -y libyaml-dev libsqlite3-dev sqlite3
sudo apt-get install -y libxml2-dev libxslt-dev
sudo apt-get install -y autoconf automake libtool bison

Step 4

Open up the RVM, straight from its repository on GitHub.

curl -L get.rvm.io | bash -s stable --rails

Step 5

Once you've successfully installed the required packages, and have opened up the RVM, it's recommended that you run a script, just so that you can enable Ruby.

source ~/.rvm/scripts/rvm

Testing the Installations

Step 1

You should now have successfully installed Ruby, and Rails. You can test for Ruby by typing the following command.

ruby -v

If installed correctly, you'll see a message confirming which version of Ruby is installed, when it was produced, and what it's using in order to work correctly.

Step 2

You can also test for Rails by typing in the following command.

rails -v

JavaScript Runtime

Step 1

As Rails required a JavaScript runtime, you'll need to install a new Ruby gem called ExecJS, and then install a JS package. You should begin by installing the gem.

gem install execjs

Step 2

As stated on the ExecJS GitHub repository, ExecJS supports all of the following runtimes:

  • therubyracer
  • therubyrhino
  • Node.js
  • Apple JavaScriptCore
  • Microsoft Windows Script Host

Install Node.js, by typing in the following command:

sudo apt-get install nodejs

Creating a Simple Project

In order to test that Rails is fully functional on the Raspberry Pi, create a simple project, by adding a new project, going to its directory, adding a scaffold, migrating the database, and then starting up the server.

Step 1

Begin by creating a new Rails project, called tutorials.

rails new tutorials

Step 2

Go to the new directory you just created:

cd tutorials

Step 3

You can now create a simple scaffold, called Steps by running the following command:

rails g scaffold Steps name:string form:string

Step 4

Now migrate the database using the following command:

rake db:migrate

Step 5

Start the Rails server:

rails server

Step 6

If all's gone well, you'll see the new project in action in the browser by typing the IP address, followed by the port number (3000 for Rails), and then by the project name. For me, that'd be the following address.

http://192.168.1.69:3000/tutorials

Summary

That's it! You've successfully installed Ruby on Rails on a Raspberry Pi!

You began by enabling SSH, then installing the required software in order for Rails to function properly.

The best way to find out what you can do with your new Rails Server would be to play around with it, and see what it can do!

Advertisement