Reputation: 838
I want to use a Ruby gem locally (not install it for the entire machine) for use in a single script. I know how to install gems with Bundler with a Gemfile and bundle install
. But for a simple script, this seems overkill to set up bundler.
Is there a way to install a gem to a subfolder of my script and use it, similar to the way npm installs Node.js packages in node_modules
?
Here's what I have tried so far.
gem install -i ruby plist
installs the plist
gem in ruby/gems/plist-3.1.0
I tried to require it in my script extract.rb
by doing require './ruby/gems/plist-3.1.0/lib/plist
but that fails with require: cannot load such file: plist/generator
(plist/generator.rb
is required by lib/plist.rb
).
Ruby 2.0 on OSX
Upvotes: 2
Views: 5532
Reputation: 7561
If you don't want to involve Bundler, just install your gems locally as in your example and then set the GEM_PATH env in your script before your require, e.g.:
#!/usr/bin/env ruby
ROOT = File.expand_path('..', __FILE__)
ENV['GEM_PATH'] = File.join(ROOT, 'ruby')
# or to just append to
# ENV['GEM_PATH'] += ":#{ File.join(ROOT, 'ruby') }"
require 'plist'
assuming your script is in the same folder as the ruby
folder (otherwise adjust the filepath accordingly).
Upvotes: 1
Reputation: 108
You can do it by creating gemset for particular application. follow these steps for that -
$ rvm gemset create <gemset_name>
It will create a gemset for currently selected ruby version. you can check currently selected ruby version by this command -
$ rvm list
Then navigate to your app directory by cd into it.
now execute this command -
$ rvm use @<gemset_name>
Now whenever you install any gem it will be installed in current gemset which is being used not for the entire machine.
Make sure - you run gem install bundler
in newly created gemset so it will not raise error when you will run bundle install
.
Upvotes: 0
Reputation: 54674
You can bundle install to a different location with the --path
option, for example:
bundle install --path vendor/bundle
Also see http://bundler.io/v1.1/bundle_install.html
Upvotes: 7