ERROR: Error Installing Nokogiri: Invalid Gem: Package is Corrupt on Mac

Issue

This error came out when I'm trying to bundle install Rails with rbenv on my MacBook, after retry several times, finally got this solution to install Nokogiri successfully.

-> bundle

Bundler::GemspecError: Could not read gem at /Users/username/.rbenv/versions/2.6.0/lib/ruby/gems/2.6.0/cache/nokogiri-1.10.3.gem. It may be corrupted.
An error occurred while installing nokogiri (1.10.3), and Bundler cannot continue.
Make sure that `gem install nokogiri -v '1.10.3' --source 'https://rubygems.org/'` succeeds before bundling.
->  gem install nokogiri -v '1.10.3' --source 'https://rubygems.org/'

ERROR:  Error installing nokogiri:
    invalid gem: package is corrupt, exception while verifying: undefined method `size' for nil:NilClass (NoMethodError) in /Users/username/.rbenv/versions/2.6.0/lib/ruby/gems/2.6.0/cache/nokogiri-1.10.3.gem

error-installing-nokogiri-on-mac

Solution

brew install libiconv

rm /Users/username/.rbenv/versions/2.6.0/lib/ruby/gems/2.6.0/cache/nokogiri-1.10.3.gem

gem install nokogiri -v '1.10.3' --source 'https://rubygems.org/'

bundle