Skip to content

Installing Ruby on Rails on Mac OS X Lion

July 24, 2011

I spent lots of hours trying to get RoR to work on Lion. Perhaps this tutorial will help you save some time. Mac by default comes with Ruby 1.8.7 and is outdated. I had better luck getting Ruby 1.9.2 to work with Rails, which requires an upgrade process.

Before you attempt to upgrade Ruby, make sure you use Xcode 4.1, not version Xcode 3.x. Once I upgraded to Xcode 4.1 the whole installation process went much smoother. On another note, several sites recommend using the “sudo” commando to install packages (which escalates user permissions to root for a particular command) but this created conflicts, as the installed files were then assigned with root privileges. For example, when one installs Ruby with sudo privileges and Rails with regular user permissions conflicts arise. I know, a beginner mistake, but worth watching out for. I had more luck just sticking to regular permissions and not using sudo.

Development environment

1) Install the latest version of XCode from the App Store (its free). Installing the latest version of Xcode 3.x from apple.com’s web site did not work for me. First, verify Xcode version:

$ xcodebuild -version

You should see the following message:

Xcode 4.1 Build version 4B110

2) Verify that Git was installed correctly with Xcode, its installed automatically with Xcode 4.1. Git is used to download and install packages from the GitHub repository. This ensures that you are always downloading the latest version of the package.

$ git --version (these are two dashes)

This command should return something such as:

git version 1.7.4.4

RVM (Version Manager)

3) Now you may install RVM. RVM allows you to manage multiple versions of Ruby on the same machine. RVM will be used later to point to the upgraded version of Ruby. It’s not a good idea to uninstall the version of Ruby (1.8.7) that comes with OS X.

$ bash < <( curl -s https://rvm.beginrescueend.com/install/rvm )

4) Add RVM to you profile path so that it loads on new terminal sessions.

echo '[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" ' >> ~/.bash_profile

If you like to open a terminal session and type bash, the above command can also be added to .bashrc, or use your favorite text editor to modify files. If you can’t see the files from within Finder, install Tinker Tool from the App Store (free) to expose hidden files. It’s simpler than using the vi editor.

Verify that your path has been updated:

$ echo $PATH

This should return something like:

/Users/(user)/.rvm/gems/ruby-1.9.2-p290/bin:/Users/(user)/.rvm/bin:/Developer/usr/bin:/Developer/

usr/libexec:/Developer/usr/sbin:/Developer/usr/local/bin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/

local/sbin:/bin:/sbin:/Users/(user)/bin:/usr/libexec:/usr/local/git:/usr/local/mysql/bin

…where (user) is your username.

By the way, during this step its a good idea to go ahead and add other paths that you may need, such as the ones above.

5) Close and re-open your terminal session. Type the following into your terminal:

$ type rvm | head -1

This should return:

rvm is a function

Install Ruby

6) Now we are ready to install Ruby:

$ rvm install 1.9.2

This will download, compile and install Ruby 1.9.2 for you. If you run into any compilation errors during this step, make sure that Xcode 4.1 was installed correctly. You can see if gcc is available from the prompt just by typing:

$ gcc -v

Should return:

Using built-in specs. Target: i686-apple-darwin11 Configured with:

/private/var/tmp/llvmgcc42/llvmgcc42-2335.15~25/src/configure –disable-checking –enable-werror

–prefix=/Developer/usr/llvm-gcc-4.2 –mandir=/share/man –enable-languages=c,objc,c++,obj-c++

–program-prefix=llvm- –program-transform-name=/^[cg][^.-]*$/s/$/-4.2/ –with-slibdir=/usr/lib

–build=i686-apple-darwin11

–enable-llvm=/private/var/tmp/llvmgcc42/llvmgcc42-2335.15~25/dst-llvmCore/Developer/usr/local

–program-prefix=i686-apple-darwin11- –host=x86_64-apple-darwin11 –target=i686-apple-darwin11

–with-gxx-include-dir=/usr/include/c++/4.2.1 Thread model: posix gcc version 4.2.1 (Based on Apple

Inc. build 5658) (LLVM build 2335.15.00)

If that doesn’t come up then Xcode or the path is not set correctly. Make sure that /Developer/usr/bin:/Developer/ usr/libexec:/Developer/usr/sbin:/Developer/usr/local/bin are in your $PATH.

You can also add Ruby’s core documentation by typing:

