Search code examples
rubyrubygemsrbenv

Can I execute gems from outside an rbenv environment?


I am able to install and execute a gem as follows:

rbenv install 2.4.1
rbenv local 2.4.1 # enter the environment
gem install fpm
fpm --version

I was expecting to be able to execute the gem from outside the environment too, something like:

rbenv local --unset # leave the environment
rbenv rehash # update shims
fpm --version

But instead I get:

rbenv: fpm: command not found

The `fpm' command exists in these Ruby versions:
  2.4.1

Have I misunderstood how rbenv shims work? Can I execute a gem from outside an rbenv environment?


Solution

  • From the comments in your Github issue it seems you got the basic idea now; rbenv shims are nothing more than glorified shell scripts that tie a command to a particular currently active Ruby version.

    But nothing prevents you from creating your own hard-coded shims. For example, let's say you want this global fpm command that is available outside any particular rbenv environment.

    Here's a simple way to do it:

    > rbenv local 2.4.1
    > ln -s `rbenv which fpm` ~/.rbenv/bin/fpm24
    > rbenv local --unset
    > fpm24 --version
    1.10.2
    

    This will install a "shim" into ~/.rbenv/bin/fpm24 that is a hard-coded pointer to the 2.4.1 gem that rbenv has installed previously. Now this fpm24 command will work anywhere, as long as you have ~/.rbenv/bin in your PATH.