Setting Up A Ruby On Rails Dev Enviroment in Ubuntu For PHP Developers

So I am trying to jump on the ruby bandwagon and just spent a few days trying to understand how to configure a dev machine.

Now, being a PHP developer I am spoiled by the relative easiness to set up a dev environment. The options are abundant: WAMP servers, the manual install is very easy, etc.

In a basic PHP Set up you need to perform the following:

  • Install Apache
  • Install PHP
  • Install MySQL

in windows this is very easy and just minimal set up is required. Now Setting something similar for ruby in Ubuntu… well, that’s why I am writing this.

What’s going on? why ubuntu?
So let me start by explaining why I am using ubuntu. For what I have read online you can sense that rails and ruby are very much at home in a linux environment. Coming from a windows world and WHM in Centos, I wanted something that would be easy to live with on a daily basis. That’s why I chose Ubuntu.
Now, if you are like me and have no concept of how ruby and rails works, then the tutorials for setting up rails seem so weird. First out of the bag is this thing “rails server” that makes your web app run in port 3000. I am used to setting a directory, copying my favorite framework (CodeIgniter) and that’s it. Then they feed you all this unnecessary information about proxing and stuff.. yuck. In reality things are much simpler, like in the php world you need the language, the framework, a webserver and a database. They just are set up in a different order. You do need one more thing, phusion passenger, a web/app server. Apache request to your app are passed to passenger.

What’s First?
First, we need to install ruby. The easiest way is to use RVM (Ruby Version Manager)
I took some of the instructions from this site How To Install Ruby on Rails…
To Install curl and RVM

sudo apt-get update
sudo apt-get install curl
curl -L https://get.rvm.io | bash -s stable

The author recommends exiting the shell session and starting a new one before the next steps

source ~/.rvm/scripts/rvm
rvm requirements

EDIT: If you are planning on using rails directly on this machine (i.e. not connecting using putty) please follow the following instructions gnome terminal integration

Then we install Ruby (I am using the latest version as of July 2014)

rvm install 2.1.2
rvm use 2.1.2 --default

you can verify the ruby version

ruby -v

Setting Up MySQL
Installing MySQL is relatively easy. Make sure to write down your sql password that is set up during the MySQL install

sudo apt-get install mysql-server mysql-client
sudo apt-get install libdbd-mysql-ruby libmysqlclient-dev
gem install mysql

Installing Rails
Now using RVM we get the latest gems required for rails, think of these as extensions for php

rvm rubygems current

Finally, install rails

gem install rails

because we installed rails as a gem we need to install node.js

sudo apt-get install nodejs

We now have a database, a language and a framework installed. Now we need a webserver.

Installing Apache

sudo apt-get install apache2 

Start apache

sudo service apache2 start

Now we need to make Apache serve your app using passenger

gem install passenger
sudo apt-get install apache2-dev libapr1-dev libaprutil1-dev
passenger-install-apache2-module
sudo service apache2 restart

pay close attention to the passenger-install-apache2-module, this is a guided install that requires you to do some extra work to configure Apache. The installer is very thorough and easy to follow

Since, for now, I only have one app to work on; I made the /etc/apache2/sites-available/000-default.conf point to the app I am working on, that way, I can access the app by going to http://localhost in my browser. Make sure your public app folder has enough rights

NOTE: Because all these instruction set up a developer environment, you need to add this line to your virtualhost in apache: PassengerAppEnv development
Like so:

<VirtualHost *:80>
    PassengerAppEnv development
...

Otherwise you might get a 500 Internal error

Hope this tutorial helps.

3 Comments


  1. Pretty good write up, but I don’t agree that setting up Rails in *nix is more difficult than setting up PHP in Windows. I think some of these steps can be avoided or simplified. For example, you could use SqlLite instead of MySql to avoid having to worry about remembering passwords or creating users, and Rails comes built-in with a SqlLite driver. Rails is also configured by default to use SqlLite. I’m not sure why Apache comes in, either. Rails has a built-in web server called WEBrick, and it does nifty things like allowing you to change your Ruby code without restarting the server. And, let’s be real, configuring Apache is no fun for anybody.

    The cool thing about Rails is once you have it set up for development, it is easy to change to your prod set up. For example, changing from SqlLite to Mysql is just a change in a config file.

    Glad to see you want on the Rails bandwagon. It’s funny, I was just thinking about doing a little piddly registry app like you suggested a while ago, and I decided to procrastinate instead. How surprised I was when I came to your site and you were writing about Rails… 🙂 Great minds think alike.

    Reply

    1. Nelson you are such a slacker…. tsk tsk I am using MySQL because that is what I know and love. I also read in one of the tutorials (can’t remember which) that MySQL is best suited for production. I know this is a dev environment but I wanted to set it up as I would PHP.

      In regards of WEBrick, first time I hear about it. I will read more about it. Also remember the title of my article “For PHP Developers”. So in the spirit of a LAMP set up, I included apache.

      I have to say that I have been reading a while on rails and it is hard to get to a cozy state where you understand what’s going on. By setting up the environment the way I did, it gives me enough familiarity to say “Ah, ok I see what you did there!”

      Are you going to put your project in github? let me know so I can check it out

      Reply

Leave a Reply

Your email address will not be published. Required fields are marked *