How to uninstall all gems installed using `bundle install`

后端 未结 10 1863
情歌与酒
情歌与酒 2020-12-23 17:09

How can I remove all the gems installed using bundle install in a particular RoR project. I don\'t want to uninstall gems that are used by other projects.

相关标签:
10条回答
  • 2020-12-23 17:44

    It is actually as simple as

    gem list --no-versions | xargs gem uninstall -a
    

    If you are not using RVM/RBENV, you might hit into issue when gem attempts to uninstall system library which can fail. In that case, call uninstall command one by one to skip these.

    gem list --no-versions | xargs -n1 gem uninstall -a
    
    0 讨论(0)
  • 2020-12-23 17:46

    If your problem is similar to mine, then you want to uninstall all gems that were installed while testing GemFile changes, in that case I simply used:

    bundle clean
    

    That uninstalled all the gems that are not specified in the GemFile and GemFile.lock.

    I have not tested it but I suppose you could delete all lines from your Gemfile and run the above command to get rid of all the gems installed by the ROR project in the current directory.

    0 讨论(0)
  • 2020-12-23 17:46

    Found solution for uninstalling all gems except of default:

    Crete delete_gems.rb with

    #!/usr/bin/env ruby
    # Remove all gems EXCEPT defaults :)
    
    `gem list -d`.split(/\n\n^(?=\w)/).each do |data|
      match = data.match(/(?<name>([^\s]+)) \((?<versions>.*)\)/)
      name = match[:name]
      versions = match[:versions].split(', ')
    
      if match = data.match(/^.*\(([\d\.]*),? ?default\): .*$/)
        next if match[1].empty? # it's the only version if this match is empty
        versions.delete(match[1] || versions[0])
      end
    
      versions.each { |v| system "gem uninstall -Ix #{name} -v #{v}" }
    end
    

    Run this script:

    sudo chmod 1777 delete_gems.rb
    ./delete_gems.rb
    

    All gems will be removed except of default. Here link on original solution http://zanshin.net/2013/06/10/how-to-delete-all-ruby-gems/

    0 讨论(0)
  • 2020-12-23 17:49

    As others have mentioned you could use rvm. To list all available gemsets

    rvm gemset list
    

    And then

    rvm gemset empty <gemset-name>
    
    0 讨论(0)
  • 2020-12-23 17:57

    Comment out all gems in your Gemfile and run

    bundle clean --force
    
    0 讨论(0)
  • 2020-12-23 17:58

    Another way from https://makandracards.com/jan0sch/9537-uninstall-all-ruby-gems-from-your-system (similar to rainkinz's answer and Ralph's comment). Here are some variations:

    # if you're the root user:
    gem list | cut -d" " -f1 | xargs -I % gem uninstall -aIx %
    
    # if you're a non-root user:
    gem list | cut -d" " -f1 | xargs -I % sudo gem uninstall -aIx %
    
    # docker compose (if your service is named "web" running the root user):
    docker-compose run web bash -c 'gem list | cut -d" " -f1 | xargs -I % gem uninstall -aIx %'
    
    ####
    
    gem install bundler
    # or if using docker compose:
    docker-compose run web gem install bundler
    
    # optionally reinstall gems:
    bundle install
    # or if using docker compose:
    docker-compose run web bundle install
    

    Breaking this down:

    • gem list lists all gems
    • cut -d" " -f1 takes the first column
    • xargs -I % gem uninstall -aIx % calls gem uninstall -aIx with each output line as an argument

    Note that I specified the argument with -I as % and passed it directly for security:

    xargs -I % gem uninstall -aIx %
    

    instead of:

    xargs gem uninstall -aIx
    

    That's because xargs has a security issue where options like -n can be passed to its command and cause unexpected results. This can be demonstrated with the following example:

    # correctly prints "-n hello" (with trailing newline):
    echo '-n Hello' | xargs -I % echo % | xargs -I % echo %
    
    # incorrectly prints "hello" (without trailing newline):
    echo '-n Hello' | xargs echo
    
    0 讨论(0)
提交回复
热议问题