In my post, Install CakePHP on Ubuntu 10.10 and 11.10, we took an in-depth view into installing CakePHP on Ubuntu 10.10. Since then, I’ve received some emails that collectively critiqued my choice of foundational programming language and suggested Ruby on Rails would be more well-received. PHP has historically been a bit of a security nightmare, so I am not in disagreement. It’s earned a reputation for being easy to learn but difficult to secure and so flexible that it allows developers to write messy, vulnerable code.
I’m not the sort of person that wastes too much time engaging in debates over development frameworks. I like to keep things objective and supported by data. I like the structural organization and community spirit spurred by Ruby on Rails. The way I see it, if a framework is good it will thrive, and if it isn’t good it won’t. Survival of the fittest is as powerful in the world of technology as it is in nature and capitalism. That said, both PHP and Ruby programmers have the luxury of choosing their own rapid development framework whether it be CakePHP or Ruby on Rails, respectively, so both communities are currently thriving. That makes me happy too, because it encourages healthy competition, and this competition only leads to bigger and better advances for the web as a whole.
For those of you interested in developing web applications using Ruby, this week we’ll look at how to install Ruby on Rails, the framework that inspired the creation of CakePHP. It’s not a stretch to say that if Ruby on Rails had not existed, with its RESTful design practices and file structure, CakePHP may not have ever come into being. One could argue CakePHP is a PHP-centric response to Ruby on Rails, as from a structural standpoint, CakePHP borrows a lot from it.
Unlike CakePHP, Ruby on Rails (which we’ll herein refer to as simply, “Rails”) does not require Apache (at least for development). It actually installs with a small Ruby-based web server called WEBrick that runs on port 3000. WEBrick isn’t very well documented, and it’s not the greatest web server (it’s not even intended for a production environment). That said, WEBrick is small and portable, which makes it nice for developing Rails apps and learning the framework.
This install guide begins immediately after a base install of Ubuntu has been completed. We will step through installing the fundamentals, and end with a working install of Rails, ready for development work.
Phase I: Basics
One of the coolest things about installing Rails is that unlike CakePHP, Rails can be installed with relatively few prerequisites. In fact, we don’t even need Apache or MySQL. Also, though using a package manager like apt-get
to download software in Ubuntu typically makes the most sense, when setting up Rails, I cannot think of a worse option. apt-get
pulls software down from the Ubuntu repositories, and these repositories typically do not have current versions of the software that comprises a solid Rails development install. That’s not to say we won’t use apt-get
, because we will, but only to download and install some supporting software prior to getting to the heart of the actual Rails install.
The supporting packages are:
- Git: we’ll be using git as a foundation for
rvm
- curl: this will be used to download
rvm
- build-essential: a set of packages used for compiling source code
- OpenSSH Server: for convenient remote shell access
To install the aforementioned packages, all we need to do is access our terminal and type:
regularuser@smalleycreative:~$ sudo apt-get install git curl build-essential vim openssh-server
Phase II: Get rvm
Once the base packages finish installing, we can being the heart of our Rails install by installing rvm
, the Ruby Version Manager. This is done by typing:
regularuser@smalleycreative:~$ bash < <( curl http://rvm.beginrescueend.com/releases/rvm-install-head )
Notice we didn’t use sudo
at all in this command. rvm
installs to our home directory, which we have full access to as a non-root user, so no root privileges are required! This is a plus on systems with multiple users. Every user on a server can install rvm
to their own home directory, and each user can have their own custom development setup with whichever version of Ruby they wish without interfering with any other users.
Once rvm
is downloaded, we’re going to need to add a line to our .bashrc
file. The .bashrc
file is a file that loads every time we run bash
(the default shell in Ubuntu). Each user gets their own .bashrc
file in their own home directory. Inside this file, we can customize what runs every time a new bash session is started. While the modifications we can make to our.bashrc
file are virtually limitless, for this situation, we need to add a line telling it that when we open bash
, we want rvm
to be available to us. We can do this without ever opening our text editor by typing:
regularuser@smalleycreative:~$ echo '[[ -s "$HOME/.rvm/scripts/rvm" ]] && source "$HOME/.rvm/scripts/rvm"' >> ~/.bashrc
This command echo
s (copies) everything between the single quotes to (>>
) our .bashrc
file.
After modifying our .bashrc
, we need to update the bash
session we’re currently working in so that it takes these new settings and uses them. We can very easily reload our .bashrc
file by typing:
regularuser@smalleycreative:~$. ~/.bashrc
If that generated an error or didn’t work, it could be because you forgot the first .
(period) character. This .
tells Linux that we want to run a script. What script? The .bashrc
script! Since we added lines to the end of this script, it will now include rvm
, and this is precisely what we want.
Phase III: Dependencies, Rails, and Ruby… Almost Done
Now that rvm
is installed we can finish up tying up some loose ends before we move forward with Rails development on this machine. There are only three more steps until we’re ready to develop in Rails. We need to:
- install a few more required packages that Rails depends upon
- install Rails
- choose and install a version of Ruby
Excited? You should be. Rails is an absolute joy to work with if you put the time in to learn about it. To find out what packages still need to be installed type:
regularuser@smalleycreative:~$ rvm notes
This command just prints out the release notes for rvm
, but what’s really nice is that these notes are dynamic. Since we compiled rvm
for our own server, the notes include information about our platform as well as a full list of recommended packages that should be installed. After issuing this command, you’ll likely see something like this:
Notes for Linux ( DISTRIB_ID=Ubuntu DISTRIB_RELEASE=10.10 DISTRIB_CODENAME=maverick DISTRIB_DESCRIPTION="Ubuntu 10.10" ) NOTE: 'ruby' represents Matz's Ruby Interpreter (MRI) (1.8.X, 1.9.X) This is the *original* / standard Ruby Language Interpreter 'ree' represents Ruby Enterprise Edition 'rbx' represents Rubinius bash >= 3.2 is required curl is required git is required (>= 1.7 recommended) patch is required (for ree and some ruby-head's). If you wish to install rbx and/or Ruby 1.9 head (MRI) (eg. 1.9.2-head), then you must install and use rvm 1.8.7 first. If you wish to have the 'pretty colors' again, set 'export rvm_pretty_print_flag=1' in ~/.rvmrc. dependencies: # For RVM rvm: bash curl git # For Ruby (MRI & ree) you should install the following OS dependencies: ruby: aptitude install build-essential bison openssl libreadline6 libreadline6-dev curl git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-0 libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev # For JRuby (if you wish to use it) you will need: jruby: aptitude install curl g++ openjdk-6-jre-headless # In addition to ruby: dependencies, ruby-head: subversion # For IronRuby (if you wish to use it) you will need: ironruby: aptitude install curl mono-2.0-devel
Pay particular attention to the section I marked in bold above. It reads:
# For Ruby (MRI & ree) you should install the following OS dependencies: ruby: aptitude install build-essential bison openssl libreadline6 libreadline6-dev curl git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-0 libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev
rvm notes
couldn’t make things easier. It has listed out all of the packages that should now be installed for Ruby to work optimally. Though it hints at using aptitude
to install these packages, I tend to use apt-get
for installing packages on my server. For the sake of consistency, we’ll continue to use apt-get
. To install these packages using apt-get
, simply type:
regularuser@smalleycreative:~$ sudo apt-get install build-essential bison openssl libreadline6 libreadline6-dev curl git-core zlib1g zlib1g-dev libssl-dev libyaml-dev libsqlite3-0 libsqlite3-dev sqlite3 libxml2-dev libxslt-dev autoconf libc6-dev
Once these packages finish installing, we can install Ruby by typing:
regularuser@smalleycreative:~$ rvm install 1.9.2
Once Ruby installs we tell rvm
that we want to use the version we installed (1.9.2) by typing:
regularuser@smalleycreative:~$ rvm use 1.9.2
As you can see, rvm
makes it very simple to install and switch between different versions of Ruby rather quickly. For example, if we wanted to switch to an earlier version of Ruby, say Ruby 1.8.7, we could do this by typing:
regularuser@smalleycreative:~$ rvm install 1.8.7
And select it by typing:
regularuser@smalleycreative:~$ rvm use 1.8.7
Also, if you install a version of Ruby you don’t need to reinstall it the next time you want to use it. You can just issue the use
command to rvm
, and rvm
will switch to the version of Ruby you specify. For a complete list of available versions of Ruby in rvm
, type:
regularuser@smalleycreative:~$ rvm list known
If you’re ever unclear about what version of Ruby you’re currently running your code on, just type:
regularuser@smalleycreative:~$ ruby -v
When I ran this on my server it returned:
ruby 1.9.2p136 (2010-12-25 revision 30365) [i686-linux]
Since we will typically want to default to the most current installed version of Ruby, we should type:
regularuser@smalleycreative:~$ rvm --default use 1.9.2
This sets us up so that every time we log into our server we are going to be set to use Ruby 1.9.2. As with everything else in this tutorial, because rvm
is installed in our home directory, this won’t affect any other users on our server — we can change the version of Ruby we use whenever we want without fear of breaking a friend’s development environment.
We’re now left with one final step, and that is to install Rails. After all, this isn’t a tutorial on how to install Ruby, this is a tutorial on how to install Ruby on Rails!
Now that we’ve got a solid foundation with rvm
, it’s very simple to install Rails. Just type:
regularuser@smalleycreative:~$ gem install rails
After a few seconds, Rails will be installed.
Phase IV: Get Coding!
If you’re completely unfamiliar with Rails I highly recommend heading over to http://railstutorial.org/book to begin your studies. You can read the entire Rails Tutorial book online. The author, Michael Hartl, has done an amazing job of explaining Rails fundamentals, and I highly recommend this resource as a great first step in your Rails education. If you like the book, do Michael a favor and purchase it from him. He’s done a great job — you’d be hard pressed to find a better introduction to Rails and Ruby in general. Happy coding!
Leave a Reply