Mac OS X LionにRuby 1.8.7、Rails 3.0.10をインストールする
Mac OS X LionにRuby 1.8.7の環境を作ったときのメモ。今後、1.9.2も使いたいので、複数のバージョンのRubyを切り替えて使えるツール、RVMを先にインストールする。
公式ページのInstalling RVMに書かれている通りRVMをインストールしようとすると、いきなり失敗した。
$ bash < <( curl http://rvm.beginrescueend.com/releases/rvm-install-head ) % Total % Received % Xferd Average Speed Time Time Time Current Dload Upload Total Spent Left Speed 100 184 100 184 0 0 221 0 --:--:-- --:--:-- --:--:-- 1383 bash: line 1: html: No such file or directory bash: line 2: syntax error near unexpected token `<' 'ash: line 2: `<head><title>301 Moved Permanently</title></head> montecut:bin montecut$ bash < <(curl -sk https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer) bash: line 154: git: command not found bash: line 156: git: command not found ERROR: Unable to clone the RVM repository, attempted both git:// and https://
エラーメッセージを見る限り、RVMはgitがないとインストールできないらしい。
gitをインストールするには、MacPortsが必要で、MacPortsをインストールするにはXcodeが必要なので、以下の一式を順番にインストールする。
2. MacPortsのインストール
公式サイトのMac OS X Package Installで、MacPorts download directoryのリンクからMacPorts-2.0.3-10.7-Lion.dmgをダウンロードする。.dmgファイルをクリックしてマウント、出てきた.pkgファイルをクリックしてインストールする。
3. gitのインストール
以下のページを参考にして、gitをインストールする。
最速で Git を Mac にインストールして基本的なコマンドを使う方法 WEBLE
実行したコマンドは以下の通り。Gitに関連するライブラリはたくさんあるが、全部自動でインストールしてくれる。(数十分はかかる。)
$ sudo port install git-core ... ---> Installing git-core @1.7.7.2_0+doc+pcre+python27 ---> Activating git-core @1.7.7.2_0+doc+pcre+python27 ---> Cleaning git-core $ /opt/local/bin/git --version # Gitはここにインストールされる git version 1.7.7.2 $ echo $PATH # /opt/local/bin へのパスが追加されているか確認 /opt/local/bin:/opt/local/sbin:/Library/Java/JavaVirtualMachines/1.6.0_29-b11-402.jdk/Contents/Home/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin:/Applications/android-sdk-macosx/tools/
4. RVMのインストール
準備ができたので、改めてRVMをインストール。Mac に rvm を使って ruby と rails をインストール id:itto100penさんの記事を参考にさせていただいた。
# cd ~/ # ホームディレクトリに移動 $ bash < <(curl -s https://raw.github.com/wayneeseguin/rvm/master/binscripts/rvm-installer ) # インストールコマンド $ vi .bash_profile # .bash_profileの編集 … # 以下を追記 echo '[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" # Load RVM function' >> ~/.bash_profile [[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" # Load RVM function $ source .bash_profile # 設定を再読み込み $ type rvm | head -1 # これを入力して… rvm is a function # これが出力されればテストOK $ rvm requirements
5. Rubyのインストール
以下のコマンドを実行して、RVMでインストールできるRubyの一覧を出す。古いバージョンから最新のものまで対応しているのがわかる。それより、Rubyの派生バージョンがこんなにたくさんあったことにビックリ。
$ rvm list known # インストールできるRuby # MRI Rubies … [ruby-]1.8.7[-p352] [ruby-]1.8.7-head [ruby-]1.9.1-p378 [ruby-]1.9.1[-p431] [ruby-]1.9.1-head [ruby-]1.9.2-p180 [ruby-]1.9.2[-p290] [ruby-]1.9.2-head [ruby-]1.9.3-preview1 [ruby-]1.9.3-rc1 [ruby-]1.9.3[-p0] [ruby-]1.9.3-head ruby-head … # GoRuby … # JRuby … # Rubinius … # Ruby Enterprise Edition … # Kiji … # MagLev … # Mac OS X Snow Leopard Only … # IronRuby -- Not implemented yet. ...
1.8.7をインストールする。
$ rvm install 1.8.7-p352 # インストール $ rvm use 1.8.7-p352 # 切り替え Using /Users/montecut/.rvm/gems/ruby-1.8.7-p352 $ ruby -v # バージョンを確認 ruby 1.8.7 (2011-06-30 patchlevel 352) [i686-darwin11.2.0]
6. Rails 3.0.10のインストール
Rubyに続いて、Railsをインストールする。最新のRailsは3.1だが、手元の書籍とバージョンを合わせるため、3.0系では最新の3.0.10にする。
$ rvm gemset create rails3 $ rvm use 1.8.7@rails3 Using /Users/montecut/.rvm/gems/ruby-1.8.7-p352 with gemset rails3 $ gem install rails -v 3.0.10 /Users/montecut/.rvm/rubies/ruby-1.8.7-p352/lib/ruby/1.8/timeout.rb:60: [BUG] Bus Error ruby 1.8.7 (2011-06-30 patchlevel 352) [i686-darwin11.2.0] Abort trap: 6
Railsをインストールと上記のようなエラーが出る。
調べてみると、Xcode 4でシステムのデフォルトコンパイラがGCC 4.2 から LLVM GCC 4.2に変わったせいで問題が起きているらしい。以下のリンクによると、LLVMではないGCCを使えば問題が解決するとのこと。
で、GCC4.2とLLVM GCC4.2の違いは、LLVMをちょっと調べてみた id:wagavulinさんのページがわかりやすい(といっても僕にはむずかしいorz)。
僕の理解だと、たとえばC言語の場合、C言語→機械語にコンパイルする際、x86とARMなどのCPUのアーキテクチャごとに、別々の機械語を生成しなければならなかった。そうすると、一度機械語に変換されたアプリケーションは異なるCPU間で互換性がなくなる。LLVMがあれば、Javaのアプリケーションが複数種類のプラットフォームのVM上でも動くように、中間形式のバイトコードをひとつ作るだけで、複数種類のCPU上で動作するようになるのだと思う。(間違っていたらすみません…。)
Mac OS X Lion に gcc-4.2 をインストールするよのページを参考にGCCをインストールしてみる。
まずは、以下のコマンドでGCCを確認。/usr/bin/gccが、LLVM GCC 4.2を指していることがわかる。
$ ls -l /usr/bin | grep gcc lrwxr-xr-x 1 root wheel 12 11 20 15:42 cc -> llvm-gcc-4.2 lrwxr-xr-x 1 root wheel 12 11 20 15:42 gcc -> llvm-gcc-4.2 lrwxr-xr-x 1 root wheel 28 11 20 15:42 gcov-4.2 -> ../llvm-gcc-4.2/bin/gcov-4.2 lrwxr-xr-x 1 root admin 52 11 20 15:42 i686-apple-darwin11-llvm-g++-4.2 -> ../llvm-gcc-4.2/bin/i686-apple-darwin11-llvm-g++-4.2 lrwxr-xr-x 1 root admin 52 11 20 15:42 i686-apple-darwin11-llvm-gcc-4.2 -> ../llvm-gcc-4.2/bin/i686-apple-darwin11-llvm-gcc-4.2 lrwxr-xr-x 1 root admin 32 11 20 15:42 llvm-cpp-4.2 -> ../llvm-gcc-4.2/bin/llvm-cpp-4.2 lrwxr-xr-x 1 root admin 32 11 20 15:42 llvm-g++ -> ../llvm-gcc-4.2/bin/llvm-g++-4.2 lrwxr-xr-x 1 root admin 32 11 20 15:42 llvm-g++-4.2 -> ../llvm-gcc-4.2/bin/llvm-g++-4.2 lrwxr-xr-x 1 root admin 32 11 20 15:42 llvm-gcc -> ../llvm-gcc-4.2/bin/llvm-gcc-4.2 lrwxr-xr-x 1 root admin 32 11 20 15:42 llvm-gcc-4.2 -> ../llvm-gcc-4.2/bin/llvm-gcc-4.2
先ほどインストールしたMacPortsで、GCCのインストールをしようとしたが、gcc42ではビルドに失敗する。
$ port search gcc # MacPortsでGCCの検索 … apple-gcc42 @5666.3 (lang) Apple's version of gcc 4.2 ... gcc42 @4.2.4 (lang) The GNU compiler collection … Found 42 ports. $ sudo port install gcc42 # GNUコンパイラのGCC 4.2はビルドできない。 Password: ---> Fetching archive for gcc42 ---> Attempting to fetch gcc42-4.2.4_2.darwin_11.x86_64.tbz2 from http://packages.macports.org/gcc42 ---> Fetching gcc42 Error: gcc42 does not build on Snow Leopard or later. Error: Target org.macports.fetch returned: unsupported platform Log for gcc42 is at: /opt/local/var/macports/logs/_opt_local_var_macports_sources_rsync.macports.org_release_tarballs_ports_lang_gcc42/gcc42/main.log Error: Status 1 encountered during processing. To report a bug, see <http://guide.macports.org/#project.tickets>
今度はapple-gcc42で試してみる。
$ sudo port install apple-gcc42 # GCC42 $ which gcc-apple-4.2 # GCCのパスを調べる /opt/local/bin/gcc-apple-4.2
無事GCC 4.2が入ったので、Rubyのコンパイルとインストールをやりなおす。RVMにGCCを手動で指定して実行する。
$ rvm uninstall 1.8.7-p352 # いったん削除 /Users/montecut/.rvm/rubies/ruby-1.8.7-p357 has already been removed. $ CC=/opt/local/bin/gcc-apple-4.2 rvm install 1.8.7-p352 # gcc-apple-4.2を指定してインストール Installing Ruby from source to: /Users/montecut/.rvm/rubies/ruby-1.8.7-p352, this may take a while depending on your cpu(s)... ruby-1.8.7-p352 - #fetching ruby-1.8.7-p352 - #extracted to /Users/montecut/.rvm/src/ruby-1.8.7-p352 (already extracted) Applying patch 'stdout-rouge-fix' (located at /Users/montecut/.rvm/patches/ruby/1.8.7/stdout-rouge-fix.patch) ERROR: Error running 'patch -F 25 -p1 -N -f <"/Users/montecut/.rvm/patches/ruby/1.8.7/stdout-rouge-fix.patch"', please read /Users/montecut/.rvm/log/ruby-1.8.7-p352/patch.apply.stdout-rouge-fix.log ruby-1.8.7-p352 - #configuring ^[[Bruby-1.8.7-p352 - #compiling ruby-1.8.7-p352 - #installing Removing old Rubygems files... Installing rubygems-1.8.10 for ruby-1.8.7-p352 ... Installation of rubygems completed successfully. ruby-1.8.7-p352 - adjusting #shebangs for (gem irb erb ri rdoc testrb rake). ruby-1.8.7-p352 - #importing default gemsets (/Users/montecut/.rvm/gemsets/) Install of ruby-1.8.7-p352 - #complete
またエラーが出るが、--forceオプションを付けることで解決する。
$ CC=/opt/local/bin/gcc-apple-4.2 rvm install 1.8.7-p352 --force # 2度目なので--forceを付けないと失敗する Installing Ruby from source to: /Users/montecut/.rvm/rubies/ruby-1.8.7-p352, this may take a while depending on your cpu(s)... ruby-1.8.7-p352 - #fetching ruby-1.8.7-p352 - #extracting ruby-1.8.7-p352 to /Users/montecut/.rvm/src/ruby-1.8.7-p352 ruby-1.8.7-p352 - #extracted to /Users/montecut/.rvm/src/ruby-1.8.7-p352 Applying patch 'stdout-rouge-fix' (located at /Users/montecut/.rvm/patches/ruby/1.8.7/stdout-rouge-fix.patch) ruby-1.8.7-p352 - #configuring ruby-1.8.7-p352 - #compiling ruby-1.8.7-p352 - #installing Removing old Rubygems files... Installing rubygems-1.8.10 for ruby-1.8.7-p352 ... Installation of rubygems completed successfully. ruby-1.8.7-p352 - adjusting #shebangs for (gem irb erb ri rdoc testrb rake). ruby-1.8.7-p352 - #importing default gemsets (/Users/montecut/.rvm/gemsets/) Install of ruby-1.8.7-p352 - #complete $ rvm use 1.8.7-p352 # 切り替え Using /Users/montecut/.rvm/gems/ruby-1.8.7-p352 $ ruby -v # バージョンを確認 ruby 1.8.7 (2011-06-30 patchlevel 352) [i686-darwin11.2.0]
続いてRailsをインストール。
$ gem install rails -v 3.0.10 $ which rails /Users/montecut/.rvm/gems/ruby-1.8.7-p352@rails3/bin/rails $ rvm use 1.8.7@rails3 --default # このターミナル以外でも使えるようにする Using /Users/montecut/.rvm/gems/ruby-1.8.7-p352 with gemset rails3
うまくインストールできた。最後に、Railsでデモアプリを作成する。
$ rails new demo $ bundle install # 足りないgemをインストール $ rails generate scaffold Post name:string title:string content:text # 適当なスケルトンを作成 $ rake db:create $ rake db:migrate $ rails s # サーバーを起動する
ブラウザで、http://127.0.0.1:3000/postsにアクセスして、Railsのページが表示されればOK。