How to reinstall a gem using bundler - ruby

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.

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

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.

From project directory in terminal
gem uninstall gem_name

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

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

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

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

Related

Issues serving Jekyll to localhost, eventmachine 1.2.7 has an error when installing

Not sure what to do to specifically fix this problem, googled and nothing solved my question. When I try to:
bundle exec jekyll serve
I get told:
Could not find eventmachine-1.2.7 in any of the sources
Run `bundle install` to install missing gems.
Then I:
bundle install
Only to get this:
Installing eventmachine 1.2.7 with native extensions
Gem::Ext::BuildError: ERROR: Failed to build gem native extension.
current directory: /private/var/folders/7f/2c2swwc1153899dmr8781_x40000gn/T/bundler20201130-34411-1lzt2fceventmachine-1.2.7/gems/eventmachine-1.2.7/ext
/System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/bin/ruby -I /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/2.6.0 -r
./siteconf20201130-34411-pd54nh.rb extconf.rb
mkmf.rb can't find header files for ruby at /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/include/ruby.h
You might have to install separate package for the ruby development
environment, ruby-dev or ruby-devel for example.
extconf failed, exit code 1
Gem files will remain installed in /var/folders/7f/2c2swwc1153899dmr8781_x40000gn/T/bundler20201130-34411-1lzt2fceventmachine-1.2.7/gems/eventmachine-1.2.7 for
inspection.
Results logged to
/var/folders/7f/2c2swwc1153899dmr8781_x40000gn/T/bundler20201130-34411-1lzt2fceventmachine-1.2.7/extensions/universal-darwin-20/2.6.0/eventmachine-1.2.7/gem_make.out
An error occurred while installing eventmachine (1.2.7), and Bundler cannot continue.
Make sure that `gem install eventmachine -v '1.2.7' --source 'https://rubygems.org/'` succeeds before bundling.
In Gemfile:
minima was resolved to 2.5.1, which depends on
jekyll-feed was resolved to 0.13.0, which depends on
jekyll was resolved to 4.0.0, which depends on
em-websocket was resolved to 0.5.1, which depends on
eventmachine
Where am I going wrong here? Just updated to macOS 11.0.1, this is my first time trying to serve jekyll since the update. Thanks y'all!
Just try:
gem install eventmachine -v '1.2.7' -- --with-cppflags=-I/usr/local/opt/openssl/include
I had the same problem on Big Sur and this solution worked perfectly:
How to fix Jekyll after upgrading to MacOS 11 (Big Sur)
Download XCODE 12 beta from here and copy it to the Applications folder.
Then follow these steps to install rbenv.
git clone https://github.com/rbenv/rbenv.git ~/.rbenv
cd ~/.rbenv && src/configure && make -C src
Add ~/.rbenv/bin to your $PATH for access to the rbenv command-line utility. see: https://github.com/rbenv/rbenv#basic-github-checkout
xcode-select --switch /Applications/Xcode-beta.app/Contents/Developer
Now in your Jekyll project's root, you should be able to run bundle install and it should install all missing dependencies and should work now.
I didn't have to build ruby or install XCode. While Jekyll requires Ruby 2.4.0 or higher and Big Sur comes with 2.6.3, I followed Jekyll's instructions to install the latest version of Ruby.
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
brew install ruby
Add Ruby to your shell configuration in .bash_profile.
PATH="/usr/local/opt/ruby/bin:$PATH"
export PATH
Install bundler. I had to upgrade to Jekyll 4.2 from 4.0, and install webrick which is no longer a default gem with Ruby 3.0. If Jekyll is installed globally, then install it, webrick, and any other gems, like jekyll-paginate, too.
gem install --user-install bundler jekyll
Add the gems path to your shell configuration in .bash_profile, replacing X.X with with first two digits of the Ruby version.
PATH="$HOME/.gem/ruby/X.X.0/bin:$PATH"
export PATH

Rails bundle install doesn't work 'cause json version error

