How to reinstall a gem using bundler

I did a bundle show and get the complete path to a gem directory.

Unfortunately, I removed the directory using rm -r gem_path. Then my rails app isn't working anymore. If I try start server or start rails console it outputs the following error:

<class:Application>: uninitialized constant MyAPP::Application::Gem (NameError)

What should I do to have it back?

I tried bundle install or bundle update in hope of forcing the bundle to search the gem and install it back, but didn't work.

I also tried delete the Gemfile.lock and run bundle install. Nothing changed, same error.

The gem in question is Act as taggable on.

103208 次浏览

First I did a gem q --L, the shortcut for gem query --local. It outputs me the all the local gems installed.

actionmailer (3.2.8, 3.2.6, 3.2.1, 3.1.0)
actionpack (3.2.8, 3.2.6, 3.2.1, 3.1.0)
activemodel (3.2.8, 3.2.6, 3.2.1, 3.1.0)
activerecord (3.2.8, 3.2.6, 3.2.1, 3.1.0)
activeresource (3.2.8, 3.2.6, 3.2.1, 3.1.0)
activesupport (3.2.8, 3.2.6, 3.2.1, 3.1.0)
acts-as-taggable-on (2.3.3)
...

And then, following DVG advice, I uninstalled the gem using its correct name gem uninstall acts-as-taggable-on and ran bundle install. After that I was able to rails c or rails s again without any problem.

If using rbenv, this will let you completely uninstall and re-install a gem such as rmagick:

First: Try a simple uninstall/reinstall

gem uninstall rmagick
bundle install

If that doesn't work, you can remove all trace of the installed gem. Find your gem installation location:

bundle show rmagick
BUNDLE_DIR=$(dirname $(dirname $(bundle show rmagick)))
echo $BUNDLE_DIR

Your gem installation prefix will either be the default e.g. ~/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0 or something you set e.g. .vendor

Clear out the gem directory:

rm -rf $BUNDLE_DIR/gems/rmagick-*

Clear out the compiled gem cache:

rm $BUNDLE_DIR/cache/rmagick*.gem

Also clear out bundler's spec cache:

rm $BUNDLE_DIR/specifications/rmagick*gemspec

Then you can re-install:

bundle install

You can always use:

gem pristine acts-as-taggable-on

pristine - Restores installed gems to pristine condition from files located in the gem cache

If you just want to restore the gem for the current project you should run:

bundle exec gem pristine acts-as-taggable-on

If using RVM with gems in ~/.rvm/, this works if bundle is not re-installing a gem.

First, delete the gem source:

bundle show $GEM
rm -rf $PATH_TO_GEM

Clear out the compiled gem cache:

rm -rf ~/.rvm/gems/ruby-$RUBYVERSION[@$GEMSET]/cache/$GEM.gem

Also clear out bundler's spec cache:

rm -rf ~/.rvm/gems/ruby-$RUBYVERSION[@$GEMSET]/specifications/$GEM*gemspec

Then you can re-install:

bundle install

From project directory in terminal

gem uninstall gem_name

If you're trying to reinstall rake, gem pristine rake will fail with Skipped rake-10.X.X, it is a default gem and bundle won't install it either, because it can't uninstall it.

If you're using rvm, it seems the easiest was is simply to do a rvm reinstall 2.x.x. At least for me, rvm repair all also didn't help.

The same probably goes for all other default gems. I'll just list them here so that the desperate googlers find some help:

  • bigdecimal
  • drip
  • io-console
  • json
  • minitest
  • psych
  • rake
  • rbtree
  • rdoc
  • test-unit

If you've installed into ./bundle/vendor or similar, you need to remove the gem first but explicitly specify the GEM_HOME, e.g.

GEM_HOME=./vendor/bundle/ruby/2.3.0/ gem uninstall rmagick

This is by far the simplest way to uninstall gems installed using bundler into a vendor directory. Ideally, there would be a command bundle uninstall or bundle reinstall, etc.

If your goal is to simply reinstall, the following command will help:

GEM_HOME=./vendor/bundle/ruby/2.3.0/ gem uninstall rmagick && sudo -u http bundle install

If you're like me and have several web-applications under a directory (e.g. /srv/http), the following does it in all directories:

cd /srv/http
for d in ./*/ ; do (cd "$d" && sudo -u http GEM_HOME=./vendor/bundle/ruby/2.4.0/ gem uninstall --force rmagick && sudo -u http bundle install); done

bundle exec gem uninstall <gem_name> - uninstalls gem from the bundle (the <app_root>/vendor/bundle/ruby/2.3.0/gems/ path). This is equivalent to the answer @ioquatix gave, but is the slightly more convenient solution that he was looking for.

gem uninstall <gem_name> - uninstalls gem only from the global gemset in the system