$ rvm docs generate

7) Set Ruby 1.9.2 as the default and verify.

$ rvm use 1.9.2

Now verify:

$ Ruby -v

Should return:

ruby 1.9.2p180 (2011-02-18 revision 30909) [x86_64-darwin10.7.4]

Ruby Gems (Package Manager)

8 ) Now verify that Ruby Gems is installed and that it’s above 1.6.2. Gems is Ruby’s package manager:

$ gem -v

Should return:

1.6.2

If not, upgrade Ruby Gems with:

$ gem update --system

Database

9) Now we need to install a database. For starters it’s better to begin with SQLite3: it’s already included with OS X. It’s simpler to verify that RoR is working with sqlite3, once that is done then one can upgrade to another database such as MySQL.

Verify SQLite3 version:

$ sqlite3 -version

This should return something similar to:

3.6.23.1

It’s important to verify that version 3.6.12 or higher is working on your machine. If not, you need to upgrade SQLite3:

$ mkdir ~/src
$ cd ~/src
$ curl http://www.sqlite.org/sqlite-3.7.5.tar.gz | tar xvz
$ cd sqlite-3.7.5
$ autoconf
$ ./configure –prefix=/usr/local
$ make
$ make install

Verify SQLite3 version again. If it’s still on the old version, you may want to move /usr/local/bin/sqlite3 to /usr/bin/sqlite3.

10) Now install Ruby bindings for SQLite3:

$ gem install sqlite3

Rails

11) Next, we install Ruby’s web framework, Rails. You should have version 3.0.7 or higher installed:

$ gem install rails

Once that’s done, you can verify version by typing:

$ rails -v

This should return Rails 3.0.7 or higher.

Test

12) The last steps make sure that RoR is working correctly. Go to your $HOME path and create a new directory, cd into it and create the skeletal application:

$ mkdir www
$ cd www
$ rails new myapp
$ cd myapp

This will create a bunch of files. Then, we start the rails server by typing:

$ rails server

Navigate to http://localhost:3000 which should display a “Welcome aboard” page.

I got tons of errors along the way, so if you are getting any maybe I can be of help, as I took lots of notes as I went along.

These sites helped me with the above steps:

http://pragmaticstudio.com/blog/2010/9/23/install-rails-ruby-mac

http://stackoverflow.com/questions/2100187/error-installing-ruby-1-9-with-rvm-under-snow-leopard

http://guides.rubyonrails.org/getting_started.html

