How to uninstall all gems installed using `bundle install`

后端 未结 10 1933
情歌与酒
情歌与酒 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: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
    

提交回复
热议问题