Ruby "gem" command fails with Permission denied @ rb_sysopen

Something seems to be broken with my "gem" command. Here's what I get on running "gem"

/usr/local/lib/site_ruby/2.3.0/rubygems/stub_specification.rb:114:in initialize': Permission denied @ rb_sysopen - /var/lib/gems/2.3.0/specifications/unicode-display_width-1.4.1.gemspec (Errno::EACCES) from /usr/local/lib/site_ruby/2.3.0/rubygems/stub_specification.rb:114:in open' from /usr/local/lib/site_ruby/2.3.0/rubygems/stub_specification.rb:114:in data' from /usr/local/lib/site_ruby/2.3.0/rubygems/stub_specification.rb:204:in valid?' from /usr/local/lib/site_ruby/2.3.0/rubygems/specification.rb:774:in select' from /usr/local/lib/site_ruby/2.3.0/rubygems/specification.rb:774:in gemspec_stubs_in' from /usr/local/lib/site_ruby/2.3.0/rubygems/specification.rb:798:in block in map_stubs' from /usr/local/lib/site_ruby/2.3.0/rubygems/specification.rb:795:ineach' from /usr/local/lib/site_ruby/2.3.0/rubygems/specification.rb:795:in flat_map' from /usr/local/lib/site_ruby/2.3.0/rubygems/specification.rb:795:in map_stubs' from /usr/local/lib/site_ruby/2.3.0/rubygems/specification.rb:788:in installed_stubs' from /usr/local/lib/site_ruby/2.3.0/rubygems/specification.rb:826:in stubs' from /usr/local/lib/site_ruby/2.3.0/rubygems/specification.rb:747:in _all' from /usr/local/lib/site_ruby/2.3.0/rubygems/specification.rb:1131:in latest_specs' from /usr/local/lib/site_ruby/2.3.0/rubygems.rb:546:in find_latest_files' from /usr/local/lib/site_ruby/2.3.0/rubygems.rb:1111:inload_plugins' from /usr/local/lib/site_ruby/2.3.0/rubygems/gem_runner.rb:86:in <top (required)>' from /usr/local/lib/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:54:in require' from /usr/local/lib/site_ruby/2.3.0/rubygems/core_ext/kernel_require.rb:54:in require' from /usr/bin/gem:9:in'

However the command runs fine if I do "sudo gem".

The bigger problem is that I am not able to use "bundle" to install new gems(e.g., fastlane). Note that I am using "bundle update" to install a gem via a Gemfile. And that fails with the exact same stack trace as the one posted above.

How can I fix this issue? Thanks!


  • You get this error when you run without sudo because you're using what some people refer to as "system" ruby on your computer; it's the default ruby that ships with macOS. If you run which ruby it will most likely point to /usr/bin/ruby (if you haven't installed rvm, rbenv, or another ruby version manager.

    By default, system ruby (which includes the system gem and bundler commmands) points to a file location which requires sudo to install to.

    I recommend you don't run sudo to install gems; instead install a ruby version manager such as rvm (the most popular), rbenv (also a popular choice), or asdf (my preferred version manager because it manages more than just ruby, if you want to)

    Once you install a ruby version manager per the instructions you should be able to install a more recent version of ruby (system ruby is 2.3.7, the most recent is 2.6.0). The version manager will also setup sane paths for your gem and bundle commands, pointing to paths where you have write privileges without using sudo

    A ruby version manager is highly recommended for local development because it won't interfere with any system dependencies which might rely on the system ruby version, and a version manager allows you to install multiple versions of ruby and switch between them for different projects.