MacでRails環境構築する手順

※インストールする際、参考にしたサイトのメモ書きみたいなもの

まずパッケージ管理ツールであるHomebrewをインストール
インストールする際のスクリプトは公式ページのをコピペ
Homebrew — OS X用パッケージマネージャー

$ ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
==> This script will install:
/usr/local/bin/brew
/usr/local/Library/...
/usr/local/share/man/man1/brew.1
==> The following directories will be made group writable:
/usr/local/.
==> The following directories will have their group set to admin:
/usr/local/.

Press RETURN to continue or any other key to abort
==> /usr/bin/sudo /bin/chmod g+rwx /usr/local/.
Password:
==> /usr/bin/sudo /usr/bin/chgrp admin /usr/local/.
==> /usr/bin/sudo /bin/mkdir /Library/Caches/Homebrew
==> /usr/bin/sudo /bin/chmod g+rwx /Library/Caches/Homebrew
==> Downloading and installing Homebrew...
remote: Counting objects: 230117, done.
remote: Compressing objects: 100% (60326/60326), done.
remote: Total 230117 (delta 168531), reused 230117 (delta 168531)
Receiving objects: 100% (230117/230117), 52.85 MiB | 354.00 KiB/s, done.
Resolving deltas: 100% (168531/168531), done.
From https://github.com/Homebrew/homebrew
 * [new branch]      master     -> origin/master
HEAD is now at 6484348 rakudo-star: update 2015.01 bottle.
==> Installation successful!
==> Next steps
Run `brew doctor` before you install anything
Run `brew help` to get started

次に、Run `brew doctor` before you install anythingとあるのでbrew doctorを実行し、問題ないようであれば/usr/local/binと/usr/local/sbinにパスを通す。私の環境では/etc/pathsに/usr/local/binが既に書いてあったので、/usr/local/sbinのみ.bash_profileに追加。その後、.bash_profileを再読み込みする。

ターミナルにて
$ brew doctor

.bash_profileにて
export PATH=/usr/local/sbin:$PATH

ターミナルにて(.bash_profileを再読み込み)
$ source .bash_profile


.bash_profileの話は以下のサイト

.bash_profile ? .bashrc ? いろいろあるけどこいつらなにもの? - Qiita


本当に正しい .bashrc と .bash_profile の使ひ分け - Qiita


次にHomebrewの動作確認がてらGitを入れる(結果はメモり忘れた)

$ brew install git

ここまでで参照したサイト:

Git をインストールする手順[ 2 ] – Homebrew からインストールする | memocarilog


Mac - homebrewとは何者か。仕組みについて調べてみた - Qiita

次にRubyをインストールする前に、Rubyが利用する「readline」と「OpensSSL」の2つのライブラリを先にインストールする

$ brew install readline
==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/readline-6.3.8.yosemite.bottle.tar.gz
######################################################################## 100.0%
==> Pouring readline-6.3.8.yosemite.bottle.tar.gz
==> Caveats
This formula is keg-only, which means it was not symlinked into /usr/local.

Mac OS X provides similar software, and installing this software in
parallel can cause all kinds of trouble.

OS X provides the BSD libedit library, which shadows libreadline.
In order to prevent conflicts when programs look for libreadline we are
defaulting this GNU Readline installation to keg-only.

Generally there are no consequences of this for you. If you build your
own software and it requires this formula, you'll need to add to your
build variables:

    LDFLAGS:  -L/usr/local/opt/readline/lib
    CPPFLAGS: -I/usr/local/opt/readline/include

==> Summary
🍺  /usr/local/Cellar/readline/6.3.8: 40 files, 2.1M


$ brew install openssl
==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/openssl-1.0.2.yosemite.bottle.tar.gz
######################################################################## 100.0%
==> Pouring openssl-1.0.2.yosemite.bottle.tar.gz
==> Caveats
A CA file has been bootstrapped using certificates from the system
keychain. To add additional certificates, place .pem files in
  /usr/local/etc/openssl/certs

and run
  /usr/local/opt/openssl/bin/c_rehash

This formula is keg-only, which means it was not symlinked into /usr/local.

Mac OS X already provides this software and installing another version in
parallel can cause all kinds of trouble.

Apple has deprecated use of OpenSSL in favor of its own TLS and crypto libraries

Generally there are no consequences of this for you. If you build your
own software and it requires this formula, you'll need to add to your
build variables:

    LDFLAGS:  -L/usr/local/opt/openssl/lib
    CPPFLAGS: -I/usr/local/opt/openssl/include

==> Summary
🍺  /usr/local/Cellar/openssl/1.0.2: 459 files, 18M

ちなみに"Mac OS X already provides this software and installing another version in parallel can cause all kinds of trouble."とあることからわかるようにOS Xには元からOpenSSLが入っています。readlineでのsimilar softwareが何を指しているのかはわかりません。

$ find /usr/bin -name openssl
/usr/bin/openssl

OpenSSLとreadlineでの

Generally there are no consequences of this for you. If you build your
own software and it requires this formula, you'll need to add to your
build variables:

    LDFLAGS:  -L/usr/local/opt/openssl/lib
    CPPFLAGS: -I/usr/local/opt/openssl/include

この部分の意味は、"ソフトウェアをビルドするとき、今インストールしたライブラリを必要とする場合、ビルドするときに指定する「ライブラリの指定」に「LDFLAGS」のパスを、「includeファイルの指定」に「CPPFLAGS」のパスを追加してください"みたいな感じです。

