In my post, Install CakePHP on Ubuntu 10.10, we took an in-depth view into installing CakePHP on Ubuntu 10.10. Â Since then, I’ve received some emails that collectively said:
Hey, why no love for Ruby on Rails?
When I posted the CakePHP tutorial it was not in any way an attempt to slight Ruby on Rails. Â In fact, I like the structural organization and community spirit spurred by Ruby on Rails just as much as the next person, and I’m not the sort of person that wastes too much time engaging in debates over development frameworks. Â They 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 didn’t exist, with its RESTful design practices and file structure, CakePHP may not have ever come into being. Â From a structural standpoint, CakePHP borrows a lot from Ruby on Rails, so there is definitely some truth to this notion.
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, but WEBrick is small and portable, which somehow makes it nice for developing and learning Rails.
This install guide begins immediately after a base install of Ubuntu has been completed. Â It takes us through installing the fundamentals, and ends with a working install of Rails, ready for development work.
Phase I: The 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 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 simply 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 stuff prior to getting into the heart of the actual Rails install.
The supporting packages are:
- Git: we’ll be using git as a foundation for
- curl: this will be used to download
- 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
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 that didn’t use
sudo at all in this command.
rvm installs to our home directory, which we have full access to, 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 whatever version of Ruby they wish without interfering with any other users.
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 (our default terminal shell in Ubuntu). Each user gets their own
.bashrc file in their own home directory. Inside this file, we can set up customizations to our environment that run every time bash is started. There are probably hundreds of cool modifications you can make to your
.bashrc file, but for this situation, we need to add a line to it 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
echos everything between the single quotes to (>>) our
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:
Now 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
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 bolded 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 will switch to the version of Ruby you specify. For a complete list of available versions of Ruby in
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!