Saturday 21 January 2012

Get Ruby up and running on your Mac

As part of my mission to learn the Ruby language, I've had to setup my Mac for development. These are the steps I had to go through to get the latest version (1.9.3-p0) of Ruby up and running on my Mac. I've just gathered the info from other blogs and websites and put it together here into a simple list of steps. Note that this is just one of many ways of getting Ruby up and running on your machine. In fact a Mac already comes with Ruby pre-installed - but it'll be an older version - 1.8.7 is what I had with Lion.


Writing apps with Ruby requires so little code its amazing. Combining it with the Sinatra gem makes it ideal for writing RESTful backends for apps. We'll leave this bit for another blog.  ;-)



Warning: some of the steps below download and execute remote scripts on your machine. This is generally something to be very careful of. Please note in the cases below that these are valid and trustworthy scripts. The source code can be viewed before you execute them with the hyperlinks I've included.


STEP 1: Install Apples Xcode developer toolset (if you haven't already)

We need Xcode for the c compilers which are used to build and install the tools to follow. Open up the MacAppStore and search for Xcode, then install. This will take a while...

Once installed; for some reason... the following install steps failed at on the compile of rvm. You may not have this issue, but just in case, lets build a dummy Xcode project. This will ensure everything goes smoothly later on.

Just open Xcode and start a new Project. Select Mac OSX and Cocoa Application. Let it auto-setup the project for you. You've just got to provide a name and a location for your project. Don't bother with source control. We are only doing this just to run the compiler.

You should now see something like this:


Next select Product -> Build which will compile your dummy Mac OSX application; then Run (if you want to see an empty application). Thats it now - go on with the next steps...


STEP 2: Install Homebrew

Homebrew is "The missing package manager for OSX".

Open up Terminal and enter the following command to install it:

/usr/bin/ruby -e "$(curl -fsSL https://raw.github.com/gist/323731)"


This actually downloads a remote script (run program) which is executed to download, build and install Homebrew. You can see the script here. Installs Homebrew to /usr/local so you don't need sudo to 'new install'.


STEP 3: Install Git

Git is a source control and version manager program. It is used absolutely everywhere by the open source community. You can use it to manage source code (or any files for that matter) repositories on your local machine, between remote machines or more commonly with a source code repository such as GitHub.

Open up Terminal again and type the following, which will ask Homebrew to go and get Git and install it on your local machine.

brew install git

At this point you can use Homebrew to install all sorts of other tools (however I skipped this for now). For example to install MySQL use:

brew install mysql



STEP 4: Install rvm

rvm is the Ruby Version Manager. It lets you easily switch between different installed versions of Ruby. Here's a blurb from the rvm site:

"Wondering why you should use RVM? For a start, not only does RVM make installing multiple ruby interpreters / runtimes easy and consistent, it provides features such as gemsets that aren't typically supported out of the box on most ruby installs.
RVM also lets you use different rubies in a manner that wont mess with your existing ruby install (unless you tell it to) as well as letting you run multiple different rubies in seperate terminals concurrently!"


Enter the following command to download a script which will install fetch and install the latest version of rvm. See the scripts source code here if you like.


bash < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer)



You can also add the following to the end of your bash_profile (~/.bash_profile) which makes it easier to work with rvm at the command line. I didn't have to do this as it was already in there - maybe due to an updated installer script.


export PATH="./bin:$PATH" 
[[ -s "/Users/jason/.rvm/scripts/rvm" ]] && source "/Users/jason/.rvm/scripts/rvm"


You can now tell rvm to use this bash environment with:


source ~/.bash_profile




STEP 5: Install the latest publicly available version of Ruby

Tell rvm to install the latest vernon of Ruby with this command line:

rvm install 1.9.3-p0 --with-gcc=clang


The --with-gcc=clang is necessary if you're running Xcode 4.2 or newer. Without it you'll receive errors similar to this:


Error running ' ./configure --prefix=/Users/jason/.rvm/rubies/ruby-1.9.3-p0 --enable-shared --disable-install-doc --with-libyaml --with-opt-dir=/Users/jason/.rvm/usr ', please read /Users/jason/.rvm/log/ruby-1.9.3-p0/configure.log
There has been an error while running configure. Halting the installation.



This will take a little while to run while Ruby is downloaded and compiled and you'll see the following output:


Fetching yaml-0.1.4.tar.gz to /Users/jason/.rvm/archives
Extracting yaml-0.1.4.tar.gz to /Users/jason/.rvm/src
Configuring yaml in /Users/jason/.rvm/src/yaml-0.1.4.
Compiling yaml in /Users/jason/.rvm/src/yaml-0.1.4.
Installing yaml to /Users/jason/.rvm/usr
Installing Ruby from source to: /Users/jason/.rvm/rubies/ruby-1.9.3-p0, this may take a while depending on your cpu(s)...


ruby-1.9.3-p0 - #fetching 
ruby-1.9.3-p0 - #extracted to /Users/jason/.rvm/src/ruby-1.9.3-p0 (already extracted)
ruby-1.9.3-p0 - #configuring 
ruby-1.9.3-p0 - #compiling 
ruby-1.9.3-p0 - #installing 
Retrieving rubygems-1.8.15
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  245k  100  245k    0     0   105k      0  0:00:02  0:00:02 --:--:--  148k
Extracting rubygems-1.8.15 ...
Removing old Rubygems files...
Installing rubygems-1.8.15 for ruby-1.9.3-p0 ...
Installation of rubygems completed successfully.
ruby-1.9.3-p0 - adjusting #shebangs for (gem irb erb ri rdoc testrb rake).
ruby-1.9.3-p0 - #importing default gemsets (/Users/jason/.rvm/gemsets/)
Install of ruby-1.9.3-p0 - #complete 



Great! Now we have the latest version of Runy ready to go. We just need one more thing to tell rvm to always use this latest version as the default so run this at the command line:


rvm use 1.9.3-p0 --default



Just to check that you do have the latest version and everything has gone just write enter:

ruby -v

and you should see

ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-darwin11.2.0]



Now start coding...

No comments:

Post a Comment