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:
1
| 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:
1
2
3
| 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:
1
| 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.
1
| 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.
1
| 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.
1
2
3
4
| 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.
1
| 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.
1
| 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.
1
| 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.
1
| 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.
1
| 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:
1
| 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
.
1
| rails new tutorials |
Step 2
Go to the new directory you just created:
1
| cd tutorials |
Step 3
You can now create a simple scaffold, called
Steps
by running the following command:
1
| rails g scaffold Steps name:string form:string |
Step 4
Now migrate the database using the following command:
1
| rake db:migrate |
Step 5
Start the Rails server:
1
| 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.
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!
No comments:
Post a Comment