82

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.

waldyr.ar
  • 14,424
  • 6
  • 33
  • 64

8 Answers8

82

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
werkshy
  • 1,605
  • 1
  • 13
  • 22
  • 4
    I wanted to add that there might be binaries created that you've got to delete too, this was the case for me with the charlock_holmes gem. I had to delete the following path as well to get it to fully rebuild: `vendor/bundle/ruby/2.0.0/extensions/x86_64-linux/2.0.0/charlock_holmes-0.6.9.4/` – lfxgroove Feb 22 '15 at 19:24
  • 2
    This worked for me along with the tip from @lfxgroove. On my bundler-and-capistrano based installation, the relevant hierarchy was not under `vendor/` but `shared/bundle/ruby` etc. – Leo Aug 25 '15 at 09:06
  • When doing this with a gemfile, you need to do a bundle install without the gem in the Gemfile (commented out) to remove it from gemfile.lock, then add it back (uncomment) in order for it to reinstall – Carpela Apr 06 '17 at 12:34
48

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

BooVeMan
  • 353
  • 1
  • 8
Calin
  • 6,661
  • 7
  • 49
  • 80
  • 1
    I use this every-time I screw up debugging with `EDITOR=vim bundle open my-gem` – equivalent8 Feb 10 '15 at 16:39
  • 1
    This should be the accepted answer, as it does what the OP required – BooVeMan Jun 29 '15 at 10:46
  • `bundle exec gem pristine` restores the gem from the local cache. So, if the gem exists only in your project, it would not work with `Bundler::GemNotFound` error. @Magne's answer followed by `bundle install` should do the job in that case. – Masa Sakano Sep 18 '22 at 15:01
14

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.

waldyr.ar
  • 14,424
  • 6
  • 33
  • 64
  • I have a similar problem, but the gem in question was installed **using** the `bundle` command and is **not present at all** in the output of `gem q --L`. It is only listed by `bundle list`, but the bare `gem` command cannot "see" it, so it cannot uninstall/reinstall it. `gem uninstall gem-in-question` finishes w/o errors (or any output for that matter), but has no effect on the contents of `bundle list`, and a subsequent `bundle install` just says "Your bundle is complete!". What to do now? – Szczepan Hołyszewski Sep 19 '13 at 08:24
  • Well, I have no idea. You could run a `bundle update` before listing, although I don't know if it's going to repair. – waldyr.ar Sep 19 '13 at 15:17
  • 2
    Have you tried `bundle exec gem uninstall the-gem-in-question`? I know that `gem list` shows you all versions but `bundle exec gem list` show those gems specific to the bundle. – mr rogers Sep 22 '13 at 01:30
  • Szczepan - I had the same problem. I deleted my gem from Gemfile.lock but that didn't do it. I then ran a find under .rvm/gems for my gem name and deleted all instances - it was in a few bundler cache directories. Subsequently, running "bundle install" re-installed the gem. HTH. – Jonathan Swartz Dec 04 '14 at 19:44
13

From project directory in terminal

gem uninstall gem_name 
Sazzad
  • 773
  • 11
  • 22
10

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
JosephL
  • 5,952
  • 1
  • 28
  • 25
10

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

Magne
  • 16,401
  • 10
  • 68
  • 88
7

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
ioquatix
  • 1,411
  • 17
  • 32
  • The `bundle uninstall ` equivalent command you're looking for is `bundle exec gem uninstall ` Cheers https://stackoverflow.com/a/48094089/380607 – Magne Jan 04 '18 at 11:18
3

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
Matthias Winkelmann
  • 15,870
  • 7
  • 64
  • 76