IanC
IanC

Reputation: 2058

Cleaning gems from older gem version

I recently upgraded my distro (Ubuntu from 16.04 to 18.04), and I guess either on the upgrade or on the regular sudo update/upgrade calls gem was upgraded and I currently have the 2.7.6 version. I barely use Ruby lately, but just now I had to run a jekyll/bundle command which returned me that it had a bad interpreter (it was looking for ruby2.3 and it's currently installed on the 2.5 version).

My next step would be updating the jekyll and bundle gems (so they update the interpreter version), but to my surprise they aren't listed when I run gem list. They were obviously installed since there's a /usr/local/bin/jekyll executable and a /usr/local/bin/bundle one as well. But I think when gem was upgraded it started checking gems on a different folder (/var/lib/gems/2.5.0 I'm guessing) but jekyll/bundle apparently are in /var/lib/gems/2.3.0. I'd be fine just installing jekyll/bundle on the 2.5.0 version, but is there a way to remove all gem files from the older version since they are useless now?

Upvotes: 0

Views: 1645

Answers (3)

IanC
IanC

Reputation: 2058

I'd like to thank both answers. I upvoted them because even though they're not a direct solution to my problem they give good directions to prevent it.

NOTE: These are the steps I took, which doesn't mean they are the correct way, so don't take more as a reference than a guide.

Here's what I did to clean up those old Ruby version gems:

  • First I needed to add an environment variable for gem to be able to find those obsolete gems, by running GEM_PATH=/var/lib/gems/2.3.0.
  • Then it's possible to get the contents and specification of the gems with gem contents name_of_gem and gem specification name_of_gem.
  • I individually uninstalled those gems that were obsolete with sudo GEM_PATH=/var/lib/gems/2.3.0 gem uninstall -i /var/lib/gems/2.3.0 name_of_gem. In my case I did a sudo install, so I needed sudo which also required me to set the environment variable again because of sudo's security policy. Also, I needed to manually set the install directory for some reason.
  • Some gems will have executables, and the uninstall will ask if you want to remove it. I wasn't able to get the gem uninstall to remove it because it claimed I didn't have write permission to /var/lib/gems/2.3.0/bin (which is weird because the EXECUTABLE DIRECTORY gem environment variable was /usr/local/bin). I asked gem not to remove the executables, wrote down their names and removed them manually from the EXECUTABLE DIRECTORY folder. You can also use which gem_executable_name to find out where it's located.

After uninstalling all the gems I believe it's safe to remove the /var/lib/gems/2.3.0 folder and its contents. Running gem contents on all of them only returned me files on this folder so I believe the only external files were the script/executables that were added to /usr/local/bin.

Upvotes: 0

Kache
Kache

Reputation: 16687

Here's what I recommend:

  1. Use rbenv for multiple Ruby version management, no customizations needed
    • a ruby installer plugin is now included with rbenv
    • it also handles ruby executable shims automatically, don't need to rbenv rehash anymore
    • it loads really fast (rvm has a noticable load time on shell startup)
  2. Use bundler to dynamically resolve gems at runtime (options below)
    • it's fast enough anyways
    • don't need a special gem solution, bundler comes included /w Ruby now

Options to invoke bundler dynamically (I recommend the last one):

  1. use bundle exec in front of every ruby executable
    • variant: create alias be='bundle exec'
  2. create bundle binstubs <LIST GEM EXECUTABLES YOU WANT> for each project
    • use bin/ in front of every ruby executable to call the binstubs
  3. do #2 and then set up .git/safe
    • lets you manually allow PATH lookups to the bin/ folder while in that project root
    • don't need to type bin/ anymore

Now multiple gem versions will all be installed into the same Ruby version bucket, and you let bundler dynamically add the right versions to the load path before every startup.

Removing a Ruby version will also mean removing all the gems (and versions of those gems) installed for that Ruby version as well.

Upvotes: 2

Foo Bar Zoo
Foo Bar Zoo

Reputation: 216

I highly recommend you to use a ruby version manager like rvm or rbenv to manage different ruby versions installed on your system.

If you just want to remove the gems from your disk, you can find the location of each gem with the command bundle show $gem_name and delete the parent directory to delete all of them. You may need to delete the Gemfile.lock as well to reset the locked gem versions.

Upvotes: 2

Related Questions