66

When using node package manager you can specify npm install --save mynodemodule which automatically pops the module in package.json

I was wondering if there is a command for bundler that allows you to add the gem and version to the gemfile from the command line?

For example bundle install --save nokogiri

sren
  • 3,513
  • 4
  • 25
  • 28
  • Yeah, it looks like gem doesn't have this feature, that's weird, because almost every package manager has something similar. – Vedmant Oct 14 '16 at 14:40

4 Answers4

76

If you visit this question in 2018, bundler now has a cli to do this: bundle add <gem-name> <version>

Version string could be your typical gem version including >= and ~

zocoi
  • 990
  • 8
  • 11
  • This should be the default behavior of `gem install`. Nothing happens when you just do `gem install`, from what I can tell. It doesn't get installed in the project. – Andrew Koster Aug 18 '19 at 04:27
  • @AndrewKoster i'm late but the 'gem' gem is separate from rails, it is typically a system-wide gem packaged with Ruby containing executables used to install gems to the system, as well as package and publish gems. it has no concept of Bundler or a Gemfile, which are rails/application specific conventions. – speakingcode Mar 04 '20 at 17:39
  • I guess I forgot that unlike `npm` which can be used to install libraries globally or in a project folder, `gem` only does global installs. Probably because Rails didn't have a built-in Gemfile back when I first started using it. – Andrew Koster Mar 06 '20 at 02:31
  • Finally I found the answer for my doubt. – Víctor Hugo Jul 30 '23 at 20:29
50

Just wrote Gemrat to do this.

    $ gem install gemrat 
    $ gemrat nokogiri

    #=> gem 'nokogiri', '1.6.0' added to your Gemfile.
    #=> Bundling...
Dru
  • 9,632
  • 13
  • 49
  • 68
  • 3
    +1 for the awesome meme in the readme of Gemrat. Also a good, convenient gem – taylorc93 Aug 07 '13 at 14:20
  • I'm not convinced that locking the Gem to an exact version in the Gemfile by default is the best idea. Gemfile.lock is there to lock the exact version. Setting a minimum version, sure... but if I used this I'd either have to always use one of the other options or decide never to update my gems. – tgf Sep 11 '15 at 02:07
  • 1
    What about specifying environment? Like adding gems to only the test env, or prod, or dev? – Nick Res Nov 01 '17 at 17:22
25
echo 'gem "nokogiri"' >> Gemfile
dj2
  • 9,534
  • 4
  • 29
  • 52
  • 2
    Not quite, I want it to install it locally, and also chuck in the version automatically too. – sren Nov 04 '11 at 07:35
  • 13
    echo 'gem "nokogiri"' >> Gemfile && bundle install – dj2 Nov 05 '11 at 06:47
  • 10
    I think Simon wants the equivalent of "npm install --save foo". If so, what is wanted is to download and install the latest version of gem foo, then add it to the Gemfile with a version qualifier based on the version that was actually downloaded and installed, e.g. gem "foo", "~> 1.2.3" – sheldonh May 14 '13 at 11:20
11

as @zocoi described, you can use bundle and specify the group:

bundle add rails --group "development, test"

More information

bundle add --help
lesterzone
  • 233
  • 3
  • 6