MBAにRuby環境を作る

  #Ruby
$ sw_vers
ProductName:    Mac OS X
ProductVersion: 10.8.4
BuildVersion:   12E3067

rbenvのreadmeにそってやる。
PATHについて解説までしてくれるとかマジ天使。

rbenvのインストール

# homebrewを最新の状態に
$ brew update
$ brew upgrade

# rbenvをインストール
$ brew install rbenv
==> Downloading https://github.com/sstephenson/rbenv/archive/v0.4.0.tar.gz
######################################################################## 100.0%
==> Caveats
To use Homebrew's directories rather than ~/.rbenv add to your profile:
  export RBENV_ROOT=/usr/local/var/rbenv

To enable shims and autocompletion add to your profile:
  if which rbenv > /dev/null; then eval "$(rbenv init -)"; fi
==> Summary
🍺  /usr/local/Cellar/rbenv/0.4.0: 31 files, 152K, built in 2 seconds

# ruby-buildをインストール
$ brew install ruby-build
######################################################################## 100.0%
==> ./configure --prefix=/usr/local/Cellar/autoconf/2.69
==> make install
🍺  /usr/local/Cellar/autoconf/2.69: 69 files, 2.0M, built in 8 seconds
==> Installing ruby-build
==> Downloading https://github.com/sstephenson/ruby-build/archive/v20130907.tar.gz
######################################################################## 100.0%
==> ./install.sh
🍺  /usr/local/Cellar/ruby-build/20130907: 83 files, 368K, built in 2 seconds

profileとかrcに以下を追記する。

$ echo 'if which rbenv > /dev/null; then eval "$(rbenv init -)"; fi' >> ~/.zprofile
$ source ~/.zprofile
$ rbenv --help
rbenv 0.4.0
Usage: rbenv <command> [<args>]

Some useful rbenv commands are:
   commands    List all available rbenv commands
   local       Set or show the local application-specific Ruby version
   global      Set or show the global Ruby version
   shell       Set or show the shell-specific Ruby version
   install     Install a Ruby version using the ruby-build plugin
   uninstall   Uninstall a specific Ruby version
   rehash      Rehash rbenv shims (run this after installing executables)
   version     Show the current Ruby version and its origin
   versions    List all Ruby versions available to rbenv
   which       Display the full path to an executable
   whence      List all Ruby versions that contain the given executable

See `rbenv help <command>' for information on a specific command.
For full documentation, see: https://github.com/sstephenson/rbenv#readme

rbenv-default-gemsをインストール

ついでにrbenv-default-gemsを入れておく。
rbenv install <version>でRubyをインストールする際に、一緒に必要なgemを入れてくれる。

$ echo 'bundler' >> ~/.rbenv/default-gems

ひとまずbundlerだけでいいかな。

# 一応homebrewを再度更新
$ brew update
$ brew upgrade

# ruby 2.0をインストール
$ rbenv install 2.0.0-p247
Downloading openssl-1.0.1e.tar.gz...
-> https://www.openssl.org/source/openssl-1.0.1e.tar.gz
Installing openssl-1.0.1e...

BUILD FAILED

Inspect or clean up the working tree at /var/folders/fz/6spm20xn2n34zk3rhc5q1b6c0000gn/T/ruby-build.20130922145805.39114
Results logged to /var/folders/fz/6spm20xn2n34zk3rhc5q1b6c0000gn/T/ruby-build.20130922145805.39114.log

Last 10 log lines:
RANLIB        =true
ARFLAGS       =
PERL          =/usr/bin/perl
SIXTY_FOUR_BIT_LONG mode
DES_UNROLL used
DES_INT used
RC4 uses uchar
RC4_CHUNK is unsigned long
sh: make: command not found
/usr/local/bin/ruby-build: line 387: make: command not found

makeコマンドがないぽい。
command line toolsが入ってなかった。

Xcode -> Preferences -> Downloads -> Command Line Tools -> Install

リトライ。

$ rbenv install 2.0.0-p247
Downloading openssl-1.0.1e.tar.gz...
-> https://www.openssl.org/source/openssl-1.0.1e.tar.gz
Installing openssl-1.0.1e...
Installed openssl-1.0.1e to /Users/kobtea/.rbenv/versions/2.0.0-p247

Downloading ruby-2.0.0-p247.tar.gz...
-> http://cache.ruby-lang.org/pub/ruby/2.0/ruby-2.0.0-p247.tar.gz
Installing ruby-2.0.0-p247...
Installed ruby-2.0.0-p247 to /Users/kobtea/.rbenv/versions/2.0.0-p247

ERROR:  While executing gem ... (Gem::FilePermissionError)
    You don't have write permissions into the /Library/Ruby/Gems/1.8 directory.
rbenv: error installing gem `bundler'

今度は権限がない。
Issueに載っていた。
どうやら環境変数に$RBENV_ROOTが必要らしい。

$ rbenv root

で表示されたパスを.zprofileとかに書く。

$ echo 'export RBENV_ROOT=$HOME/.rbenv' >> ~/.zprofile
$ source ~/.zprofile

リトライ。

$ rbenv install 2.0.0-p247
rbenv: /Users/kobtea/.rbenv/versions/2.0.0-p247 already exists
continue with installation? (y/N) y
Downloading openssl-1.0.1e.tar.gz...
-> https://www.openssl.org/source/openssl-1.0.1e.tar.gz
Installing openssl-1.0.1e...
Installed openssl-1.0.1e to /Users/kobtea/.rbenv/versions/2.0.0-p247

Downloading ruby-2.0.0-p247.tar.gz...
-> http://cache.ruby-lang.org/pub/ruby/2.0/ruby-2.0.0-p247.tar.gz
Installing ruby-2.0.0-p247...
Installed ruby-2.0.0-p247 to /Users/kobtea/.rbenv/versions/2.0.0-p247

Fetching: bundler-1.3.5.gem (100%)
Successfully installed bundler-1.3.5
Parsing documentation for bundler-1.3.5
Installing ri documentation for bundler-1.3.5
1 gem installed

できた。

# ruby, gemのパスが$RBENV_ROOT以下に向いているか確認
$ rbenv rehash
$ rbenv versions
* system (set by /Users/kobtea/.rbenv/version)
  2.0.0-p247
$ which ruby
/Users/kobtea/.rbenv/shims/ruby
$ which gem
/Users/kobtea/.rbenv/shims/gem

# システムデフォルトのバージョン確認
$ ruby --version
ruby 1.8.7 (2012-02-08 patchlevel 358) [universal-darwin12.0]
$ gem --version
1.3.6

# デフォルトのバージョンを2.0に変更
$ rbenv global 2.0.0-p247
$ rbenv versions
  system
* 2.0.0-p247 (set by /Users/kobtea/.rbenv/version)

$ ruby --version
ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-darwin12.4.1]
$ gem --version
2.0.3

こんにちはRuby。

追記

Macデフォルトのreadlineは使わないほうがいいらしい。
rbenv installする際にはhomebrewで入れたreadline, opensslを指定する。
指定しない場合、Macだとirbでマルチバイトが使えない。
irbで日本語が文字化けする


comments powered by Disqus