Install with Gem on macOS Catalina

5.3k Views Asked by At

I was trying to install Travis.rb as documented by the team. However, the location that gem tried to install to is protected by the OS. So I got an error like this:

% gem install travis
ERROR:  While executing gem ... (Gem::FilePermissionError)
    You don't have write permissions for the /Library/Ruby/Gems/2.6.0 directory.

I tried the following command but did not fully work - installation was okay, but I could not call % travis.

gem install travis --user-install

Configuration: I am using gem shipped with the OS:

% ruby -v  
ruby 2.6.3p62 (2019-04-16 revision 67580) [universal.x86_64-darwin19]
2

There are 2 best solutions below

0
On

The issue from the question seems similar to what I faced. The solution that worked for me is coming from the installation instructions for Jekyll.

There are few important points:

  • macOS comes preinstalled with Ruby, but they don’t recommend using that version to install Jekyll
  • they recommend chruby as a Ruby version manager because it’s the simplest and least likely to cause issues

So the 3 steps to get a working Ruby environment are:

Install chruby and ruby-install with Homebrew:

brew install chruby ruby-install xz

Install latest version of Ruby:

ruby-install ruby

Configure shell:

echo "source $(brew --prefix)/opt/chruby/share/chruby/chruby.sh" >> ~/.zshrc
echo "source $(brew --prefix)/opt/chruby/share/chruby/auto.sh" >> ~/.zshrc
echo "chruby ruby-3.1.2" >> ~/.zshrc # run 'chruby' to see actual version

(for Bash use .bash_profile instead of .zshrc in the last command)

You can check the version of your installed Ruby with ruby -v.

0
On

It is always a good idea to do user installation and avoid system things. Therefore doing...

gem install travis --user-install

... seems to be recommended way.

After that, the executable files are there, but you have to fix you PATH to run by adding your ~/.gem/ruby/2.6.0 folder to the path. See https://guides.rubygems.org/faqs/#user-install on how to do it.