About these ads
46 Comments
  1. Worked really well! Thanks for posting this up .. However before ‘rails server’ it should be ‘cd myapp’ first.

  2. Jenny permalink

    Greg, Thanks for posting. (typo should be git –version, two dashes). Anyway, I am working on a new iMac w lion installed and when I checked the version was 1.87. I installed RVM but when I did echo ‘[[ -s "$HOME/.rvm/scripts/rvm" ]] && . “$HOME/.rvm/scripts/rvm” ‘ >> ~/.bash_profile I got no such file or directory. When I do echo $PATH I get /usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin What should I do? (New to Ruby would appreciate your help). Everything else before that line worked. Thanks!

    • Hi Jenny, first of all thanks for pointing out the typo for checking the git version.

      As far as pointing to RVM goes, cd into your rvm bin path after install to make sure it’s there:

      user $ cd /Users//.rvm/gems/ruby-1.9.2-p290/bin

      If it’s not there re-install. Log out and then log back into Terminal.app so that your profile is updated with the new RVM path. Or you can reload your profile by typing:

      user $ source .bash_profile
      or
      user $ source .bashrc

      Make sure that ‘[[ -s "$HOME/.rvm/scripts/rvm" ]] && . “$HOME/.rvm/scripts/rvm” # Load RVM function’ is in your .bash_profile and/or .bashrc files by using vi or the cat command:

      user $ cd $HOME
      user $ cat .bash_profile or cat .bashrc

      You can also use your favorite text editor to modify files by opening them from Finder, but you’ll need to install TinkerTool first. If you install RVM with root privileges using the ‘sudo’ command the rvm files will be added for all users in your system, if you install as single user then the bin files will be added to your $HOME directory, which by the looks of it is what you did.

      Hope this helps.

      • Jenny permalink

        Greg,
        I got a “Upgrade of RVM in /Users/pretzel/.rvm/ is complete” but when I cd to it, it says no such file or directory. I reinstalled 2x (turned off terminal.app) and got the same upgrade is complete and long message from Wayne. What am I doing wrong?

      • Very odd indeed…so if you cd into $HOME/ and list all files/folders you don’t se the .rvm directory at all? Perhaps the .rvm directory is being installed in /usr/local/rvm instead of $HOME//.rvm. In that case, your .bash_profile / .bashrc should have /usr/local/rvm in it, it should be automatically added for all users but doesn’t always do so. Also, make sure the ../.rvm/scripts directory is in there. If none of this works try

        user $ sudo bash < <(curl -s https://rvm.beginrescueend.com/install/rvm)

        Then try

        user $ type rvm | head -1

        Let me know how it goes.

  3. Jenny permalink

    Thanks for the info. But before I proceed using sudo install, someone told me to be sure to delete the rvm I installed so it doesn’t cause conflict later on. But if it can’t find it in that path, how can I make sure I’ve deleted it? Btw, when I cd $HOME I see (Desktop, Documents, Downloads, Libraries, Movies, Music, Pictures, Public) when I ls on each one I don’t see rvm. (Also if I do a spotlight search on ruby I see .test files 1-5 as well as Ruby Cocoa files but I am assuming those are the 1.87v. You can email me directly (apologize for the questions!).

    • I agree that you should avoid using root (sudo) to install RVM. Did you check the /usr/local directory? If you did get a message saying that RVM was correctly installed in /User//.rvm and don’t see it within your home path do you see it within Finder? Remember to see the .rvm directory in Finder you need to install TinkerTools. You said your bash session can’t see .rvm but try vi’ing or cat’ing your installation log file in Terminal and see what it says:

      /Users//.rvm/log/ruby-1.9.2-p290/install.log

      • Jenny permalink

        Greg,
        Still having difficulty with executing the bash script rvm, it seems like there is a path issue. I edited the .bash_profile file in my home directory and there is an issue with finding the rvm file even when I source the .bash profile file. I am executing and sourcing .bash profile file from /Users/ directory path. Is the $HOME directory the right place to source the .bash_profile file? Because when I do so, I have the error file not found in the $HOME/.rvm/scripts/rvm but I can CD to the .rvm directory if done manually and see the bash rvm file. It seems like a simple path issue. Thanks!
        p.s Can I directly run the rvm file from my HOME directory to see if things are actually working?

      • Hey Jenny, that’s good that you now have the .rvm directory in your $HOME path. (/Users/). The program is within the ../.rvm/bin directory, so you should be able to run it from there. Make sure you also edit your .bashrc file, not just the .bash_profile file. You can edit it with you favorite text editor and may see the files from within Finder by installing Tinker Tools.

        Let me know how it goes.

    • Craig Payne permalink

      Did you use ‘ls -a’? This will show you the ‘hidden’ files and directories who’s names start with a ‘.’. I’m not able to see the “dot files” unless I do that either.

  4. Peter Jones permalink

    Hi Greg,

    thank you for posting these instructions. I am relearning all of this after many years out of it. I get “No such file or directory” when I reopen a terminal. I have double checked and the users/peterdjones/.rvm.scripts/rvm path exists.

    echo PATH=$PATH\:Users/peterdjones/.rvm/scripts ; export PATH
    echo ‘[[ -s "$Users/peterdjones/.rvm/scripts/rvm" ]] && . “$Users/peterdjones/.rvm/scripts/rvm” ‘ >> ~/.bash_profile

    Thank you in advance for any advise.

    Kind regards,

    Peter

  5. Peter Jones permalink

    Hi Greg,

    sorry the echo statements are in .bash_profile file. I don’t have a .bashrc file. I reinstalled a clean version of Snow Leopard yesterday and then upgraded to Lion.

    Kind regards,

    Peter

    • Hi Peter try echo ‘[[ -s "$HOME/.rvm/scripts/rvm" ]] && source “$HOME/.rvm/scripts/rvm”‘ >> ~/.bash_profile instead. Notice that “User/.rvm/scripts/rvm” should be “$HOME/.rvm/scripts/rvm”.

      Let me know if that works.

  6. Jalene permalink

    Life is short, and this article saved valualbe time on this Earth.

  7. Nodin permalink

    Check that off the list of things I was cnofused about.

  8. upper permalink

    what about if i wanna install mysql and connected to RoR in Mac LION. and how to open PHPMyadmin of it, after install?

    • Hey, there, good question. I’ll post something soon with instructions on how to do that. To start, modify your /myapp/config/database.yml and comment out the sqlite3 code and add mysql information to it:

      development:
      adapter: mysql
      encoding: utf8
      database: temp_development
      username: root
      password:
      socket: /tmp/mysql.sock

      # Warning: The database defined as ‘test’ will be erased and
      # re-generated from your development database when you run ‘rake’.
      # Do not set this db to the same as development or production.
      test:
      adapter: mysql
      encoding: utf8
      database: temp_test
      username: root
      password:
      socket: /tmp/mysql.sock

      production:
      adapter: mysql
      encoding: utf8
      database: temp_production
      username: root
      password:
      socket: /tmp/mysql.sock

      Substitute userid: root and password: ___ with your own information. Then download PHPMyadmin and set it to manage your local installation of MySQL. Make sure you modify your .bash_profile and/or .bashrc files with the mysql/bin directory.

  9. Hi Greg, thanks for posting this.

    I am having a problem installing rvm.
    While following the instruction, I encountered a problem similar to Jenny’s.
    So, I uninstalled rvm (did ‘rvm implode’) and installed it again but it didn’t fix the problem.
    Right now i have /.rvm/ folder but have nothing under /.rvm/gems/.
    When I try to access /gems/ruby-1.9.2-p290/bin, it says ‘No such file or directory’.

    Also, when I try typing
    ‘echo ‘[[ -s "$HOME/.rvm/scripts/rvm" ]] && . “$HOME/.rvm/scripts/rvm” ‘ >> ~/.bash_profile’
    and
    ‘source .bashrc’,
    they both return ‘No such file or directory’

    When i typed ‘source .bash_profile’, it returns nothing.

    This is very weird because when I list all the files and folders under ‘.rvm’ folder on the terminal,
    I see ‘scripts’ folder and there’s also ‘rvm’ file under scripts.

    Thanks for your help!

    • Hi EK, what happens when you type:

      bash < <( curl -s https://rvm.beginrescueend.com/install/rvm )

      You should see the following:

      Successfully checked out branch ''
      remote: Counting objects: 477, done.
      remote: Compressing objects: 100% (135/135), done.
      remote: Total 400 (delta 266), reused 387 (delta 254)
      Receiving objects: 100% (400/400), 63.68 KiB, done.
      Resolving deltas: 100% (266/266), completed with 36 local objects.
      From git://github.com/wayneeseguin/rvm
      a29169b..03ad088 master -> origin/master
      From git://github.com/wayneeseguin/rvm
      * [new tag] 1.6.26 -> 1.6.26
      * [new tag] 1.6.27 -> 1.6.27
      * [new tag] 1.6.28 -> 1.6.28
      * [new tag] 1.6.29 -> 1.6.29
      * [new tag] 1.6.30 -> 1.6.30
      * [new tag] 1.6.31 -> 1.6.31
      * [new tag] 1.6.32 -> 1.6.32
      First, rewinding head to replay your work on top of it...
      Fast-forwarded master to 03ad08820d430ffa15577cef9dd36f7b0a506619.
      Successfully pulled (rebased) from origin

      RVM: Shell scripts enabling management of multiple ruby environments.
      RTFM: https://rvm.beginrescueend.com/
      HELP: http://webchat.freenode.net/?channels=rvm (#rvm on irc.freenode.net)

      Upgrading the RVM installation in /Users/gwerner/.rvm/
      Correct permissions for base binaries in /Users/gwerner/.rvm/bin...
      Copying manpages into place.

      Upgrade Notes

      * rvm_trust_rvmrcs has been changed to rvm_trust_rvmrcs_flag for consistency

      * Project rvmrc files are now checked for trust whenever they change, as promised by
      the note displayed during the review process

      * Ruby package dependency list for your OS is given by:
      rvm notes

      * If you encounter any issues with a ruby 'X' your best bet is to:
      rvm remove X ; rvm install X

      * If you wish to have the 'pretty colors' again, set in ~/.rvmrc:
      export rvm_pretty_print_flag=1

      * If you see the following error message: Unknown alias name: 'default'
      re-set your default ruby, this is due to a change in how default works.

      * after_use hook now supports multiple files with after_use_*
      the custom hooks can be easily turned on/off by:
      chmod +x /Users/gwerner/.rvm/hooks/after_use_[hook_name]
      chmod -x /Users/gwerner/.rvm/hooks/after_use_[hook_name]

      Upgrade of RVM in /Users/gwerner/.rvm/ is complete.

      Once you have that you should be able to see the .rvm directory in your $HOME path. The default Ruby installation that comes with MAC isn’t installed in your $HOME path, that’s why it’s important to modify your .bash_profile and/or .bashrc files, so that you are pointing to the upgraded version of rvm.

      To set Ruby to default to 1.9.2 for a specific Rails application create a gemset and then set the application to default to 1.9.2 (in this case “myFriends” is the application):

      $ rvm gemset create myFriends
      $ rvm --default use 1.9.2@myFriends

      Let me know how it goes!!

  10. Hi Greg –

    I want to thank you for this, I hope it will finally on my road to learning RoR.

    I am inexperienced using terminal, and was confused by step “4) Add RVM to you profile path so that it loads on new terminal sessions.”

    Could you elaborate on what you mean? Up to that point it was mainly copy/paste but there I got a little lost.

    Thanks in advance!!

  11. Greg,

    Me again! I got it working, don’t really understand how, but did. ;)

    While this might be obvious, I would suggest adding that the user has to be within the myapp folder to have the server run correctly.

    Thanks again!!!!

  12. FYI: Confusing to people knew to RVM is the order of instructions around “rvm docs generate”.

    That will not work UNTIL they have specific a version of Ruby to use.

    It will give them: “ERROR: Currently ‘rvm docs …’ does not work with non-rvm rubies.” until they type “rvm use 1.9.2″

  13. Sarah permalink

    Thanks for the great tutorial – I would have been a very long time working all this out.
    The only problem I encountered was an issue when running “gem install rails”. It stuck while installing the RDocs for builder. The solution is to run:
    gem install –no-rdoc –no-ri rails

    For more info, check out

  14. Sarah permalink

    Ooops, the URL got lost from my post:
    https://github.com/rails/rails/issues/3322

  15. This tutorial was magnificent. Excellent job. And thank you.

  16. Hi! But how I can using Xcode for development Rails project? Than I start Xcode I can’t create Rails or Ruby project.

    • Hi Dmitry, developing RoR with Xcode isn’t really recommended. But installing the latest version of Xcode does get all the libraries and other things you need to make sure Ruby is running correctly and that the upgraded version, 1.9.2, works correctly with Rails. If you would like an RoR IDE there are plenty, such as RadRails, RubyMine, 3rd Rail, or Netbeans for Ruby. Personally I like using TextWrangler or TextMate.

      Cheers,
      Greg

  17. Great article!

    Since I had a lot of problems with RVM I switched to rbenv. I also found a way of installing everything without the need of Xcode. You can read my findings here: http://stravid.com/articles/coffeescript-and-ruby-development-setup-on-osx-lion/

  18. Hi, just wanted to let you know that Xcode 4.2 includes a non-LLVM C compiler, so when installing Ruby using RVM you may get an error when configuring “yaml” telling you that your C compiler doesn’t generate executable files.

    So, you have to add the “–with-gcc=clang” flag to the “rvm install 1.9.3-p0″ command (1.9.3-p0 is the latest version as I write):

    rvm install 1.9.3-p0 –with-gcc=clang

    In this way, we tell the yaml configure script that it should use this flag, and everything will run smoothly :)

    Have a nice day!

  19. Nice one mate! Saved me a lot of time!

    Thanks,
    A J

  20. Thanks a lot: everything smoothly installed with no errors! :)

  21. kael permalink

    thank you

  22. mzshamma permalink

    I love you!!! Thanks!

  23. Chris permalink

    Worked great. Thank you!

  24. Dnl permalink

    Thank you so much!!

Trackbacks & Pingbacks

  1. Ruby On Rails — Study Log « reterwebber
  2. 在Mac OS X Lion下安装Ruby on Rails – 胡太狼,你在哪儿?
  3. Installing Ruby on Rails on Mac OS X Lion | Joshua Schaible
  4. Installing Ruby 2.0.0 and Rails 3.2.13 on Mac OS X 10.7.5 | Frederic's Programming Blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Follow

Get every new post delivered to your Inbox.

Join 79 other followers

%d bloggers like this: