Search code examples
rubyrubygems

Installing dependencies from a gemspec file


I wish to work on an existing gem, which I've checked out from a git repository. I'm using a clean Ruby installation. To start, I would like to install the dependencies specified in the gemspec file so that I can build and test the gem. I'm not using bundler for this.

Currently, I manually install the gems by reading the gemspec and invoking 'gem'. What I'd like to do is something like:

git clone my_gem
cd my_gem
gem magically-install-dependencies my_gem.gemspec

and have all of the runtime and development dependencies installed as they would be if I'd installed this from a .gem file. Is there a way to do this?

I've searched the gem documentation and there doesn't seem to be anything that works on gemfiles other than build.


Solution

  • Rubygems has adopted bundler into itself, so that the distinction between both tools is now diminishing. Still, you can install gems from a gemspec file with just a gem install. This command supports an optional -g flag where you can specify a gem dependencies API file. The documentation says the following about this flag:

    RubyGems can install a consistent set of gems across multiple environments using gem install -g when a gem dependencies file (gem.deps.rb, Gemfile or Isolate) is present. If no explicit file is given RubyGems attempts to find one in the current directory.

    When the RUBYGEMS_GEMDEPS environment variable is set to a gem dependencies file the gems from that file will be activated at startup time. Set it to a specific filename or to “-“ to have RubyGems automatically discover the gem dependencies file by walking up from the current directory.

    NOTE: Enabling automatic discovery on multiuser systems can lead to execution of arbitrary code when used from directories outside your control.

    Now, the easiest way to create such a gem dependencies file is to create a simple Gemfile with the following verbatim content in the same directory as your gemspec file:

    source 'https://rubygems.org'
    gemspec
    

    This file instructs gem install -g (as well as bundle install) to fetch the gemspec file from the current directory and use the dependencies specified there for installation.

    You can use bundler if you want. In that case, there is no need to commit or retain the Gemfile.lock if you always want to install the newest gem versions available.

    That way, you aren't strictly dependent on bundler but can still use the automatic facilities provided by it.