anyenvをインストール&Homebrewでインストール済のrbenvと互換を取る

rbenv 好きの [twitter:@riocampos] です。 pyenv も入れてます。 node.js はまだほとんど使ってないけど nodebrew だけぶち込んでいます。でも ndenv あると知ったのでそちらを入れたい、ついでに anyenv を入れよう、という魂胆です。

作者による anyenv 紹介

nvmがどうも好きになれないので、rbenv & ruby-buildを見習ってというか丸パクリしてndenv & node-buildを作りました。rbenvやplenvに慣れている人はそのままの感じで使えます。
node.jsのバージョン管理のためにndenv & node-buildを作ったのとanyenvの宣伝 - As a Futurist...

anyenvの説明をば。

何?

rbenvをコピーしたplenvやndenvなどは、.bashrcとかに2行追記して使うわけ
 :
anyenvを使うと、書くべきはたったの2行で済みます。

export PATH="$HOME/.anyenv/bin:$PATH"
eval "$(anyenv init -)"

これで~/.anyenv/envs以下にある何々envを全部initしてくれるので今まで通りrbenvコマンドとかが使えるようになります。

インストールも

これだけだと単に行数削減くらいしか意味ないんですが、installも作ったのでgit cloneする必要もないです。

$ anyenv install plenv
$ exec $SHELL -l
$ plenv install 5.18.0

こんな感じ。一応、再インストールもできるようにしてて、その場合は既存のversionsとversionについてはそのまま引き継いでくれるはずです。
anyenvという**env系の簡易マネージャを作った - As a Futurist...

anyenvインストール前に

nodebrew のアンインストール

