I’ve taken it upon myself to start learning a new programming language and leaving my happy little LAMP (or preferably LNMP) comfort zone. I love PHP and how easy it is to get up and running. Its pretty simple and gets the job done quick. PHP unfortunately lacks a lot of the “grace” and modern nuances that a more modern platform such as Ruby on Rails provides.
Now, before the pitchforks start coming out I already know that I’m kind of comparing Apples to Apple Pie. PHP is a language with a ton of community support and Ruby on Rails is a framework. I get it, but there’s a lot to be desired in PHP land. PHP has some good frameworks as well – CakePHP, Zend, CodeIgniter, Yii, etc. – but the sheer variety creates a lot of discontinuity in the community and a lot of the frameworks out there are just Ruby on Rails wannabe’s anyways. More importantly besides any of that, PHP is getting a little boring and I want to learn something new 🙂 So, I’m starting to learn Ruby, Sinatra and eventually Ruby on Rails and share my learning here.
So, at work I use a Mac, at home I use a PC and I dual boot between Windows 7 and Ubuntu. I personally prefer Ubuntu, but sometimes I have to have access to Photoshop and Photoshop through Wine sucks. From what I’ve found, the best way to run a development Ruby environment is through a VM installation of Ubuntu Server. It can be a little tricky to setup, but I’ve chronicled how to get yourself going with a Ruby development environment that will work on an Ubuntu server and Mac OS (variations in installation noted where appropriate).
Getting Started on a Mac
First things first if you’re running a Mac – get your system setup for development. Install a few key tools to get your system ready for developing and utilizing the command line:
- Install XCode from the AppStore
- Install Homebrew to give you access to a missing app repository that gives you the ability to install tools for development much like Ubuntu’s apt-get system.
RVM (Ruby Version Manager) will allow you to maintain individual version controls of Ruby, Rails and Gems on a per-project/folder basis. Very useful when working on multiple projects of varying maturity.
- Install RVM itself.
123curl -L https://get.rvm.io | bash -s stable --auto
This will install RVM in your user’s folder and will allow you to run any future
gemcommands without the need for
sudo. For a few steps later this is important to keep in mind as the version of Ruby installed by RVM is the one that will need to be used, not the version of Ruby that comes with your operating system.
- Install Git.On a Mac, install this with Brew:
123brew install git
On Ubuntu, install with the apt-get command:
123sudo apt-get install git
- Reload Your Bash Profile. After installing Git and RVM, you’ll want to reload your Bash profile and your local path variables:
- Install RVM Dependencies.Now that you’ve installed RVM, you’ll need to install any dependencies. Luckily RVM will tell you exactly what you need to install. Just run:
Read the instructions on screen to learn what you need to make sure is installed for your environment to work.
- Install Ruby.With RVM installed you can install Ruby easily by running:
123rvm install 1.9.3
This will take a while, so go grab a coffee.
- Set RVM’s Default Ruby Version.Check the version of Ruby that was installed by running
ruby -v. This will output your installed major version of Ruby including the patch number that will look something like:
123ruby 1.9.3p327 (2012-11-10 revision 37606) [x86_64-linux]
Set RVM to use the version seen there by running:
123rvm --default use 1.9.3-p327
Take note of the added “-” between the major version number and the patch number.
With RVM installed you’re ready to run Ruby on your machine, the next step is installing the Rails framework. This can easily be accomplished with our recently installed gem library that was installed with Ruby.
- Install Rails.Simply run:
123gem install rails -v 3.2.9
123brew install node
123sudo apt-get install nodejs
- Ubuntu Preparation.On Ubuntu machines there are some ancillary packages to install if you plan on running Ruby on Rails with MySQL or PostgreSQL databases in your local development environment. Install these with the following command:
123sudo apt-get install libmysqlclient-dev libpq-dev
Install Phusion Manager
If you have a LAMP or LNMP stack already installed on your machine you can also install Phusion Passenger for easy Virtual Host management in your development environment. This will allow you to simply create virtual hosts and run multiple Ruby on Rails, Sinatra or other Rake/Ruby based applications at the same time accessible via their virtual host domain maps. There are a few tricks to note about running Phusion Passenger with RVM, so be sure to follow the modified instructions here for installation:
- Install Phusion Passenger. Go to https://www.phusionpassenger.com/download and follow the installation instructions at the bottom of the page for your LAMP or LNMP stack with one major exception – do not run ANY of the commands with the sudo command. Since you installed RVM earlier, you want Phusion Passenger to work with your RVM installation not your operating system’s default Ruby installation.
- Install the Passenger Module. After running the appropriate passenger module installation instructions from step 2 on the Phusion Passenger installation page, be sure to follow any instructions given by the installer for adding lines to your Apache configuration files. These lines will automatically be set to use your RVM installed copy of Ruby so you don’t need to modify them. Base your virtual host configurations based off the example given by the Passenger installer. You can also consult the Phusion Passenger documentation for more details – Apache or Nginx – for more detail on setting up for various frameworks.
- Configure Your Local Environment. By default Rails and Rack environments are set to production when using Passenger. If you are running Apache you can simply set the environments to development in your domain’s
123456# Ruby on Rails environmentSetEnv RAILS_ENV development# Rack environment (Sinatra and other Ruby frameworks)SetEnv RACK_ENV development
Alternatively, this can go in an .htaccess file when running Apache or even right next to your Passenger module load lines if you want all your environments to be development.
- Speed up Passenger.Out of the box, Passenger is slow as mollasses. This of course negates much of the benefit of running something like Passenger, so lets speed things up. Add these lines next to your Passenger module load lines:
12345678910111213141516171819202122232425262728# Speeds up spawn time tremendously - if your app is compatible# RMagick seems to be incompatible with smart spawning# Older versions of Passenger called this RailsSpawnMethodPassengerSpawnMethod smart# Keep the application instances alive longer in secondsPassengerPoolIdleTime 1000# Keep the spawners alive, which speeds up spawning a new# Application listener after a period of inactivity at# the expense of memory.RailsAppSpawnerIdleTime 0# Automatically hit your site when apache starts, so that you# don't have to wait for the first request for passenger to# "spin up" your application. This even helps when you have# smart spawning enabled.PassengerPreStart http://www.mywebsite.com/PassengerPreStart http://www.myotherwebsite.com:3000/# Minimum number of application instances that must be kept# around whenever the application is first accessed or after# passenger cleans up idle instances. With this option, 3# application instances will ALWAYS be available after the# first request, even after passenger cleans up idle onesPassengerMinInstances 3
See the original StackOverflow article for more information on what this does.
- Special Modifications For Virtual Box Installations. If you have installed Ubuntu Server in a Virtual Box VM and have your development workspace available to the server as a permanent Shared Folder, you will need to make some user permission modifications. As the Shared Folder mount will be owned by the
vboxsfgroup, you will need to make sure that both Apache and Phusion Passenger are run as part of that group. Modify the
APACHE_RUN_GROUPvalue to be
/etc/apache2/envvarsfile to tell Apache to run itself as a user belonging to the
vboxsfgroup. To tell Phusion Passenger to run under a different group, just add the line
PassengerGroup vboxsfright next to the other lines you added to your Apache configuration in step 2.
That’s it! You should now have a fully functional development environment ready for Ruby on Rails