How do I uninstall Ruby Gems in Ubuntu?

Recently, I was attempting to install a Ruby application and as part of this process installed multitude of gems. Now, I no longer need this application and was looking forward for a quick solution to uninstall all the gems associated with it. This article outlines a summarized list of commands to achieve the same.

# List all the gems installed in your machine (example output is captured from my machine).
root@labs:~$ gem list

*** LOCAL GEMS ***

bundler (1.2.1)
daemon_controller (1.0.0)
daemons (1.1.0)
eventmachine (1.0.0)
fastthread (1.0.7)
hoe (3.1.0)
passenger (3.0.17)
rack (1.4.1)
rake (0.8.7)
rmagick (2.13.1)
stompserver (0.9.9)

# If you prefer to uninstall each gem individually, utilize the below command.
root@labs:~$ gem uninstall -Iax 

# Example:
root@labs:~$ gem uninstall -Iax passenger

# If you prefer to uninstall all of them, either run the uninstaller via a for loop like below:
root@labs:~$ for gem_name in $(gem list|awk '{print $1}'); do
> gem uninstall -Iax "${gem_name}"
> done

# Or, utilize xargs as listed at:
root@labs:~$ gem list | awk '{print $1}' | xargs gem uninstall -Iax

# Legend (captured from the help documentation on gem for reference):
Usage: gem uninstall GEMNAME [GEMNAME ...] [options]

    -a, --[no-]all                   Uninstall all matching versions
    -I, --[no-]ignore-dependencies   Ignore dependency requirements while
    -x, --[no-]executables           Uninstall applicable executables without
    -i, --install-dir DIR            Directory to uninstall gem from
    -n, --bindir DIR                 Directory to remove binaries from
        --[no-]user-install          Uninstall from user's home directory
                                     in addition to GEM_HOME.
        --[no-]format-executable     Assume executable names match Ruby's prefix and suffix.
    -v, --version VERSION            Specify version of gem to uninstall
        --platform PLATFORM          Specify the platform of gem to uninstall

  Common Options:
    -h, --help                       Get help on this command
    -V, --[no-]verbose               Set the verbose level of output
    -q, --quiet                      Silence commands
        --config-file FILE           Use this config file instead of default
        --backtrace                  Show stack backtrace on errors
        --debug                      Turn on Ruby debugging

    GEMNAME       name of gem to uninstall

    Uninstall gems from the local repository



Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s