I just wanted to convert from ImageMagick v7 to ImageMagick v6.
while doing that, this error was happened.
bundle install doesn't work correctly.
It seems like json version is something wrong.
How do I fix this error?
Environment below
ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-darwin17]
Rails -v (couldn't find gem) (becuase bundle install can't work)
json list / json (default: 1.8.3)
multi_json (1.13.1, 1.11.2, 1.11.0)
Bundler version 1.16.4
Mac Mojave 10.14
when I do bundle install on terminal, then I got this error
Fetching gem metadata from https://rubygems.org/........
Fetching gem metadata from https://rubygems.org/.
Resolving dependencies.......
Using rake 12.3.2
Using concurrent-ruby 1.1.4
Using i18n 0.9.5
Fetching json 1.8.6
Installing json 1.8.6 with native extensions
Errno::EPERM: Operation not permitted # chmod_internal -
/Users/***/projects/***/vendor/bundle/ruby/2.3.0/gems/json-1.8.6/tests/test_json.rb
An error occurred while installing json (1.8.6), and Bundler cannot continue.
Make sure that `gem install json -v '1.8.6' --source
'https://rubygems.org/'` succeeds before bundling.
In Gemfile:
rails was resolved to 4.2.6, which depends on
actionmailer was resolved to 4.2.6, which depends on
actionpack was resolved to 4.2.6, which depends on
actionview was resolved to 4.2.6, which depends on
rails-dom-testing was resolved to 1.0.9, which depends on
rails-deprecated_sanitizer was resolved to 1.0.3, which depends on
activesupport was resolved to 4.2.6, which depends on
json
I tried to do like this because error statement says Make sure that
gem install json -v '1.8.6' --source 'https://rubygems.org/' succeeds before bundling
but result shows like this
ERROR: While executing gem ... (Errno::EPERM)
Operation not permitted # chmod_internal - /Users/***/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0/gems/json-1.8.6/tests/test_json.rb
I can't understand this error statement
Errno::EPERM: Operation not permitted # chmod_internal -
Also, I goggled a lot, then I update commandlinetool follow this
https://howchoo.com/g/m2u0mmuwzda/macos-mojave-fix-invalid-active-developer-path
then, I did this command again,
sudo gem install json -v '1.8.6' --source 'https://rubygems.org/'
then, it's completely succeded like this.
Building native extensions. This could take a while...
Successfully installed json-1.8.6
Parsing documentation for json-1.8.6
Installing ri documentation for json-1.8.6
Done installing documentation for json after 1 seconds
1 gem installed
But, if I do bundle install, still doesn't work: they show same error.
My recommendation is if possible to start fresh. Maybe your rbenv was not installed correctly, try reinstalling it, I suggest using brew install rbenv https://github.com/rbenv/rbenv#homebrew-on-macos
Make sure to add the eval "$(rbenv init -)" to your ~/.bash_profile then open a new terminal.
Navigate to the project directory and install the Ruby version you need: rbenv install 2.3.1
You can make sure you are using that version by issuing rbenv use 2.3.1 and ruby --version.
Now install bundler for that Ruby version, I suggest 1.17.3 for now (the latest before 2.0.1) gem install bundler -v '1.17.3'.
You should be ready to bundle install.
The most common scenario I see for a message Installing ... with native extensions to result in errors is usually due to the lack of binaries, header files and C related code to build that native extension, in your case the issue is due to permissions, hence why I am suggesting some fresh installation.
I second #Danilo Cabello's recommendation to start fresh if you can. I just have a few other trouble-shooting ideas:
The fact that your bundle is installing gems in /vendor/bundle means that at some point, you must have specified the path with bundle install --path vendor/bundle as #mogbee alludes to. That path flag will load files associated with your gems into vendor/bundle instead of your system gem location. You might have done that if you're trying to keep the project's gems separate from any other project, but if not, you will need to update your bundler's gem path.
To do this, first, check for any issues by running bundle doctor. If no issues are found, check your bundle configuration with bundle env. Make sure that your RubyGems Gem Home and Gem Path are routed through .rbenv, so they should match and look like /Users/***/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0. If they don't match, run the rbenv-doctor command below which should make sure your rbenv installation exported the path properly.
If the output of bundle env tells you that you're actually running an older version of bundler (older than 1.16.4), I would definitely update, and would recommend version 1.17.3 as #Danilo Cabello did.
Second, I would run this rbenv-doctor curl command to check the status of your rbenv install: curl -fsSL https://github.com/rbenv/rbenv-installer/raw/master/bin/rbenv-doctor | bash (https://github.com/rbenv/rbenv-installer#rbenv-doctor).
Third, depending on the output of ls -l within your project folder, you may also want to recursively change owner/group for your ~/.rbenv folder to make sure everything is owned by you and not root (https://superuser.com/questions/260925/how-can-i-make-chown-work-recursively/260939#260939)

Uninstalling Ruby gems

user#machine:~$ gem list
*** LOCAL GEMS ***
bigdecimal (1.2.8)
did_you_mean (1.0.0)
io-console (0.4.5)
json (1.8.3)
minitest (5.8.4)
net-telnet (0.1.1)
power_assert (0.2.7)
psych (2.0.17)
rake (10.5.0)
rdoc (4.2.1)
sass (3.4.23)
test-unit (3.1.7)
user#machine:~$ sudo gem uninstall rake
ERROR: While executing gem ... (Gem::InstallError)
rake is not installed in GEM_HOME, try:
gem uninstall -i /usr/share/rubygems-integration/all rake
user#machine:~$ sudo gem uninstall -i /usr/share/rubygems-integration/all rake
Remove executables:
rake
in addition to the gem? [Yn] Y
ERROR: While executing gem ... (Gem::FilePermissionError)
You don't have write permissions for the /usr/share/rubygems-integration/all/bin directory.
user#machine:~$ ls /usr/share/rubygems-integration/all/
specifications
user#machine:~$
I can not uninstall this gem in particular, neither use it :(
If seems that it doesn't exist.
My ruby verison is 2.3.1.p112
Ideally you would use RVM or rbenv to create a local sandbox and handle your Ruby install and gems instead of the system default. Then you won't have to use sudo you can just use gem uninstall <gem>. It doesn't let you have access for a reason, so it's harder to accidentally mess up the system Ruby.
If you can just read these install directions to get set up with RVM and then install the gems you used previously in your new environment. It'll save you a lot headaches in the future managing Ruby and gems.
--
If you don't want to do that you could try this answer
Try this answer from this related question.
gem list -d 'name of gem' and note the "Installed at:" location
sudo gem uninstall 'name of gem' -i 'the path noted above'
If it still returns a permissions error. Create a folder /bin, in the path above. Continue uninstalling as in step 2, still using the original path (/System/Library/Frameworks/Ruby.framework/Versions/1.8/usr/lib/ruby/gems/1.8).
sudo is not a magic wand that casts a spell “do it anyway.”
When you list your gems, you execute plain gem list from the user. It lists gems installed for the user. Afterwards you try sudo gem uninstall rake. Why do you expect superuser’s list of gems being the same as user’s one?
To remove user’s gems, listed with gem list, use gem uninstall rake. Without sudo.
To list superuser’s gems, that you were tried to remove with sudo gem uninstall rake, use sudo gem list. With sudo.
More info on sudo: http://aplawrence.com/Basics/sudo.html

Ruby json gem not showing after install

So I installed the json gem using:
sudo gem install json
Everything appeared to install correctly, but when I call gem list I get this:
bundler (1.2.1)
hpricot (0.8.6)
i18n (0.6.1)
mail (2.4.4)
mime-types (1.19)
polyglot (0.3.3)
rack (1.4.1)
rack-protection (1.2.0)
rake (10.0.0)
rubygems-bundler (1.1.0)
rvm (1.11.3.5)
sinatra (1.3.3)
tilt (1.3.3)
treetop (1.4.12)
As you can see there is no json gem, and it is making it so I can run my Ruby Program. Any help would be great!
You installed JSON using sudo but it appears you're running RVM. Under RVM, using sudo to install gems, is a big NO-NO and causes this sort of problem.
sudo gem install json installed it over the pre-existing JSON in your system, not into the RVM-controlled Ruby.
Do it again, but do NOT use sudo and see what happens.
And, unless you understand what's happening with sudo gem install json or some other gem, don't use sudo gem install or sudo gem remove.
EDIT:
Per the Troubleshooting page on RVM's site, here's how to remove the system-wide installation:
#!/bin/bash
/usr/bin/sudo rm -rf $HOME/.rvm $HOME/.rvmrc /etc/rvmrc /etc/profile.d/rvm.sh /usr/local/rvm /usr/local/bin/rvm
/usr/bin/sudo /usr/sbin/groupdel rvm
/bin/echo "RVM is removed. Please check all .bashrc|.bash_profile|.profile|.zshrc for RVM source lines and delete
or comment out if this was a Per-User installation."
If you need sudo permissions to install a gem you should use: rvmsudo
In this case, it fixed my problem without having to reinstall everything.

How to remove installed ri and rdoc?

How can I remove the ri and rdoc of installed gems? Thanks
You can simply remove the doc directory in the RubyGems installation directory.
rm -r `gem env gemdir`/doc
On Mac OS X by default, it's /Library/Ruby/Gems/1.8/doc.
Keep in mind that there might be several installation directories for RubyGems.
RubyGems will try to install to your user directory (something like ~/.gem/ruby/1.8/) if it can't access the normal installation directory (e.g. you installed a gem without sudo).
RVM also installs RubyGems for each Ruby it installs which will contain a doc directory containing rdoc and ri files (e.g. ~/.rvm/gems/ruby-1.9.2-preview1/doc).
This will merely remove existing files, but new ones will come with new installations anyway, unless you use the --no-document flag for gem install or make it a default.
It worked for me when I tried this:
gem rdoc <gem name> --no-ri --overwrite
Then you can remove only ri of the gem, and leave the gem itself. I don't see we need any other way around for this.
I just had this problem to, after thinking it would be a good idea to have some local rdoc for the train etc, I completely filled up my disc!
So, I bit the bullet, and did it (the hard way).
First, this is how I got in this stupid predicament :-
$ gem install rdoc-data
$ rdoc-data --install
$ gem rdoc --all --overwrite
To undo this, I thought I'd uninstall the gems, and then install them as needed (but without rdoc!)
Get list :-
$ gem list
*** LOCAL GEMS ***
aasm (2.1.1)
actionmailer (3.2.3, 3.2.2, 3.1.4, 3.1.3, 3.1.1, 3.1.0, 2.3.8, 2.3.5)
actionpack (3.2.3, 3.2.2, 3.1.4, 3.1.3, 3.1.1, 3.1.0, 2.3.8, 2.3.5)
active_support (3.0.0)
... +100 ...
Copy and search/replace \(.*\) with '\'
Bulk uninstall :-
$ gem uninstall actionmailer \
actionpack \
active_support \
... 100 more ...
ZenTest \
-a -x -I
Watch as the disc gains GB's of free space! (~11GB!)
Twas a stupid idea in the first place, and my solution may be just as much so, but it worked.

Resources