ERROR: Error installing cocoapods

CocoaPodsのインストールでハマったのでメモ。
人によっていろいろ違うはずなので、自分メモとして残します。

$ sudo gem install cocoapods
Building native extensions.  This could take a while...
ERROR:  Error installing cocoapods:
	ERROR: Failed to build gem native extension.

    rake RUBYARCHDIR=/Users/hoge/.rvm/rubies/ruby-1.9.2-p320/lib/ruby/gems/1.9.1/extensions/x86_64-darwin-11/1.9.1/xcodeproj-0.17.0 RUBYLIBDIR=/Users/hoge/.rvm/rubies/ruby-1.9.2-p320/lib/ruby/gems/1.9.1/extensions/x86_64-darwin-11/1.9.1/xcodeproj-0.17.0
/Users/hoge/.rvm/rubies/ruby-1.9.2-p320/lib/ruby/site_ruby/1.9.1/rubygems.rb:244:in `bin_path': can't find gem rake ([">= 0"]) with executable rake (Gem::GemNotFoundException)
	from /Users/hoge/.rvm/gems/ruby-1.9.2-p320@global/bin/rake:19:in `<main>'
	from /Users/hoge/.rvm/gems/ruby-1.9.2-p320/bin/ruby_noexec_wrapper:14:in `eval'
	from /Users/hoge/.rvm/gems/ruby-1.9.2-p320/bin/ruby_noexec_wrapper:14:in `<main>'

rake failed, exit code 1

Gem files will remain installed in /Users/hoge/.rvm/rubies/ruby-1.9.2-p320/lib/ruby/gems/1.9.1/gems/xcodeproj-0.17.0 for inspection.
Results logged to /Users/hoge/.rvm/rubies/ruby-1.9.2-p320/lib/ruby/gems/1.9.1/extensions/x86_64-darwin-11/1.9.1/xcodeproj-0.17.0/gem_make.out

rakeで怒られているので、ひとまずMacに入っているrubyrailsを最新にしてみる。
その前に、CocoaPodsのインストールにはXCodeのCommandLineToolsがない場合も同様のエラーになるとのことで、
もう一度確認も含めてCommnandLineToolsもインストールします。

1.XCodeのCommandLineToolsをインストールする

xcode-select –install and then again run:sudo gem install cocoapods
http://www.christianengvall.se/error-when-installing-cocoapods-on-mavericks/
と記載されていたので、試してみるも、「このソフトウェアは現在ソフトウェアアップデートサーバーから入手できないため、インストールできません」とエラーが起こるのでXCodeから

a.XCode->Open Develper Tools->More Developer Tools->Web->CommandLineToolsを選んでDL->インストール


b.XCode->Preference->Locations->Command Line Toolsをインストールしたものに設定する


2.portsを最新にする

#portを最新にする
$ sudo port -d selfupdate
$ sudo port -d sync

#versionを確認
$ port -v

3.rubyを最新にする

#インストール可能なrubyの一覧
$ port list ruby*
--------------------------------------------------
ruby                           @1.8.7-p374     lang/ruby
ruby-build                     @20140408       ruby/ruby-build
ruby19                         @1.9.3-p547     lang/ruby19
ruby20                         @2.0.0-p481     lang/ruby20
ruby21                         @2.1.2          lang/ruby21
ruby186                        @1.8.6-p420     lang/ruby186
ruby_select                    @1.0            sysutils/ruby_select
--------------------------------------------------

#一覧にあるrubyの中で最新のものを入れる
$ sudo port install ruby21

#.bashrcにGEM_HOMEを記載
vim .bashrc(下記1行を追記)
export GEM_HOME=/opt/local/lib/ruby2.1/gems/2.1.0

#.bashrcの更新
$ source .bashrc

#デフォルトのrubyをruby21で使用する
$ sudo port select ruby ruby21

#portで選択されているrubyを確認する
$ port select --show ruby

4.gemをupdateする

$ sudo gem update --system

5.railsを入れる

$ sudo gem install rails

6.もう一度CocoaPodsをインストール

$ sudo gem install cocoapods
#result
Successfully installed cocoapods-0.33.1
Parsing documentation for activesupport-3.2.18
Installing ri documentation for activesupport-3.2.18
Parsing documentation for claide-0.6.1
$ pod setup
#result
Setting up CocoaPods master repo
Already up-to-date.
Setup completed (read-only access)


入った!!OK~.

そのほか

$ pod setupをしたときに

/opt/local/lib/ruby2.1/site_ruby/2.1.0/rubygems/dependency.rb:313:in `to_specs': Could not find 'cocoapods' (>= 0) among 33 total gem(s) (Gem::LoadError)

になる場合はGEM_HOMEで指定した場所にcocoapodがインストールされていない。
$ gem env
を実行して、GEM_HOMEを確認し、

1.GEM_HOMEを追記
vim .bashrc(下記1行を追記)
export GEM_HOME=/opt/local/lib/ruby2.1/gems/2.1.0

2.

$ sudo gem install cocoapods

を試そう。