0-1-4 Macの場合:HomebrewでRubyをインストールしよう
Macの場合は比較的最新のモデルであれば、あらかじめRubyがインストールされています。
ただしバージョンが古いので最新のバージョンをインストールしましょう。
Homebrewをインストールする
ターミナル上で以下のコマンドを実行します。
$ /usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
==> This script will install: /usr/local/bin/brew /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 /usr/local/Homebrew ==> The following new directories will be created: /usr/local/Cellar /usr/local/Homebrew /usr/local/Frameworks /usr/local/bin /usr/local/etc /usr/local/include /usr/local/lib /usr/local/opt /usr/local/sbin /usr/local/share /usr/local/share/zsh /usr/local/share/zsh/site-functions /usr/local/var ==> The Xcode Command Line Tools will be installed. Press RETURN to continue or any other key to abort
Xcode Command Line Toolsをインストールする
Press RETURN to continue or any other key to abort
と聞いてくるので、[Return]キーを押して「Xcode Command Line Tools」をインストールします。
Xcode Command Line Toolsとは主にMac上でプログラムを開発するために必要なライブラリのうち、コマンドラインのみを提供するものです。
==> /usr/bin/sudo /bin/mkdir -p /usr/local/Cellar /usr/local/Homebrew /usr/local/Frameworks /usr/local/bin /usr/local/etc /usr/local/include /usr/local/lib /usr/local/opt /usr/local/sbin /usr/local/share /usr/local/share/zsh /usr/local/share/zsh/site-functions /usr/local/var Password: ==> /usr/bin/sudo /bin/chmod g+rwx /usr/local/Cellar /usr/local/Homebrew /usr/local/Frameworks /usr/local/bin /usr/local/etc /usr/local/include /usr/local/lib /usr/local/opt /usr/local/sbin /usr/local/share /usr/local/share/zsh /usr/local/share/zsh/site-functions /usr/local/var ==> /usr/bin/sudo /bin/chmod 755 /usr/local/share/zsh /usr/local/share/zsh/site-functions ==> /usr/bin/sudo /usr/sbin/chown grasen /usr/local/Cellar /usr/local/Homebrew /usr/local/Frameworks /usr/local/bin /usr/local/etc /usr/local/include /usr/local/lib /usr/local/opt /usr/local/sbin /usr/local/share /usr/local/share/zsh /usr/local/share/zsh/site-functions /usr/local/var ==> /usr/bin/sudo /usr/bin/chgrp admin /usr/local/Cellar /usr/local/Homebrew /usr/local/Frameworks /usr/local/bin /usr/local/etc /usr/local/include /usr/local/lib /usr/local/opt /usr/local/sbin /usr/local/share /usr/local/share/zsh /usr/local/share/zsh/site-functions /usr/local/var ==> /usr/bin/sudo /bin/mkdir -p /Users/grasen/Library/Caches/Homebrew ==> /usr/bin/sudo /bin/chmod g+rwx /Users/grasen/Library/Caches/Homebrew ==> /usr/bin/sudo /usr/sbin/chown grasen /Users/grasen/Library/Caches/Homebrew ==> /usr/bin/sudo /bin/mkdir -p /Library/Caches/Homebrew ==> /usr/bin/sudo /bin/chmod g+rwx /Library/Caches/Homebrew ==> /usr/bin/sudo /usr/sbin/chown grasen /Library/Caches/Homebrew ==> Searching online for the Command Line Tools ==> /usr/bin/sudo /usr/bin/touch /tmp/.com.apple.dt.CommandLineTools.installondemand.in-progress ==> Installing Command Line Tools (macOS High Sierra version 10.13) for Xcode-9.3 ==> /usr/bin/sudo /usr/sbin/softwareupdate -i Command\ Line\ Tools\ (macOS\ High\ Sierra\ version\ 10.13)\ for\ Xcode-9.3 Software Update Tool Downloading Command Line Tools (macOS High Sierra version 10.13) for Xcode Installing Command Line Tools (macOS High Sierra version 10.13) for Xcode Done with Command Line Tools (macOS High Sierra version 10.13) for Xcode Done. ==> /usr/bin/sudo /bin/rm -f /tmp/.com.apple.dt.CommandLineTools.installondemand.in-progress ==> /usr/bin/sudo /usr/bin/xcode-select --switch /Library/Developer/CommandLineTools ==> Downloading and installing Homebrew... remote: Counting objects: 100679, done. remote: Compressing objects: 100% (21/21), done. remote: Total 100679 (delta 6), reused 16 (delta 3), pack-reused 100655 Receiving objects: 100% (100679/100679), 22.97 MiB | 1.80 MiB/s, done. Resolving deltas: 100% (73314/73314), done. From https://github.com/Homebrew/brew * [new branch] master -> origin/master * [new tag] 0.1 -> 0.1 …(中略)… * [new tag] 1.6.3 -> 1.6.3 HEAD is now at e7db10c92 Merge pull request #4148 from MikeMcQuaid/issue-template-newlines ==> Tapping homebrew/core Cloning into '/usr/local/Homebrew/Library/Taps/homebrew/homebrew-core'... remote: Counting objects: 4749, done. remote: Compressing objects: 100% (4545/4545), done. remote: Total 4749 (delta 55), reused 618 (delta 17), pack-reused 0 Receiving objects: 100% (4749/4749), 3.84 MiB | 3.17 MiB/s, done. Resolving deltas: 100% (55/55), done. Tapped 4541 formulae (4,791 files, 12MB) ==> Cleaning up /Library/Caches/Homebrew... ==> Migrating /Library/Caches/Homebrew to /Users/grasen/Library/Caches/Homebrew... ==> Deleting /Library/Caches/Homebrew... Already up-to-date. ==> Installation successful! ==> Homebrew has enabled anonymous aggregate user behaviour analytics. Read the analytics documentation (and how to opt-out) here: https://docs.brew.sh/Analytics.html ==> Next steps: - Run `brew help` to get started - Further documentation: https://docs.brew.sh
Xcode Command Line Toolsのインストール後、そのまま引き続いてHomebrewがインストールされます。インストールがうまくいくと上記のように出力されます。
Homebrewのバージョンを確認する
Homebrewのインストールが完了したのち、brew -v
コマンドでインストールしたHomebrewのバージョンを確認します。
$ brew -v Homebrew 1.6.3 Homebrew/homebrew-core (git revision 0444; last commit 2018-05-05)
brew
コマンドでrbenv
をインストールする
brew
コマンドはHomebrewが提供するパッケージ管理のためのコマンドです。
brew install [パッケージ名]
とすることで必要なパッケージを全てインストールしてくれます。
また、rbenv
はOS上でインストールされているRubyのバージョンを管理するコマンドです。rbenv
を入れると複数のバージョンのRubyを簡単に切り替えることができます。
以下のコマンドを実行してrbenv
をインストールします。
$ brew install rbenv Updating Homebrew... ==> Auto-updated Homebrew! Updated 1 tap (homebrew/core). ==> Updated Formulae dcd ==> Installing dependencies for rbenv: autoconf, pkg-config, openssl, ruby-build ==> Installing rbenv dependency: autoconf ==> Downloading https://homebrew.bintray.com/bottles/autoconf-2.69.high_sierra.bottle.4.tar.gz ######################################################################## 100.0% ==> Pouring autoconf-2.69.high_sierra.bottle.4.tar.gz ==> Caveats Emacs Lisp files have been installed to: /usr/local/share/emacs/site-lisp/autoconf ==> Summary 🍺 /usr/local/Cellar/autoconf/2.69: 71 files, 3.0MB ==> Installing rbenv dependency: pkg-config ==> Downloading https://homebrew.bintray.com/bottles/pkg-config-0.29.2.high_sierra.bottle.tar.gz ######################################################################## 100.0% ==> Pouring pkg-config-0.29.2.high_sierra.bottle.tar.gz 🍺 /usr/local/Cellar/pkg-config/0.29.2: 11 files, 627.2KB ==> Installing rbenv dependency: openssl ==> Downloading https://homebrew.bintray.com/bottles/openssl-1.0.2o_1.high_sierra.bottle.tar.gz ######################################################################## 100.0% ==> Pouring openssl-1.0.2o_1.high_sierra.bottle.tar.gz ==> Caveats A CA file has been bootstrapped using certificates from the SystemRoots keychain. To add additional certificates (e.g. the certificates added in the System keychain), 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, because Apple has deprecated use of OpenSSL in favor of its own TLS and crypto libraries. If you need to have this software first in your PATH run: echo 'export PATH="/usr/local/opt/openssl/bin:$PATH"' >> ~/.bash_profile For compilers to find this software you may need to set: LDFLAGS: -L/usr/local/opt/openssl/lib CPPFLAGS: -I/usr/local/opt/openssl/include For pkg-config to find this software you may need to set: PKG_CONFIG_PATH: /usr/local/opt/openssl/lib/pkgconfig ==> Summary 🍺 /usr/local/Cellar/openssl/1.0.2o_1: 1,791 files, 12.3MB ==> Installing rbenv dependency: ruby-build ==> Downloading https://github.com/rbenv/ruby-build/archive/v20180424.tar.gz ==> Downloading from https://codeload.github.com/rbenv/ruby-build/tar.gz/v20180424 ######################################################################## 100.0% ==> ./install.sh 🍺 /usr/local/Cellar/ruby-build/20180424: 396 files, 200.6KB, built in 4 seconds ==> Installing rbenv ==> Downloading https://homebrew.bintray.com/bottles/rbenv-1.1.1.high_sierra.bottle.tar.gz ######################################################################## 100.0% ==> Pouring rbenv-1.1.1.high_sierra.bottle.tar.gz 🍺 /usr/local/Cellar/rbenv/1.1.1: 36 files, 62.7KB
インストールしたrbenv
のバージョンを確認する
以下のコマンドを実行してrbenv
のバージョンを確認します。
$ rbenv -v
rbenv 1.1.1
Rubyのパスを通す
あらかじめインストールされているRubyのパスは/usr/bin/ruby
です。rbenv
経由で実行されるRubyコマンドのパスは~/.rbenv/bin/ruby
です。
また、rbenvの初期化をしてくれるrbenv init
コマンドを新しいバージョンのRubyインストールのたびに実行する必要がないように、.bash_profile
に以下のコマンドで追記します。
$ echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile $ echo 'if which rbenv > /dev/null; then eval "$(rbenv init -)"; fi' >> ~/.bash_profile
記述後、.bash_profile
を読み込み直します。
$ . ~/.bash_profile
インストールするRubyのバージョンを確認する
rbenv install -l
コマンドを実行すると、現時点でインストールできるRubyバージョンを一覧できます。
$ rbenv install -l Available versions: 1.8.5-p52 …(中略)… 2.5.0-dev 2.5.0-preview1 2.5.0-rc1 2.5.0 2.5.1 2.6.0-dev 2.6.0-preview1 jruby-1.5.6 …(中略)… maglev-1.0.0 maglev-1.1.0-dev maglev-2.0.0-dev mruby-dev mruby-1.0.0 mruby-1.1.0 mruby-1.2.0 mruby-1.3.0 rbx-2.2.2 …(中略)… ree-1.8.7-2011.03 ree-1.8.7-2011.12 ree-1.8.7-2012.01 ree-1.8.7-2012.02 topaz-dev
なお、先頭にjruby
やmruby
などが記述されているものは特殊なRubyなので、ここでは通常版のもので正式リリースされている数字のみの2.5.1
をインストールします。
数字のあとにdev
やpreview
があるバージョンはまだ正式リリースされておらず開発中のものであることに注意が必要です。
Rubyをインストールする
rbenv install
コマンドでインストールするRubyのバージョンを指定してRubyをインストールします。なお2018年5月時点での最新のRubyバージョンは2.5.1
です。
$ rbenv install 2.5.1 ruby-build: use openssl from homebrew Downloading ruby-2.5.1.tar.bz2... -> https://cache.ruby-lang.org/pub/ruby/2.5/ruby-2.5.1.tar.bz2 Installing ruby-2.5.1... Installed ruby-2.5.1 to /Users/grasen/.rbenv/versions/2.5.1
Rubyのインストールにはそれなりに時間がかかるので気長に待ちましょう。手元の環境(Mac OS High Sierraバージョン10.13.3)では約3分程かかりました。
インストールしたRubyのバージョンを確認する
Rubyバージョンが最新になっていることを確認します。バージョンを確認するには-v
オプションを指定してruby
コマンドを実行します。
$ ruby -v ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-darwin17]