This post will guide you to prepare your Mac OS to install Ruby on Rails using
rvm. It will also show you how to create a sample rails app to enjoy all the efforts involved in installation.
There is a good news and a bad news. First the good news. Ruby comes preinstalled on all Mac's.
To check which version you have, open up Terminal and type
$ ruby -v
The most current version as of July 2012 is 1.9.3.
Now the bad news, the version you have may not be the version you want and it's a pain to manage different ruby versions manually. Ruby Version Manager (rvm) does great job so it carry the burden of installing and managing different versions.
Step 0. Install git and gcc
To check if you already have git, inside Terminal run this command
$ git --version
If you don't have git installed, download the recent installer from
http://code.google.com/p/git-osx-installer/downloads/list?can=3
Open the .dmg file and you will find the installer package. Follow the instructions to install git.
Ruby is compiled locally using gcc, hence make sure you have gcc installed.
$ gcc --version
Gcc does not come preinstalled on Mac. You need a developer account to install it separately.
- If you are running Lion, login using Apple ID and download Apple's official Command Line Tools for Xcode. Simply double click .dmg file and then the enclosed .pkg file to start the installation process.
- If you on Snow Leopard, you can download Xcode (4 GB in size) and install it, which includes gcc. The easier option would be to download the unofficial Gcc compiler and related tools from osx-gcc compiler downloads page.
Step 1. Install RVM
Open up Terminal and execute this command -
$ curl -L https://get.rvm.io | bash -s stable
It downloads and runs RVM installation script.
If curl complains about certificates, try with -k option
$ curl -kL https://get.rvm.io | bash -s stable
If you run into other issues, make sure you have git installed. Don't worry, it's easy to setup.
Once rvm installtion is completed it shows the files it modified. rvm is now ready to be used. Close the current Terminal window and open a new one. Test rvm using
$ rvm --version
You should see a version number with no errors. In case it was not recognized as a command you will have to load the rvm script into the shell so that shell can recognize 'rvm' as a valid command. You do that by executing following command verbatim.
$ echo '[[ -s "$HOME/.rvm/scripts/rvm" ]] && "$HOME/.rvm/scripts/rvm" ' >> ~/.bash_profile
Step 2. Install Ruby using rvm
We will install 1.9.3 as part of this tutorial. To get a feel of what rvm is capable of installing, run this command
$ rvm list known
It lists different versions of Ruby packages that can be installed and managed by rvm.
To install ruby use this format rvm install
for example,
$ rvm install 1.9.3
It downloads the source code for ruby 1.9.3 and then compiles it locally. If there are any errors, rvm will also tell you how to fix them. Once done, it displays "Install
- #complete"
To verify, list the installed versions using
$ rvm list
It should list the installed version like this -
~$ rvm list
rvm rubies
* ruby-1.9.3-p194 [ x86_64 ]
# => - current
# =* - current && default
# * - default
and now to double check, verify the ruby version itself using
$ ruby -v
Now set it as your default ruby version.
$ rvm --default 1.9.3
Optionally, you might want to generate Ruby core documentation by running
$ rvm docs generate
Step 3. Install Rails and other Gems
It's simple. Run this command and it will install rails and all other dependencies which work with your ruby version. It also install the docs for al the gems. Takes some time ...
$ gem install rails
To install SQLite 3, first verify you have sqlite3 installed on your system by typing
$ sqlite3 -version
It will respond with a long version string. Now install ruby binding for sqlite using
$ gem install sqlite3
Step 4. Ruby On Rails Test Drive
Now that you have all the required components in place, let's create a your first Ruby on Rails app to make sure everything is working smoothly. Again inside a Terminal window, navigate to a directory where you want to create your new app. ~/rails-app for example. Run these commands one by one
$ rails new todos
This will create the template application with model, view and controller and some static files.
$ cd todos
Tell rails to generate scema for your todos app database and also generate controller and views for standard actions.
$ rails g scaffold todo name:string completed:boolean
Run database migration
$ rake db:migrate
Then tart the rails WEBrick server on default port 3000
$ rails s
Finally, point your browser to
http://localhost:3000/ If you see a welcome page, your app is running fine. Congratulations! Your todos can be managed at
http://localhost:3000/todos
When you are done, you can stop the server by typing CTRL+C in the shell window where you started it earlier.
Extra Credit: More rvm commands
To revert to system version of ruby, i.e. pre-rvm ruby use this,
$ rvm system
To go back to 1.9.3 or any other version, use
$ rvm 1.9.3
To get the list of all rvm commands,
$ rvm usage
Hope it helped!