ビルドってなにって場合

仕事で使える魔法のLAMP(6):「ビルド」という作業は何を指しているのか - @IT



LinuxにPHPをインストールする方法は大きく2通りあると思います… - 人力検索はてな



一応インストールした二つのライブラリが参照できるように、リンクを貼っておく(/usr/local/binにシンボリックリンクを作る)
参考サイト:
積んメモ: keg-onlyなパッケージはシンボリックリンクが作られない

$ brew link readline --force
Linking /usr/local/Cellar/readline/6.3.8... 18 symlinks created
$ brew link openssl --force
Linking /usr/local/Cellar/openssl/1.0.2... 1510 symlinks created

ちなみにシンボリックリンクのを作る際、 * has multiple installed versionsと出た時の対応は以下のサイト参考。この段階で出るわけないけど、後学のため。

brew link を実行したら * has multiple installed versions と出たときの対応 - Programming log - Shindo200



次に、Rubyのバージョン管理のためrbenvをインストールする。rbenvについては以下のサイトを参考。
rbenvつかってrailsチュートリアルやってみよう! | niwatako$

$ 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 6 seconds

"To enable shims and autocompletion add to your profile: if which rbenv > /dev/null; then eval "$(rbenv init -)"; fi"とあるので、.bash_profileに追加

.bash_profileにて
if which rbenv > /dev/null; then eval "$(rbenv init -)"; fi

次にrbenvを使ってRubyをインストールするためのruby-buildをインストール

$ brew install ruby-build
==> Installing dependencies for ruby-build: autoconf, pkg-config
==> Installing ruby-build dependency: autoconf
==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/autoconf-2.69.yosemite.bottle.1.tar.gz
######################################################################## 100.0%
==> Pouring autoconf-2.69.yosemite.bottle.1.tar.gz
🍺  /usr/local/Cellar/autoconf/2.69: 70 files, 3.1M
==> Installing ruby-build dependency: pkg-config
==> Downloading https://downloads.sf.net/project/machomebrew/Bottles/pkg-config-0.28.yosemite.bottle.2.tar.gz
######################################################################## 100.0%
==> Pouring pkg-config-0.28.yosemite.bottle.2.tar.gz
🍺  /usr/local/Cellar/pkg-config/0.28: 10 files, 612K
==> Installing ruby-build
==> Downloading https://github.com/sstephenson/ruby-build/archive/v20150130.tar.gz
######################################################################## 100.0%
==> ./install.sh
🍺  /usr/local/Cellar/ruby-build/20150130: 154 files, 656K, built in 5 seconds

これでRubyをインストールする準備が整っったので、Rubyを早速インストール、今回は2.0.0-p481と1.9.3-p194を入れた。

$ RUBY_CONFIGURE_OPTS="--with-readline-dir=`brew --prefix readline` --with-openssl-dir=`brew --prefix openssl`" rbenv install 2.0.0-p481
Downloading ruby-2.0.0-p481.tar.gz...
-> http://dqw8nmjcqpjn7.cloudfront.net/00dd3d72435eb77f2bd94537c1738e5219ca42b6d68df3d4f20c183f4bd12d0f
Installing ruby-2.0.0-p481...
Installed ruby-2.0.0-p481 to /Users/ユーザ名/.rbenv/versions/2.0.0-p481

$ CONFIGURE_OPTS="--with-readline-dir=/usr/local/opt --with-openssl-dir=/usr/local/opt" rbenv install 1.9.3-p194
Downloading yaml-0.1.6.tar.gz...
-> http://dqw8nmjcqpjn7.cloudfront.net/7da6971b4bd08a986dd2a61353bc422362bd0edcc67d7ebaac68c95f74182749
Installing yaml-0.1.6...
Installed yaml-0.1.6 to /Users/ユーザ名/.rbenv/versions/1.9.3-p194

Downloading ruby-1.9.3-p194.tar.gz...
-> http://dqw8nmjcqpjn7.cloudfront.net/46e2fa80be7efed51bd9cdc529d1fe22ebc7567ee0f91db4ab855438cf4bd8bb
Installing ruby-1.9.3-p194...
Installed ruby-1.9.3-p194 to /Users/ユーザ名/.rbenv/versions/1.9.3-p194

次に、使用するRubyのバージョンを変える

$ ruby -v
ruby 2.0.0p481 (2014-05-08 revision 45883) [universal.x86_64-darwin14]

$ rbenv version(現在使用しているrubyのバージョン)
system (set by /Users/ユーザ名/.rbenv/version)

$ which ruby (/usr/bin以下のrubyを使用しているものの、.rbenv/shims以下のシンボリックリンク(?)から参照していることがわかる)
/Users/ユーザ名/.rbenv/shims/ruby

$ rbenv versions(先ほど入れた2.0.0-p481が入ってることがわかる)
* system (set by /Users/ユーザ名/.rbenv/version)
  2.0.0-p481

$ rbenv global 2.0.0-p481(2.0.0の方に切り替える)

$ rbenv rehash(忘れずにrehash)

$ rbenv version(ちゃんと変わってることが確認でkリウ)
2.0.0-p481 (set by /Users/ユーザ名/.rbenv/version)

$ rbenv -v(rbenvのバージョン確認 ちなみに-vはversionでなくverbose)
rbenv 0.4.0

続きはまた今度かく