bokunonikki.net
JP / EN

Macでrailsの開発環境を作る手順

Fri Jul 28, 2017
Fri Jul 28, 2017

Macでrailsの開発環境を作るときの手順を自分なりにまとめおきます。 人のブログばかりみてると詰まることが多かったので。

Homebrewのインストール

まず、AppStoreからXcodeをダウンロードします。

次にターミナルに移動して、下のコマンドを実行してください。

1
$ xcode-select --install

インストール画面が表示されると思いますので、そのままインストールを行ってください。

次にHomebrewのインストールですが、これはMacOSが違うとコマンドが違うので気をつけましょう。

Sierraの場合

1
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

Sierra以外

1
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"

コマンドを実行するとパスワードが求められます。その時は、現在お使いのMac端末のログイン・パスワードを入力します。

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
$ ruby -e "$(curl-fsSLhttps://raw.githubusercontent.com/Homebrew/install/master/intall)"
==> This script will install:
/usr/local/bin/brew
/usr/local/Library/...
/usr/local/share/doc/homebrew
/usr/local/share/man/man1/brew.1
/usr/local/share/zsh/site-functions/_brew
/usr/local/etc/bash_completion.d/brew
==> The following directories will be made group writable:
/usr/local/.
/usr/local/bin
==> The following directories will have their owner set to boku:
/usr/local/.
/usr/local/bin
==> The following directories will have their group set to admin:
/usr/local/.
/usr/local/bin

Press RETURN to continue or any other key to abort
==> /usr/bin/sudo /bin/chmod g+rwx /usr/local/. /usr/local/bin
Password:

MacPortsでインストールされたRubyをアンインストールする

聞いたことがあるかもしれませんが、MacにはあらかじめRubyがインストールされています。しかし、バージョンが古いことが多いので、取り除いておきます。 まずは、Rubyの場所を確認します。

1
$ which ruby

僕の場合は、このように表記されます。環境によって異なります。

1
2
Mac$ which ruby
/Users/boku/.rbenv/shims/ruby

パスが出てこなかった場合は、次のRVMの除去に進んでください。 それではRubyをアンインストールします。

1
$ sudo port uninstall ruby

先ほどと同じくパスワードを求められるので、現在お使いのMac端末のログイン・パスワードを入力します。

1
2
3
$ sudo port uninstall ruby
Password:
Warning: port definitions are more than two weeks old, consider updating them by running 'port selfupdate'.

RVMの除去

Rubyの管理パッケージにrbenvをしようする場合、rvnがインストールされていると干渉しあって邪魔臭いので、取り除いておきましょう。

1
$ rvm implode

コマンドを実行した時に-bash: rvm: command not foundと表示された場合は、問題ありません。

homebrewを最新にする

1
$ brew upgrade

rbenv、ruby-build のインストール

1
$ brew install rbenv ruby-build

rbenvの設定を行う

1
2
$ echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
$ source ~/.bash_profile

なんも出てこないが心配なし。

Rubyの最新版を確認

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
$ rbenv install --list
Available versions:
  1.8.5-p52
  1.8.5-p113
  1.8.5-p114
  1.8.5-p115
  1.8.5-p231
  1.8.6
  :
  :
  2.2.4
  2.2.5
  2.2.6
  2.2.7
  2.3.0-dev
  2.3.0-preview1
  2.3.0-preview2
  2.3.0
  2.3.1
  2.3.2
  2.3.3
  2.3.4

Rubyのインストール

Rubyのバージョンを指定してインストールします。今回は、2.3.0をインストールします。

1
$ rbenv install 2.3.0

標準で使うRubyのバージョンを2.3.0にします

1
$ rbenv global 2.3.0

正しくできたかを確認します。

1
2
$ ruby -v
ruby 2.3.0p95 (2017-04-13 revision 50295) [x86_64-darwin16]

ruby 2.3.0 が表示されていればインストールは成功です。

Bundler のインストール

gemの管理に必要になるbundlerというgemをインストールします。

1
gem install bundler

PostgreSQLをインストールする

MySQLでもいいですが、今回は、PostgreSQLをインストールします。

1
$ brew install postgresql

PostgreSQLをインストール後、以下のコマンドを実行します。

1
$ brew services start postgresql

稀にpostgresqlが起動しない場合があるのでその時は、下のコマンドを実行します。

1
2
$ ln -sfv /usr/local/opt/postgresql/*plist ~/Library/LaunchAgents
$ launchctl load ~/Library/LaunchAgents/homebrew.mxcl.postgresql.plist

PostgreSQLの設定は終了です。

Gitをインストールする

1
brew install git

以上でMacのMacでrailsの開発環境は終わりです。

See Also