How to run a ruby script within bundler context? How to run a ruby script within bundler context? ruby ruby

How to run a ruby script within bundler context?


Pass the script name to the ruby command:

bundle exec ruby script_name

If you also want the Rails environment:

bundle exec rails runner script_name


For instance, I wanted to use the same version of Rubocop as my Rails app and not the latest system one, so doing this in a script:

require 'bundler'Bundler.require# ...

Allowed me to use my app's version of rubocop.


You can just make it a script - add

#!/usr/bin/env ruby

to the start of the file, and make it executable. Then bundle exec foo.rb will work as expected.

(This is on unix or OSX - not sure about Windows)

See http://bundler.io/v1.15/man/bundle-exec.1.html#Loading

Also see https://coderwall.com/p/kfyzcw/execute-ruby-scripts-directly-without-bundler-exec for how to run ruby scripts with bundled dependencies, without needing bundle exec