Windowsでも、Pythonのpyenvのように、Rubyでも仮想環境(実行環境の切り替え)を使えるものだと思ってましたが、Ubuntu同様簡単に使えると高をくくっていた私には意外にハードルが高く、少し手間がかかりました。
(補足)MSYS2を使っているのは個人的な好みです。
概要
Windows(MSYS2)でrbenvを使おうとしたら、大きく以下の問題をクリアする必要がありました。
- src/realpath.cがコンパイルエラーになる
- rbenvにパスが通らない
- libexe/rbenv-*を呼び出してくれない
- ruby-buildでrubyがビルドできない(未だ原因不明)
Ubuntu等でのrbenvのインストール方法(ご参考)
普通?は、これでうまくいくはずです。ところが、Windowsではそう甘くありませんでした。
$ git clone https://github.com/rbenv/rbenv.git ~/.rbenv
$ cd ~/.rbenv && src/configure && make -C src
$ git clone https://github.com/rbenv/ruby-build.git ~/.rbenv/plugins/ruby-build
$ ~/.rbenv/plugins/ruby-build/install.sh
問題1の解決
ダウンロード(git clone)後、realpath()
が無い様でmake -C src
がエラーになります。
$ cd ~/.rbenv && src/configure && make -C src
make: ディレクトリ '/home/Taro/.rbenv/src' に入ります
gcc -fPIC -c -o realpath.o realpath.c
realpath.c: In function 'realpath_builtin':
realpath.c:18:13: warning: implicit declaration of function 'realpath'; did you mean '_makepath'? [-Wimplicit-function-declaration]
realbuf = realpath(p, NULL);
^~~~~~~~
_makepath
realpath.c:18:11: warning: assignment makes pointer from integer without a cast [-Wint-conversion]
realbuf = realpath(p, NULL);
^
gcc -shared -Wl,-soname,../libexec/rbenv-realpath.dylib -o ../libexec/rbenv-realpath.dylib realpath.o
realpath.o:realpath.c:(.text+0x3f): undefined reference to `realpath'
collect2.exe: error: ld returned 1 exit status
make: *** [Makefile:22: ../libexec/rbenv-realpath.dylib] エラー 1
make: ディレクトリ '/home/Taro/.rbenv/src' から出ます
realpath()
を_fullpath()
に置き換えました。正直、rbenvがどう変わったのかわかりませんが、関数の代替はできているはずです。
--- realpath.c 2018-06-10 10:30:30.460637000 +0900
+++ realpath.c.NEW 2018-06-10 10:33:03.433143400 +0900
@@ -15,7 +15,7 @@ WORD_LIST *list;
for (es = EXECUTION_SUCCESS; list; list = list->next) {
p = list->word->word;
- realbuf = realpath(p, NULL);
+ realbuf = _fullpath(NULL, p,_MAX_PATH);
if (realbuf == NULL) {
es = EXECUTION_FAILURE;
// builtin_error("%s: cannot resolve: %s", p, strerror(errno));
問題2の解決
ビルド後に(パスを通して)、rbenv
としてもエラーになりました。
$ ~/.rbenv/bin/rbenv init
.rbenv/bin/rbenv: 行 1: ../libexec/rbenv: No such file or directory
$ file ~/.rbenv/bin/rbenv
.rbenv/bin/rbenv: ASCII text, with no line terminators
$ rm ~/.rbenv/bin/rbenv
$ ln -s ../libexec/rbenv .
問題3の解決
MSYS2はインストール直後は、シンボリックリンクは使わない設定のようです。シンボリックリンクが使えないと(なぜか)libexec/rbenv-*
が呼び出せないようです。
msys64/msys2_shell.cmd
中の記述を変更して、シンボリックリンクを使えるように変更します。
rem set MSYS=winsymlinks:nativestrict
set MSYS=winsymlinks:nativestrict
問題4の解決
ruby-buildを入れて所望のバージョンをインストールしようとしましたが、エラーになりました。これは原因がわからず断念しました。
GitHubのoneclick/rubyinstaller2から所望のバージョンの7zファイルをダウンロードして、.rbenv/versions下に解凍して使いました。
環境
- Windows 10 Pro 1803 ビルド17134.48
- MINGW64_NT-10.0 DESKTOP-NAME 2.10.0(0.325/5/3) 2018-02-09 15:25 x86_64 Msys
- rbenv 1.1.1-37-g1c772d5