'Failed to build gem native extension: Unable to install Rails + MySQL2

I am on MAC OS X LION and keep getting error message Failed to build gem native extension when trying to install rails and mysql.

This is what I am doing. I enter:

    $ sudo gem install rails

Error message

Building native extensions.  This could take a while...

ERROR:  Error installing rails:
ERROR: Failed to build gem native extension.

/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/bin/ruby extconf.rb
mkmf.rb can't find header files for ruby at /System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/ruby.h


Gem files will remain installed in /Library/Ruby/Gems/1.8/gems/json-1.6.5 for inspection.
Results logged to /Library/Ruby/Gems/1.8/gems/json-1.6.5/ext/json/ext/parser/gem_make.out
ingjerd$ ruby -v
ruby 1.8.7 (2010-01-10 patchlevel 249) [universal-darwin11.0]

The same happens when I try to install mysql2 gem.

When I do the command

    gem list

Then rails and mysql is not in that list of local gems.

When I try the command

    which rails

I get:

/usr/bin/rails

So then that kind of makes me think that rails is in there somewhere. But when I try:

  $ rails -v

I get:

Rails is not currently installed on this system. To get the latest version, simply type:
$ sudo gem install rails You can then rerun your "rails" command.

I wonder whether this happens because I did things in the wrong order?

I first updated gems and then I installed XCODE and changed the .bash_profile to the following - and I think I should have done this before updating gems.

PS1="\u$ "
alias ll="ls -laGH"

export PATH="/usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin:$PATH"

I think I should have done the .bash_profile path before updating gems and I should also have installed xcode first, but I was unsure of the order of doing things.

Please help a true newbie to ruby as I can't start learning before I have a working environment....



Solution 1:[1]

For me, installing Xcode and then Command Line Tools worked.

You can install Command Line Tools from Xcode Preferences > Downloads > Command Line Tools. I don't know if you can just download Command Line Tools from here and skip the Xcode installation.

I also recommend you to install rbenv– see that you can install it with brew, mentioned below– to manage several ruby versions, or if you want RVM– I have tried both of them, don't know exactly what are its differences. This is because when you upgrade ruby, PATH system variable needs to be changed to detect newly installed Ruby which -a rubyshows you the paths of the Rubies you have installed.

The second part, to install mysql2, I have installed brew and then mysql with its header files.

# to install brew
ruby -e "$(curl -fsSkL raw.github.com/mxcl/homebrew/go)"
# to install mysql
brew install mysql

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Glorfindel