bundle binstubs

In Ruby, we use Bundler to manage the gem in our projects. We also use bundle exec prefix to run the command in our projects for ensuring that it’s running in current bundler. Eg.

bundle exec rake db:migrate

But, it seems a little inconvenient so that we can use binstubs to omit it. Assume that we want to generate binstubs of rake:

bundle binstubs rake

This will generate rake script in the bin folder, and now we can run


equal to

bundle exec rake

If you want to generate all binstubs of this project, you can run

bundle install --binstubs

But I don’t recommend this because it will generate a config .bundler in the same time. It will re-generate all binstubs when you run bundle install. It will overwrite all the binstubs so that you can not use some gem like spring.

Environment Variable

But we still need to type ./bin/. To solve this, we can add ./bin to environment variable PATH. In Mac we can modify .bash_profile

vi ~/.bash_profile


export PATH="./bin:$PATH"

We have to prepend at first for first priority. And now, we can type rake directly.


If we use rvm, the above method will not work because rvm will overwrite the PATH. In this case, we can use direnv to solve it. We use brew to install:

brew install direnv

And add following config to .bash_profile

eval "$(direnv hook bash)"

And create .envrc in the project folder, add this:

PATH_add bin

After that, it will add bin to environment variable PATH if we enter the project folder. You maybe see the error message at first time.

direnv: error .envrc is blocked. Run `direnv allow` to approve its content.

According to the hint, enter:

direnv allow

Allow it apply the setting.