[PYTHON] Explanation for those who are having trouble with "command not found" in rbenv or pyenv

Target readers: Beginners of Ruby, Python, etc. who are in trouble with this error message

$ fluentd
rbenv: fluentd: command not found

The `fluentd' command exists in these Ruby versions:
   2.6.3

If you are using ** env, such as rbenv or pyenv, you may get an error message like this.

To solve this problem, you need to understand how ** env works.

How ** env works

In the first place, if you are using ** env, just moving the directory like this will automatically switch the version of ruby and python, but how is this realized? Are you there?

$ ruby --version
ruby 2.3.7p456 (2018-03-28 revision 63024) [universal.x86_64-darwin18]
$ cd my-project
$ cat .ruby-version
2.4.5
$ ruby --version
ruby 2.4.5p335 (2018-10-18 revision 65137) [x86_64-darwin18]

Actually, the ruby running here is not the real ruby! It's a script located in ~ / .rbenv / shims /.

$ which ruby
/Users/x-xxxxx/.rbenv/shims/ruby

$ cat ~/.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="/Users/x-xxxxx/.rbenv"
exec "/Users/x-xxxxx/.rbenv/libexec/rbenv" exec "$program" "$@"

The same applies to bundled commands such as bundler and gem, and commands added by Gem.

$ which gem
/Users/x-xxxxx/.rbenv/shims/gem

$ which bundle
/Users/x-xxxxx/.rbenv/shims/bundle

$ which fluentd
/Users/x-xxxxx/.rbenv/shims/fluentd

And if you set up with rbenv init normally,~ / .rbenv / shims /will be added to the beginning of $ PATH and it will be used with the highest priority.

Flow until the error message is displayed

The ~ / .rbenv / shims / script will try to execute the command of the same name in the ** currently in use version of Ruby **.

Therefore, if the command is not installed in ** the version of Ruby currently in use **, an error will occur;

  1. You run fluentd
  2. $ PATH is searched from the beginning and found first ~ / .rbenv / shims / fluentd is executed
  3. ~ / .rbenv / shims / fluentd searches the current directory for .rbenv-version to determine the Ruby version.
  4. I try to run flunetd in that version of Ruby, but I can't find fluentd (although it's installed in another version of Ruby)
  5. Display the following error message
$ fluentd
rbenv: fluentd: command not found

The `fluentd' command exists in these Ruby versions:
   2.6.3

Countermeasures

Specify the command with an absolute path

Execute directly without going through ~ / .rbenv / shims / hoge.

~/.rbenv/versions/2.6.1/bin/ruby

Add to the beginning of $ PATH

Add the path before ~ / .rbenv / shims.

Note that Ruby and Python have the ability to install commands in a directory directly under your home (not in the interpreter directory). It's a good idea to add that directory to your $ PATH.

$ export PYTHONUSERBASE=~/.local
$ pip3 install --user awscli
$ ls ~/.local/bin jupyter-notebook
$ ls ~/.local/bin/
jupyter
jupyter-bundlerextension
jupyter-console
jupyter-kernel
...
# ~/.bashrc
eval "$(pyenv init -)"
export PATH=$HOME/.local/bin:$PATH

Don't use rbenv init

In fact, you can build Ruby with rbenv install without using rbenv init.

For some people it may be enough to build.

Used via pipenv or bundler

Installing and using Ruby and Python commands globally is the source of trouble in the first place. Always use via pipenv or bundler.

Recommended Posts

Explanation for those who are having trouble with "command not found" in rbenv or pyenv
For Debian users who are having trouble with a bug in the Linux kernel 5.10
For those who are in trouble with an error when pip install xg boost
For those who are in trouble because NFC is read infinitely when reading NFC with Python
Environment construction procedure for those who are not familiar with the python version control system
Command not found with sudo rosdep init in ROS setup
For those who are analyzing in atmosphere (Linear Regression Model 1)
Are you having trouble with "pipenv.exceptions.ResolutionFailure"?
Java SE8 Gold measures (for those who are not good at it)
[YOLO v5] Object detection for people who are masked and those who are not
Tips for those who are wondering how to use is and == in Python
[Solved] I have a question for those who are familiar with Python mechanize.
Java SE8 Gold measures (for those who are not good at it)
Infrastructure automation tool Ansible for people who are not good at Ruby
Developed a simple spreadsheet app for people who are not good at Excel
[YOLO v5] Object detection for people who are masked and those who are not
Environment construction procedure for those who are not familiar with the python version control system
Explanation for those who are having trouble with "command not found" in rbenv or pyenv
Image inspection machine for those who do not do their best
For those who are analyzing in atmosphere (Linear Regression Model 1)
For those who are new to programming but have decided to analyze data with Python