LoginSignup
7
7

More than 5 years have passed since last update.

zshでrbenvを使うとき発生した問題点を解決したので整理した。

Last updated at Posted at 2016-11-01

rbenvで設置したRUBYバージョンが表示されない。

症状

  • rbenv versionsで確認したとき、system以外に他のバージョンは表示されない。
[devsoul@dev:~] % rbenv versions
* system (set by /usr/local/bin/rbenv/version)

原因

  • RBENV_ROOTという環境変数が設定されてないか間違って設定されている場合に発生する。

対応

  • RBENV_ROOTを確認する。
% brew info rbenv
rbenv: stable 1.0.0 (bottled), HEAD

...snip...

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
  • 使っているシェルの設定ファイルに追記する。
% vim .zshenv
export RBENV_ROOT=/usr/local/var/rbenv

rbenv: no such command `shell'

症状

  • rbenv shellのコマンドが効かない。
  • 確かに去年も同じ問題があった。参考
  • そのときは何となくうまく行けたので原因を調べていなかったが今度やってみた。

原因

  • 環境変数PATHに/usr/local/var/rbenv/shimsがない。ていうのはrbenv init -がされてない。
[devsoul@dev:~] % echo $PATH
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin
  • zshはmacOS Sierraにデフォルトで設置されているものをそのまま使っている。
% which zsh
/bin/zsh
% zsh --version
zsh 5.2 (x86_64-apple-darwin16.0)
  • 使っているzshの設定ファイルは4つで、読み込み順序は~/.zshenv > /etc/zprofile > /etc/zshrc > ~/.zshrcだった。
% ls -al /etc/z*
-r--r--r--  1 root  wheel  126 10  6 22:18 /etc/zprofile
-r--r--r--  1 root  wheel  140 10  6 22:19 /etc/zshrc

% ls -al .z*
-rw-r--r--  1 devsoul  staff    429 10  7 11:29 .zshenv
-rw-r--r--  1 devsoul  staff   4612 10  7 16:34 .zshrc
  • rbenv init -は環境変数PATHに/usr/local/var/rbenv/shimsを入れてくれる。
% rbenv init -
export PATH="/usr/local/var/rbenv/shims:${PATH}"
export RBENV_SHELL=zsh
source '/usr/local/Cellar/rbenv/1.0.0/libexec/../completions/rbenv.zsh'
command rbenv rehash 2>/dev/null
rbenv() {
  local command
  command="$1"
  if [ "$#" -gt 0 ]; then
    shift
  fi

  case "$command" in
  rehash|shell)
    eval "$(rbenv "sh-$command" "$@")";;
  *)
    command rbenv "$command" "$@";;
  esac
}
  • zshの環境変数を設定する~/.zshenvに追記したが、/etc/zprofileでPATHが上書きされていた。
% cat /etc/zprofile
# system-wide environment settings for zsh(1)
if [ -x /usr/libexec/path_helper ]; then
    eval `/usr/libexec/path_helper -s`
fi
  • path_helperを実行してみるとPATHを設定していた。
% /usr/libexec/path_helper -s
PATH="/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin"; export PATH;

対応

  • rbenv init -を.zshenvから.zshrcへ移ることで解決した。
% vim .zshrc
# rbenv
if which rbenv > /dev/null; then eval "$(rbenv init -)"; fi

% echo $PATH
/usr/local/var/rbenv/shims:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/opt/X11/bin
  • /etc/zprofileの以降読み込む設定ファイルであれば~/.zshrc以外に追記しても大丈夫。

  • しかし、~/.zshrcはログインシェルとインタラクティブシェルのとき読み込まれるからこっちがいいかもね。

参考

7
7
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
7
7