LoginSignup
23
25

More than 5 years have passed since last update.

rbenvで、-bash: rails: command not found

Posted at

はじめに

rbenvな環境で、「command not found」が発生したときの対応方法を記述します。

環境

  • CentOS 7
  • rbenv 0.4.0
  • ruby 2.1.2p95
  • rails 4.1.5

rbenv rehashを実行する

$ rbenv rehash

~/.rbenv/shims へrailsが作成され、railsコマンドが実行可能となる。

$ ls -l ~/.rbenv/shims/rails
-rwxrwxr-x. 1 admin admin 397 Sep  6 10:02 /home/admin/.rbenv/shims/rails
$ rails --version
Rails 4.1.5

rbenvの仕組み

rubyなどのコマンドが実行されると、以下のような動作となります。

  • 実行されたコマンドを環境変数PATHから検索する

  • 環境変数PATHには、 ~/.rbenv/shimsが追加されているため、shims以下に作成されたコマンドと
    同じ名前のスクリプトが実行される

echo $PATH
/path/to/user_home/.rbenv/shims:/path/to/user_home/.rbenv/bin:/usr/local/bin:/usr/bin:/usr/local/sbin:/usr/sbin:/path/to/user_home/bin

たとえば、rubyコマンドが実行された場合は、~/.rbenv/shims/rubyが実行されることになります。

~/.rbenv/shims/ruby
#!/usr/bin/env bash
set -e
[ -n "$RBENV_DEBUG" ] && set -x

program="${0##*/}"
if [ "$program" = "ruby" ]; then
  for arg; do
    case "$arg" in
    -e* | -- ) break ;;
    */* )
      if [ -f "$arg" ]; then
        export RBENV_DIR="${arg%/*}"
        break
      fi
      ;;
    esac
  done
fi

export RBENV_ROOT="/path/to/user_home/.rbenv"
exec "/path/to/user_home/.rbenv/libexec/rbenv" exec "$program" "$@"
  • ~/.rbenv/shims/ruby からrbenvが実行される

  • rbenvが、 .ruby-version または ~/.rbenv/version により利用するRubyのバージョンを切替を行う

まとめ

rbenvでは、gemを追加した場合はrbenv shimsで、shims以下のスクリプトをメンテナンスする必要がある。

参考

23
25
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
23
25