15

I have created a gem(X) and it's not published. Now I am creating an another gem which will add the gem X as a dependency like this :-

s.add_dependency "X"

in gemspec file.

since the gem(X) is not on rubygem or git or rubyforge, the bundle install command throws the error:

could not find the gem X**

I think specifying the path for X will do the trick, but how?

2 Answers 2

22

I think I see what @bor1s was pointing to in the SO post about git references.

The gemspec file is about dependencies for publishing your gem. Your Gemfile sets up your local environment for development, usually by calling gemspec.

In your case your gemspec should have this

s.add_dependency "X"

And then your Gemfile would look something like this (make sure the local reference is after the gemspec call):

source "http://www.rubygems.org"
gemspec
gem "X", :path => "/Users/vikram/source/X"

Quoting from Yahuda Katz's blog post about gemspec and Bundler

If you find that you need to develop against a gem that hasn’t yet been released (for instance, Rails often develops against unreleased Rack, Arel, or Mail gems), you can add individual lines in the Gemfile that tell bundler where to find the gems. It will still use the dependencies listed in the .gemspec for dependency resolution, but it now knows exactly where to find the dependency during gem development.

source "http://www.rubygems.org"

gemspec

# if the .gemspec in this git repo doesn't match the version required by this
# gem's .gemspec, bundler will print an error
gem "rack", :git => "git://github.com/rack/rack.git"

You wouldn’t want to include this information in the .gemspec, which will eventually be released to Rubygems after the in-development gem has also been released. Again, this makes the overall system more resilient, as it doesn’t depend on transient external URLs. This information is purely something used to set up a complete environment during development, which requires some precision.

9
  • This answer was very helpful. Thank you. In my situation I had gem (Y), which depends on gem (X), however, I wanted to take this a step further and put the local development version of gem Y into my Rails application as well. I set things up as suggested and I was able to bundle gem (Y) with no problems. However, simply adding gem 'y', path: "path_to_y" to the Rails Gemfile was not enough. Bundling the Rails app would fail due to bundler not being able to find gem (x). I had to also include gem 'x', path: "path_to_x" even though it's not a direct dependent.
    – WebDev
    Nov 22, 2014 at 3:28
  • @CorlewSolutions that is correct. Any local dependencies need to be specified in the Gemfile read by Bundler, in this case your Rails application Gemfile. Bundler does not read a dependent gem's Gemfile, it only looks at .gemspec files within referenced gems.
    – jwadsack
    Nov 22, 2014 at 17:19
  • I don't understand how this is supposed to work. Bundler errors out during the gemspec call when it can't find the gem and doesn't get to the part where the github definition is placed.
    – Peter P.
    Jan 21, 2016 at 2:46
  • @PeterP. It should load all the definitions, merge them, then try to find the gems. Is the error that it can't find the gem in any of the sources?
    – jwadsack
    Jan 22, 2016 at 18:57
  • Have to agree with @PeterP.: This solution does NOT work. Using the solution from this answer I get: Could not find gem 'my_required_gem', which is required by gem 'my_gem', in any of the sources.
    – Nico
    Feb 1, 2019 at 13:14
0

I think you can find an answer for your question here: Gem dependency

Hope it will help.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.