【Mac】rbenv で Ruby のバージョン管理

プロジェクトごとに異なるRubyバージョンの場合が多々あるので、rbenv でバージョンを切り替えられるようにします。

■Ruby のバージョン確認

1
2
$ ruby -v
ruby 2.0.0p648 (2015-12-16 revision 53162) [universal.x86_64-darwin15]

■rbenv のインストール
Homebrewにて導入します。

まずは、Homebrew のリポジトリを最新にします。

1
$ brew update

その後、下記コマンドで rbenv をインストールします。

1
$ brew install rbenv ruby-build

結果

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
==> Installing dependencies for rbenv: autoconf, pkg-config, openssl, rub
==> Installing rbenv dependency: autoconf
==> Downloading https://homebrew.bintray.com/bottles/autoconf-2.69.el_capitan.bo
######################################################################## 100.0%
==> Pouring autoconf-2.69.el_capitan.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: 70 files, 3.0M
==> Installing rbenv dependency: pkg-config
==> Downloading https://homebrew.bintray.com/bottles/pkg-config-0.29.1.el_capita
######################################################################## 100.0%
==> Pouring pkg-config-0.29.1.el_capitan.bottle.tar.gz
/usr/local/Cellar/pkg-config/0.29.1: 10 files, 627.2K
==> Installing rbenv dependency: openssl
==> Downloading https://homebrew.bintray.com/bottles/openssl-1.0.2g.el_capitan.b
######################################################################## 100.0%
==> Pouring openssl-1.0.2g.el_capitan.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.
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.2g: 1,678 files, 12.0M
==> Installing rbenv dependency: ruby-build
==> Downloading https://github.com/rbenv/ruby-build/archive/v20160330.tar.gz
==> Downloading from https://codeload.github.com/rbenv/ruby-build/tar.gz/v201603
######################################################################## 100.0%
==> ./install.sh
/usr/local/Cellar/ruby-build/20160330: 204 files, 120.3K, built in 7 seconds
==> Installing rbenv
==> Downloading https://homebrew.bintray.com/bottles/rbenv-1.0.0.el_capitan.bott
######################################################################## 100.0%
==> Pouring rbenv-1.0.0.el_capitan.bottle.tar.gz
==> Caveats
Rbenv stores data under ~/.rbenv by default. If you absolutely need to
store everything under Homebrew's prefix, include this in your profile:
export RBENV_ROOT=/usr/local/var/rbenv
To enable shims and autocompletion, run this and follow the instructions:
rbenv init
==> Summary
/usr/local/Cellar/rbenv/1.0.0: 36 files, 61.9K

完了したらパスを通します。

1
2
3
$ echo 'export RBENV_ROOT=$HOME/.rbenv' >> ~/.zshrc
$ echo 'if which rbenv > /dev/null; then eval "$(rbenv init -)"; fi' >> ~/.zshrc
$ source ~/.zshrc

