はじめに
Rubyを触ったことがなかったので、今更ながら触ってみようと思い自分のMac上にRubyの環境を構築したという話。
Macには標準でRubyがインストールされているのですが、バージョンが古く、どうせ最新版入れるならPHPのphpenvと同様に
Rubyもrbenvを入れて複数バージョンを切り替えられるようにした方が今後も色々と楽そうと思い立ったので今回の記事にしました。
環境
- MacBook Pro 13 - macOS Mojave 10.14.6
※今回の手順ではbrewコマンドを使用するため事前にHomebrewがインストールされているものとします
目的
Macで複数バージョンのRubyを管理し、それを切り替えて使えるようにする。
(プロジェクトごとにバージョンが違うとか)
rbenv導入手順
まずはHomebrewのアップデート
$ brew update
次にrbenvをインストール
$ brew install rbenv
...
Error: Permission denied @ apply2files - /usr/local/lib/node_modules/cordova/node_modules/extglob/lib/.DS_Store
私の環境では何やらパーミッションエラーが出ましたが.DS_Storeのものなので無視
最後にrbenvのバージョンを確認
$ rbenv --version
rbenv 1.1.2-11-gc46a970
これでインストール完了です!
簡単すぎて拍子抜けしちゃいます。
PHPのphpenvを導入した時はもっと面倒くさかった思い出しかない。。。
おまけ
rbenvコマンドを引数無しで実行すればヘルプが見れますが、念の為。
$ rbenv
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 ruby-build
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 installed Ruby versions
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/rbenv/rbenv#readme
- インストール可能なRubyバージョンの確認
$ rbenv install --list-all
1.8.5-p52
1.8.5-p113
1.8.5-p114
...
- 指定したバージョンのRubyをインストール
$ rbenv install 2.7.1
Downloading ruby-2.7.1.tar.bz2...
-> https://cache.ruby-lang.org/pub/ruby/2.7/ruby-2.7.1.tar.bz2
Installing ruby-2.7.1...
ruby-build: using readline from homebrew
Installed ruby-2.7.1 to /Users/XXXXX/.rbenv/versions/2.7.1
- インストール済みRubyバージョンの確認
$ rbenv versions
* system (set by /Users/XXXXX/.rbenv/version)
2.7.1
※systemは元々Macにインストール済みのRuby
- 現在使用しているRubyバージョンの確認
$ rbenv version
system (set by /Users/XXXXX/.rbenv/version)
- カレントディレクトリ以下で使用するRubyのバージョンを指定
$ rbenv local 2.7.1
- システム全体で利用するRubyバージョンを指定
$ruby global 2.7.1
各種コマンドやコマンドごとの動作もphpenvと一緒です。
なのでphpenvを使った事がある人なら特に違和感なく使えるかも。