Start a hosting plan from $3.92/mo and get a free year on Tuts+ (normally $180)
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.
- 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
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:
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.
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.
On a Mac, open up Terminal, which can be found within the Applications > Utilities folder. Type the following command into the window:
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
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
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
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
Open up the RVM, straight from its repository on GitHub.
curl -L get.rvm.io | bash -s stable --rails
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.
Testing the Installations
You should now have successfully installed Ruby, and Rails. You can test for Ruby by typing the following command.
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.
You can also test for Rails by typing in the following command.
gem install execjs
As stated on the ExecJS GitHub repository, ExecJS supports all of the following runtimes:
- 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.
Begin by creating a new Rails project, called
rails new tutorials
Go to the new directory you just created:
You can now create a simple scaffold, called
Steps by running the following command:
rails g scaffold Steps name:string form:string
Now migrate the database using the following command:
Start the Rails server:
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.
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!