node.js はほとんど使ってないので、いまは全て消えてもいいや、というやり方で作業します。
インストールは Homebrew ではなく本家 hokaccha/nodebrew から入れたようです。

  1. nodebrew で管理している node.js の削除(nodebrew uninstall VERSION
  2. ~/.nodebrew 以下の削除
  3. ~/.npm 以下の削除(node.js 使ってる場合はこれ残しておいてもいいのかしら?)
Homebrewでインストールしたrbenv及びpyenvのアンインストール
$ brew uninstall rbenv
Uninstalling /usr/local/Cellar/rbenv/0.4.0...
$ brew uninstall ruby-build
Uninstalling /usr/local/Cellar/ruby-build/20141016...
$ brew uninstall pyenv pyenv-pip-rehash
Uninstalling /usr/local/Cellar/pyenv/20141012...
Uninstalling /usr/local/Cellar/pyenv-pip-rehash/0.0.4...
.bash_profileから**env系の環境設定を削除
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"

とかいうやつですね。これを削除。

anyenvインストール

$ git clone https://github.com/riywo/anyenv ~/.anyenv
Cloning into '/Users/riocampos/.anyenv'...
remote: Counting objects: 130, done.
remote: Total 130 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (130/130), 21.02 KiB | 0 bytes/s, done.
Resolving deltas: 100% (44/44), done.
Checking connectivity... done.
$ echo 'export PATH="$HOME/.anyenv/bin:$PATH"' >> ~/.bash_profile 
$ echo 'eval "$(anyenv init -)"' >> ~/.bash_profile 

この時点での .bash_profile(分かりやすいように#anyenvというコメント行を追加)

source ~/.bashrc

#anyenv
export PATH="$HOME/.anyenv/bin:$PATH"
eval "$(anyenv init -)"

ログインシェルで.bash_profile読み直し

$ exec $SHELL -l

これでおk

ついでにanyenv-updateも入れておきましょう

**env とプラグイン(rbenv ならば ruby-install とか)を更新してくれる anyenv-update 。入れておかないとあとあと面倒ですよね。

This is an anyenv plugin that provides anyenv update command to update all **env and all plugins.
znz/anyenv-update

$ mkdir -p $(anyenv root)/plugins
$ git clone https://github.com/znz/anyenv-update.git $(anyenv root)/plugins/anyenv-update
Cloning into '/Users/riocampos/.anyenv/plugins/anyenv-update'...
remote: Counting objects: 19, done.
remote: Total 19 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (19/19), done.
Checking connectivity... done.

これで

$ anyenv update

が使えるようになりました。

anyenv環境でrbenvインストール

ruby-build まで入れてくれます。ありがたい。

$ anyenv install rbenv
/var/folders/r8/t51gts3s5kb5b5qvskswx22c0000gn/T/rbenv.20141018220626.71548 ~
Cloning https://github.com/sstephenson/rbenv.git...
Cloning into 'rbenv'...
remote: Counting objects: 1973, done.
remote: Compressing objects: 100% (53/53), done.
remote: Total 1973 (delta 26), reused 0 (delta 0)
Receiving objects: 100% (1973/1973), 320.67 KiB | 151.00 KiB/s, done.
Resolving deltas: 100% (1224/1224), done.
Checking connectivity... done.
~
~/.anyenv/envs/rbenv/plugins ~
Cloning https://github.com/sstephenson/ruby-build.git...
Cloning into 'ruby-build'...
remote: Counting objects: 3868, done.
remote: Compressing objects: 100% (36/36), done.
remote: Total 3868 (delta 25), reused 0 (delta 0)
Receiving objects: 100% (3868/3868), 680.11 KiB | 316.00 KiB/s, done.
Resolving deltas: 100% (1919/1919), done.
Checking connectivity... done.
~

Install rbenv succeeded!
Please reload your profile (exec $SHELL -l) or open a new session.

最後に「exec $SHELL -l でプロファイル読み直してね」と出るのが親切。

入れてくれたrbenvは
$ which rbenv
/Users/riocampos/.anyenv/envs/rbenv/bin/rbenv
$ ls -l ~/.anyenv/envs/rbenv/bin/
total 16
lrwxr-xr-x  1 riocampos  staff   16 10 18 22:19 rbenv -> ../libexec/rbenv
-rwxr-xr-x  1 riocampos  staff  724 10 18 22:19 ruby-local-exec

ということで ls -l ~/.anyenv/envs/rbenv/libexec/ 以下に入っていることが分かりました。

ではruby-buildは

~/.anyenv/envs/rbenv/plugins 以下にいます。

つまりanyenvでインストールしたrbenvは

比較してみると、 ~/.rbenv の代わりに ~/.anyenv/envs/rbenv 以下にファイルを置いている、という構造になっていることが分かります。

予め入れていたrbenvは…見てくれませんorz

$ rbenv versions
* system (set by /Users/riocampos/.anyenv/envs/rbenv/version)

全く見てませんw

anyenvで入れたrbenvに対応するようリンクを張ります

$ git clone https://github.com/sstephenson/rbenv.git ~/.rbenv

でrbenvをインストールした環境であれば

anyenv を使い始める前に、まずは既に導入済みの rbenv を anyenv 管理下に移したいところです。
~/.anyenv/envs 以下にディレクトリを置けばいいようだったので、試しに以下のようにシンボリックリンクを張ってみました。

% ln -s ~/.rbenv/ ~/.anyenv/envs/rbenv

rbenv を anyenv で管理する - akishin999の日記

この方法で良いのですが、 Homebrew で rbenv をインストールした環境では .rbenv/bin や .rbenv/libexec が存在しません。なので要所だけリンクを張っておきます。

$ ln -s ~/.rbenv/shims ~/.anyenv/envs/rbenv/
$ ln -s ~/.rbenv/version ~/.anyenv/envs/rbenv/
$ ln -s ~/.rbenv/versions ~/.anyenv/envs/rbenv/

rbenv-communal-gems を入れているので ~/.rbenv/gems に gem が入っています。のでこれもリンクを張ります。

$ ln -s ~/.rbenv/gems ~/.anyenv/envs/rbenv/

確認。

$ rbenv versions
  system
  1.8.7-p375
  1.9.3-p484
* 1.9.3-p547 (set by /Users/riocampos/.anyenv/envs/rbenv/version)
  2.0.0-p576
  2.1.3

おk。
(でも一旦 .rbenv を退避してから Homebrew じゃない手順で rbenv をインストールして、 ln -s ~/.rbenv/ ~/.anyenv/envs/rbenv したほうが後々ラクかもしれません^^; → 書きました。anyenvをインストール2 - 別館 子子子子子子(ねこのここねこ)

Homebrewにて入れたrbenv関連pluginを削除

Mac だと plugin 含めて Homebrew でいろいろ入れてしまうのだけど、 anyenv との互換が悪くなりそうな気がしてきました。さきほど anyenv-update を入れたのでプラグインの世話も全て anyenv に任せた方が安心だと思います。
なので Homebrew ではアンインストールして、プラグインを改めて入れ直しましょう。でも .rbenv の中で管理します。

まず anyenv 側にある rbenv のプラグインフォルダを.rbenv側に移動

いままで rbenv のプラグインは全てHomebrew任せだったので .rbenv/plugins が無い状況です。なので一気に移動します。

$ mv ~/.anyenv/envs/rbenv/plugins ~/.rbenv 

そしてリンクを張ります。

$ ln -s ~/.rbenv/plugins ~/.anyenv/envs/rbenv/
rbenv-gem-rehash

Ruby - rbenv を使っているなら rbenv-gem-rehash を使おう - Qiita
とか書いて私の中でイチオシなプラグインです。というかコレ使わないと面倒で仕方ないよね。
で、入れ直します。

$ brew uninstall rbenv-gem-rehash
Uninstalling /usr/local/Cellar/rbenv-gem-rehash/1.0.0...
$ git clone https://github.com/sstephenson/rbenv-gem-rehash.git ~/.rbenv/plugins/rbenv-gem-rehash
Cloning into '/Users/riocampos/.rbenv/plugins/rbenv-gem-rehash'...
remote: Counting objects: 97, done.
remote: Total 97 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (97/97), done.
Checking connectivity... done.
rbenv-communal-gems

2.1.x とかのほぼ同じバージョンで gem 共有が出来るようにするプラグインです。ディスクを余分に喰わなくてありがたい。
これも入れ直し。

$ brew uninstall rbenv-communal-gems
Uninstalling /usr/local/Cellar/rbenv-communal-gems/1.0.1...
$ git clone git://github.com/tpope/rbenv-communal-gems.git ~/.rbenv/plugins/rbenv-communal-gems
Cloning into '/Users/riocampos/.rbenv/plugins/rbenv-communal-gems'...
remote: Counting objects: 45, done.
remote: Total 45 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (45/45), 6.97 KiB | 0 bytes/s, done.
Resolving deltas: 100% (10/10), done.
Checking connectivity... done.

anyenv環境でpyenvインストール

$ anyenv install pyenv
/var/folders/r8/t51gts3s5kb5b5qvskswx22c0000gn/T/pyenv.20141019170152.13814 ~
Cloning https://github.com/yyuu/pyenv.git...
Cloning into 'pyenv'...
remote: Counting objects: 7654, done.
remote: Compressing objects: 100% (69/69), done.
remote: Total 7654 (delta 37), reused 1 (delta 0)
Receiving objects: 100% (7654/7654), 1.33 MiB | 521.00 KiB/s, done.
Resolving deltas: 100% (5645/5645), done.
Checking connectivity... done.
~

Install pyenv succeeded!
Please reload your profile (exec $SHELL -l) or open a new session.
$ exec $SHELL -l

今までの .pyenv へリンクを張る。

$ ln -s ~/.pyenv/shims ~/.anyenv/envs/pyenv/
$ ln -s ~/.pyenv/version ~/.anyenv/envs/pyenv/
$ ln -s ~/.pyenv/versions ~/.anyenv/envs/pyenv/
プラグインフォルダ

~/.anyenv/envs/pyenv/plugins に python-build があり、 ~/.pyenv/plugins に pyenv-pip-rehash(rbenv における rbenv-gem-rehash)だけがあります。 rbenv では .rbenv/plugins にプラグイン本体を置いて、anyenv 側にはリンクを置きました。 pyenv でも同様にしましょう。

$ rm -rf  ~/.pyenv/plugins
$ mv -f ~/.anyenv/envs/pyenv/plugins ~/.pyenv 
$ ln -s ~/.pyenv/plugins ~/.anyenv/envs/pyenv/

pyenv-pip-rehash インストール。

$ git clone https://github.com/yyuu/pyenv-pip-rehash.git ~/.pyenv/plugins/pyenv-pip-rehash
Cloning into '/Users/riocampos/.pyenv/plugins/pyenv-pip-rehash'...
remote: Counting objects: 117, done.
remote: Total 117 (delta 0), reused 0 (delta 0)
Receiving objects: 100% (117/117), 14.99 KiB | 0 bytes/s, done.
Resolving deltas: 100% (51/51), done.
Checking connectivity... done.

これでいいですね。

anyenv環境でndenvインストール

やっと当初の目的の所までやってきましたw

$ anyenv install ndenv
/var/folders/r8/t51gts3s5kb5b5qvskswx22c0000gn/T/ndenv.20141019181444.40085 ~
Cloning https://github.com/riywo/ndenv.git...
Cloning into 'ndenv'...
remote: Counting objects: 53, done.
remote: Total 53 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (53/53), done.
Checking connectivity... done.
~
~/.anyenv/envs/ndenv/plugins ~
Cloning https://github.com/riywo/node-build.git...
Cloning into 'node-build'...
remote: Counting objects: 34, done.
remote: Total 34 (delta 0), reused 0 (delta 0)
Unpacking objects: 100% (34/34), done.
Checking connectivity... done.
~

Install ndenv succeeded!
Please reload your profile (exec $SHELL -l) or open a new session.
$ exec $SHELL -l

では node.js をインストール。

$ ndenv install --list
Available versions:
  v0.1.14
  v0.1.15
  v0.1.16
  v0.1.17
  v0.1.18
  v0.1.19
  v0.1.20
  v0.1.21
  v0.1.22
  v0.1.23
  v0.1.24
  v0.1.25
  v0.1.26
  v0.1.27
  v0.1.28
  v0.1.29
  v0.1.30
  v0.1.31
  v0.1.32
  v0.1.33
  v0.1.90
  v0.1.91
  v0.1.92
  v0.1.93
  v0.1.94
  v0.1.95
  v0.1.96
  v0.1.97
  v0.1.98
  v0.1.99
  v0.1.100
  v0.1.101
  v0.1.102
  v0.1.103
  v0.1.104
  v0.2.0
  v0.2.1
  v0.2.2
  v0.2.3
  v0.2.4
  v0.2.5
  v0.2.6
  v0.3.0
  v0.3.1
  v0.3.2
  v0.3.3
  v0.3.4
  v0.3.5
  v0.3.6
  v0.3.7
  v0.3.8
  v0.4.0
  v0.4.1
  v0.4.2
  v0.4.3
  v0.4.4
  v0.4.5
  v0.4.6
  v0.4.7
  v0.4.8
  v0.4.9
  v0.4.10
  v0.4.11
  v0.4.12
  v0.5.0
  v0.5.1
  v0.5.2
  v0.5.3
  v0.5.4
  v0.5.5
  v0.5.6
  v0.5.7
  v0.5.8
  v0.5.9
  v0.5.10
  v0.6.0
  v0.6.1
  v0.6.2
  v0.6.3
  v0.6.4
  v0.6.5
  v0.6.6
  v0.6.7
  v0.6.8
  v0.6.9
  v0.6.10
  v0.6.11
  v0.6.12
  v0.6.13
  v0.6.14
  v0.6.15
  v0.6.16
  v0.6.17
  v0.6.18
  v0.6.19
  v0.6.20
  v0.6.21
  v0.7.0
  v0.7.1
  v0.7.2
  v0.7.3
  v0.7.4
  v0.7.5
  v0.7.6
  v0.7.7
  v0.7.8
  v0.7.9
  v0.7.10
  v0.7.11
  v0.7.12
  v0.8.0
  v0.8.1
  v0.8.2
  v0.8.3
  v0.8.4
  v0.8.5
  v0.8.6
  v0.8.7
  v0.8.8
  v0.8.9
  v0.8.10
  v0.8.11
  v0.8.12
  v0.8.13
  v0.8.14
  v0.8.15
  v0.8.16
  v0.8.17
  v0.8.18
  v0.8.19
  v0.8.20
  v0.8.21
  v0.8.22
  v0.8.23
  v0.8.24
  v0.8.25
  v0.8.26
  v0.8.27
  v0.8.28
  v0.9.0
  v0.9.1
  v0.9.2
  v0.9.3
  v0.9.4
  v0.9.5
  v0.9.6
  v0.9.7
  v0.9.8
  v0.9.9
  v0.9.10
  v0.9.11
  v0.9.12
  v0.10.0
  v0.10.1
  v0.10.2
  v0.10.3
  v0.10.4
  v0.10.5
  v0.10.6
  v0.10.7
  v0.10.8
  v0.10.9
  v0.10.10
  v0.10.11
  v0.10.12
  v0.10.13
  v0.10.14
  v0.10.15
  v0.10.16
  v0.10.17
  v0.10.18
  v0.10.19
  v0.10.20
  v0.10.21
  v0.10.22
  v0.10.23
  v0.10.24
  v0.10.25
  v0.10.26
  v0.10.27
  v0.10.28
  v0.10.29
  v0.10.30
  v0.10.31
  v0.10.32
  v0.11.0
  v0.11.1
  v0.11.2
  v0.11.3
  v0.11.4
  v0.11.5
  v0.11.6
  v0.11.7
  v0.11.8
  v0.11.9
  v0.11.10
  v0.11.11
  v0.11.12
  v0.11.13
  v0.11.14

最新は v0.11.14 みたいなのでそれを入れましょう。

$ ndenv install v0.11.14
Downloading node-v0.11.14-darwin-x64.tar.gz...
-> http://nodejs.org/dist/v0.11.14/node-v0.11.14-darwin-x64.tar.gz
Installing node-v0.11.14-darwin-x64...
Installed node-v0.11.14-darwin-x64 to /Users/riocampos/.anyenv/envs/ndenv/versions/v0.11.14

$ ndenv versions
  v0.11.14
$ ndenv global v0.11.14 
$ ndenv versions
* v0.11.14 (set by /Users/riocampos/.anyenv/envs/ndenv/version)
$ which node
/Users/riocampos/.anyenv/envs/ndenv/shims/node
$ node -v
v0.11.14

おkですね。

最後にanyenv update

$ anyenv update
Updating 'anyenv'...
Already up-to-date.

Updating 'anyenv-update'...
Already up-to-date.

Updating 'ndenv'...
Already up-to-date.

Updating 'node-build'...
Already up-to-date.

Updating 'pyenv'...
Already up-to-date.

Updating 'pyenv-pip-rehash'...
Already up-to-date.

Skipping 'python-build'

Updating 'rbenv'...
Already up-to-date.

Updating 'rbenv-communal-gems'...
Already up-to-date.

Updating 'rbenv-gem-rehash'...
Already up-to-date.

Updating 'ruby-build'...
Already up-to-date.

おしまーい。