Ruby2.5 向け、標準外gemをアンインストール

# 標準外gemをアンインストール
[インストールされているgemを一括で削除する - Qiita](https://qiita.com/hachi8833/items/e6b0380c3b6d5e115e36) を参考にします。
除外するgemを、Ruby2.5の標準gemに変更します。


## sh, bash用

```bash
gem uni -aIx $(gem li --no-versions | grep -v -E "bigdecimal|bundler|cmath|csv|date|dbm|did_you_mean|etc|fcntl|fiddle|fileutils|io-console|ipaddr|json|minitest|net-telnet|openssl|power_assert|psych|rake|rdoc|scanf|sdbm|stringio|strscan|webrick|zlib")
```

## fish-shell用

```sh
gem uni -aIx (gem li --no-versions | grep -v -E "bigdecimal|bundler|cmath|csv|date|dbm|did_you_mean|etc|fcntl|fiddle|fileutils|io-console|ipaddr|json|minitest|net-telnet|openssl|power_assert|psych|rake|rdoc|scanf|sdbm|stringio|strscan|webrick|zlib")
```
# エラーが出たら

標準gem以外でもエラーが起きてアンインストールできないことがあります。

## パスエラー

ディレクトリを指定しないとアンイストールできないことがあります。

例えば、私の環境では`test-unit`で次のエラーが出ました。

```sh
~ gem uninstall test-unit
ERROR:  While executing gem ... (Gem::InstallError)
    test-unit is not installed in GEM_HOME, try:
	gem uninstall -i /usr/local/Cellar/ruby/2.5.0/lib/ruby/gems/2.5.0 test-unit
```

インタラクションにしたがって、インストールディレクトリのパスを指定すればアンインストールできます。


```sh
~ gem uninstall -i /usr/local/Cellar/ruby/2.5.0/lib/ruby/gems/2.5.0 test-unit
Successfully uninstalled test-unit-3.2.7
```

## 権限エラー

過去に`sudo gem install`していたgemは、アンインストールする際も、スーパーユーザー権限が必要です。

例えば、私の環境では`rake`で次のエラーが出ました。

```sh
~ gem uninstall rake
ERROR:  While executing gem ... (Gem::InstallError)
    rake is not installed in GEM_HOME, try:
	gem uninstall -i /usr/local/Cellar/ruby/2.5.0/lib/ruby/gems/2.5.0 rake
~ gem uninstall -i /usr/local/Cellar/ruby/2.5.0/lib/ruby/gems/2.5.0 rake
Remove executables:
	rake

in addition to the gem? [Yn]
ERROR:  While executing gem ... (Gem::FilePermissionError)
    You don't have write permissions for the /usr/local/Cellar/ruby/2.5.0/lib/ruby/gems/2.5.0/bin directory.
```

sudoをつけてアンインストールします。

```sh
~ sudo gem uninstall -i /usr/local/Cellar/ruby/2.5.0/lib/ruby/gems/2.5.0 rake
Password:
ERROR:  While executing gem ... (Gem::InstallError)
    rake is not installed in GEM_HOME, try:
	gem uninstall -i /usr/local/lib/ruby/gems/2.5.0 rake
```

エラーが続くときはインタラクションに従い、インストールディレクトリのパスを修正して再実行します。

```
~ sudo gem uninstall -i /usr/local/lib/ruby/gems/2.5.0 rake
Remove executables:
	rake

in addition to the gem? [Yn]
Removing rake
Successfully uninstalled rake-12.3.0
```

### 追記 rakeをアンインストールしてHomebrewでエラーが出たら

上記の手順で`rake`をアンインストールしたあとに、`brew upgrade`したら次のエラーが出ました。

```sh
==> Upgrading ruby
==> Downloading https://homebrew.bintray.com/bottles/ruby-2.5.0_1.sierra.bottle.tar.gz
######################################################################## 100.0%
==> Pouring ruby-2.5.0_1.sierra.bottle.tar.gz
Error: The `brew link` step did not complete successfully
The formula built, but is not symlinked into /usr/local
Could not symlink bin/rake
Target /usr/local/bin/rake
already exists. You may want to remove it:
  rm '/usr/local/bin/rake'

To force the link and overwrite all conflicting files:
  brew link --overwrite ruby

To list all files that would be deleted:
  brew link --overwrite --dry-run ruby

Possible conflicting files are:
/usr/local/bin/rake
/usr/local/lib/ruby/gems/2.5.0/cache/rake-12.3.0.gem
/usr/local/lib/ruby/gems/2.5.0/gems/rake-12.3.0/.gitignore
/usr/local/lib/ruby/gems/2.5.0/gems/rake-12.3.0/CONTRIBUTING.rdoc
/usr/local/lib/ruby/gems/2.5.0/gems/rake-12.3.0/Gemfile
/usr/local/lib/ruby/gems/2.5.0/gems/rake-12.3.0/History.rdoc
/usr/local/lib/ruby/gems/2.5.0/gems/rake-12.3.0/MIT-LICENSE
/usr/local/lib/ruby/gems/2.5.0/gems/rake-12.3.0/README.rdoc
/usr/local/lib/ruby/gems/2.5.0/gems/rake-12.3.0/Rakefile
/usr/local/lib/ruby/gems/2.5.0/gems/rake-12.3.0/bin/console
/usr/local/lib/ruby/gems/2.5.0/gems/rake-12.3.0/bin/setup
/usr/local/lib/ruby/gems/2.5.0/gems/rake-12.3.0/doc/command_line_usage.rdoc
/usr/local/lib/ruby/gems/2.5.0/gems/rake-12.3.0/doc/example/Rakefile1
/usr/local/lib/ruby/gems/2.5.0/gems/rake-12.3.0/doc/example/Rakefile2
/usr/local/lib/ruby/gems/2.5.0/gems/rake-12.3.0/doc/example/a.c
/usr/local/lib/ruby/gems/2.5.0/gems/rake-12.3.0/doc/example/b.c
/usr/local/lib/ruby/gems/2.5.0/gems/rake-12.3.0/doc/example/main.c
/usr/local/lib/ruby/gems/2.5.0/gems/rake-12.3.0/doc/glossary.rdoc
/usr/local/lib/ruby/gems/2.5.0/gems/rake-12.3.0/doc/jamis.rb
/usr/local/lib/ruby/gems/2.5.0/gems/rake-12.3.0/doc/proto_rake.rdoc
/usr/local/lib/ruby/gems/2.5.0/gems/rake-12.3.0/doc/rake.1
/usr/local/lib/ruby/gems/2.5.0/gems/rake-12.3.0/doc/rakefile.rdoc
/usr/local/lib/ruby/gems/2.5.0/gems/rake-12.3.0/doc/rational.rdoc
/usr/local/lib/ruby/gems/2.5.0/gems/rake-12.3.0/exe/rake
/usr/local/lib/ruby/gems/2.5.0/gems/rake-12.3.0/lib/rake/application.rb
/usr/local/lib/ruby/gems/2.5.0/gems/rake-12.3.0/lib/rake/backtrace.rb
/usr/local/lib/ruby/gems/2.5.0/gems/rake-12.3.0/lib/rake/clean.rb
/usr/local/lib/ruby/gems/2.5.0/gems/rake-12.3.0/lib/rake/cloneable.rb
/usr/local/lib/ruby/gems/2.5.0/gems/rake-12.3.0/lib/rake/cpu_counter.rb
/usr/local/lib/ruby/gems/2.5.0/gems/rake-12.3.0/lib/rake/default_loader.rb
/usr/local/lib/ruby/gems/2.5.0/gems/rake-12.3.0/lib/rake/dsl_definition.rb
/usr/local/lib/ruby/gems/2.5.0/gems/rake-12.3.0/lib/rake/early_time.rb
/usr/local/lib/ruby/gems/2.5.0/gems/rake-12.3.0/lib/rake/ext/core.rb
/usr/local/lib/ruby/gems/2.5.0/gems/rake-12.3.0/lib/rake/ext/string.rb
/usr/local/lib/ruby/gems/2.5.0/gems/rake-12.3.0/lib/rake/file_creation_task.rb
/usr/local/lib/ruby/gems/2.5.0/gems/rake-12.3.0/lib/rake/file_list.rb
/usr/local/lib/ruby/gems/2.5.0/gems/rake-12.3.0/lib/rake/file_task.rb
/usr/local/lib/ruby/gems/2.5.0/gems/rake-12.3.0/lib/rake/file_utils.rb
/usr/local/lib/ruby/gems/2.5.0/gems/rake-12.3.0/lib/rake/file_utils_ext.rb
/usr/local/lib/ruby/gems/2.5.0/gems/rake-12.3.0/lib/rake/invocation_chain.rb
/usr/local/lib/ruby/gems/2.5.0/gems/rake-12.3.0/lib/rake/invocation_exception_mixin.rb
/usr/local/lib/ruby/gems/2.5.0/gems/rake-12.3.0/lib/rake/late_time.rb
/usr/local/lib/ruby/gems/2.5.0/gems/rake-12.3.0/lib/rake/linked_list.rb
/usr/local/lib/ruby/gems/2.5.0/gems/rake-12.3.0/lib/rake/loaders/makefile.rb
/usr/local/lib/ruby/gems/2.5.0/gems/rake-12.3.0/lib/rake/multi_task.rb
/usr/local/lib/ruby/gems/2.5.0/gems/rake-12.3.0/lib/rake/name_space.rb
/usr/local/lib/ruby/gems/2.5.0/gems/rake-12.3.0/lib/rake/packagetask.rb
/usr/local/lib/ruby/gems/2.5.0/gems/rake-12.3.0/lib/rake/phony.rb
/usr/local/lib/ruby/gems/2.5.0/gems/rake-12.3.0/lib/rake/private_reader.rb
/usr/local/lib/ruby/gems/2.5.0/gems/rake-12.3.0/lib/rake/promise.rb
/usr/local/lib/ruby/gems/2.5.0/gems/rake-12.3.0/lib/rake/pseudo_status.rb
/usr/local/lib/ruby/gems/2.5.0/gems/rake-12.3.0/lib/rake/rake_module.rb
/usr/local/lib/ruby/gems/2.5.0/gems/rake-12.3.0/lib/rake/rake_test_loader.rb
/usr/local/lib/ruby/gems/2.5.0/gems/rake-12.3.0/lib/rake/rule_recursion_overflow_error.rb
/usr/local/lib/ruby/gems/2.5.0/gems/rake-12.3.0/lib/rake/scope.rb
/usr/local/lib/ruby/gems/2.5.0/gems/rake-12.3.0/lib/rake/task.rb
/usr/local/lib/ruby/gems/2.5.0/gems/rake-12.3.0/lib/rake/task_argument_error.rb
/usr/local/lib/ruby/gems/2.5.0/gems/rake-12.3.0/lib/rake/task_arguments.rb
/usr/local/lib/ruby/gems/2.5.0/gems/rake-12.3.0/lib/rake/task_manager.rb
/usr/local/lib/ruby/gems/2.5.0/gems/rake-12.3.0/lib/rake/tasklib.rb
/usr/local/lib/ruby/gems/2.5.0/gems/rake-12.3.0/lib/rake/testtask.rb
/usr/local/lib/ruby/gems/2.5.0/gems/rake-12.3.0/lib/rake/thread_history_display.rb
/usr/local/lib/ruby/gems/2.5.0/gems/rake-12.3.0/lib/rake/thread_pool.rb
/usr/local/lib/ruby/gems/2.5.0/gems/rake-12.3.0/lib/rake/trace_output.rb
/usr/local/lib/ruby/gems/2.5.0/gems/rake-12.3.0/lib/rake/version.rb
/usr/local/lib/ruby/gems/2.5.0/gems/rake-12.3.0/lib/rake/win32.rb
/usr/local/lib/ruby/gems/2.5.0/gems/rake-12.3.0/lib/rake.rb
/usr/local/lib/ruby/gems/2.5.0/gems/rake-12.3.0/rake.gemspec
/usr/local/lib/ruby/gems/2.5.0/specifications/rake-12.3.0.gemspec
```

この時点でrakeのバージョンは

```sh
~ rake --version
rake, version 0.9.6
```

古いです。
`/usr/local/bin/rake`に古いバージョンのrakeコマンドが残っています。
Homebrewの管理ディレクトリ配下のバイナリへのシンボリック出ない場合は、Homebrewは自動的には置き換えません。
インタラクションに従い手動で置き換えます。

```
rm /usr/local/bin/rake
brew link --overwrite ruby
```

を実行すると

```sh
~ rake --version
rake, version 12.3.0
```

新しいバージョンが使えます。

```sh
~ ls -la /usr/local/bin/rake
lrwxr-xr-x  1 shigerunakajima  admin  31  2 19 14:35 /usr/local/bin/rake -> ../Cellar/ruby/2.5.0_1/bin/rake
```

リンク先がHomebrewの管理ディレクトリ(`/usr/local/Cellar`)に変わっています。

# エラーが起きても無視して次のgemをアンインストール
もうちょっと雑に、エラーが出ないgemだけをアンインストールする方法もあります。

[Uninstalling all gems Ruby 2.0.0 - Stack Overflow](https://stackoverflow.com/questions/15100496/uninstalling-all-gems-ruby-2-0-0)

参考： [Homebrew — macOS 用パッケージマネージャー](https://brew.sh/index_ja.html)

## sh, bash用

```sh
for i in `gem list --no-versions`; do gem uninstall -aIx $i; done
```

## fish-shell用

```sh
for i in (gem list --no-versions); gem uninstall -aIx $i; end
```

# その他の参考
- [Ruby2.0でgemを全てアンインストールする - Qiita](https://qiita.com/yuki_ycino/items/938b8f198336c51dfa38)