■Ruby のインストール
まずはインストール可能なバージョンを確認します。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
$ rbenv install -l
Available versions:
1.8.6-p383
1.8.6-p420
1.8.7-p249
1.8.7-p302
1.8.7-p334
1.8.7-p352
1.8.7-p357
1.8.7-p358
1.8.7-p370
1.8.7-p371
1.8.7-p374
1.8.7-p375
1.9.1-p378
1.9.1-p430
1.9.2-p0
1.9.2-p180
1.9.2-p290
1.9.2-p318
1.9.2-p320
1.9.2-p326
1.9.2-p330
1.9.3-dev
1.9.3-preview1
1.9.3-rc1
1.9.3-p0
1.9.3-p125
1.9.3-p194
1.9.3-p286
1.9.3-p327
1.9.3-p362
1.9.3-p374
1.9.3-p385
1.9.3-p392
1.9.3-p429
1.9.3-p448
1.9.3-p484
1.9.3-p545
1.9.3-p547
1.9.3-p550
1.9.3-p551
2.0.0-dev
2.0.0-preview1
2.0.0-preview2
2.0.0-rc1
2.0.0-rc2
2.0.0-p0
2.0.0-p195
2.0.0-p247
2.0.0-p353
2.0.0-p451
2.0.0-p481
2.0.0-p576
2.0.0-p594
2.0.0-p598
2.0.0-p643
2.0.0-p645
2.0.0-p647
2.0.0-p648
2.1.0-dev
2.1.0-preview1
2.1.0-preview2
2.1.0-rc1
2.1.0
2.1.1
2.1.2
2.1.3
2.1.4
2.1.5
2.1.6
2.1.7
2.1.8
2.1.9
2.2.0-dev
2.2.0-preview1
2.2.0-preview2
2.2.0-rc1
2.2.0
2.2.1
2.2.2
2.2.3
2.2.4
2.3.0-dev
2.3.0-preview1
2.3.0-preview2
2.3.0
2.4.0-dev
jruby-1.5.6
jruby-1.6.3
jruby-1.6.4
jruby-1.6.5
jruby-1.6.5.1
jruby-1.6.6
jruby-1.6.7
jruby-1.6.7.2
jruby-1.6.8
jruby-1.7.0-preview1
jruby-1.7.0-preview2
jruby-1.7.0-rc1
jruby-1.7.0-rc2
jruby-1.7.0
jruby-1.7.1
jruby-1.7.2
jruby-1.7.3
jruby-1.7.4
jruby-1.7.5
jruby-1.7.6
jruby-1.7.7
jruby-1.7.8
jruby-1.7.9
jruby-1.7.10
jruby-1.7.11
jruby-1.7.12
jruby-1.7.13
jruby-1.7.14
jruby-1.7.15
jruby-1.7.16
jruby-1.7.16.1
jruby-1.7.16.2
jruby-1.7.17
jruby-1.7.18
jruby-1.7.19
jruby-1.7.20
jruby-1.7.20.1
jruby-1.7.21
jruby-1.7.22
jruby-1.7.23
jruby-1.7.24
jruby-9.0.0.0.pre1
jruby-9.0.0.0.pre2
jruby-9.0.0.0.rc1
jruby-9.0.0.0.rc2
jruby-9.0.0.0
jruby-9.0.1.0
jruby-9.0.3.0
jruby-9.0.4.0
jruby-9.0.5.0
jruby-9.1.0.0-dev
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
rbx-2.2.2
rbx-2.2.3
rbx-2.2.4
rbx-2.2.5
rbx-2.2.6
rbx-2.2.7
rbx-2.2.8
rbx-2.2.9
rbx-2.2.10
rbx-2.3.0
rbx-2.4.0
rbx-2.4.1
rbx-2.5.0
rbx-2.5.1
rbx-2.5.2
rbx-2.5.3
rbx-2.5.4
rbx-2.5.5
rbx-2.5.6
rbx-2.5.7
rbx-2.5.8
rbx-2.6
rbx-2.7
rbx-2.8
rbx-2.9
rbx-2.10
rbx-2.11
rbx-2.71828182
rbx-3.0
rbx-3.1
rbx-3.2
rbx-3.3
rbx-3.4
rbx-3.5
rbx-3.6
rbx-3.7
rbx-3.8
rbx-3.9
rbx-3.10
rbx-3.11
rbx-3.12
rbx-3.13
rbx-3.14
rbx-3.15
rbx-3.16
rbx-3.17
rbx-3.18
rbx-3.19
rbx-3.20
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

その中から必要なバージョンをインストール。

1
$ rbenv install 2.2.0

rbenvでインストールした一覧は下記で確認できます。

1
2
3
4
$ rbenv versions
* system (set by /Users/lab3-34/.rbenv/version)
1.9.3-p551
2.2.0

切り替えは次のコマンドでできます。

1
$ rbenv global 2.2.0