How do I install ruby gems on Mac
Asked Answered
M

6

85

How do I install RubyGems on my Mac?

I tried to run $ gem install rubygems-update with no luck. It returns:

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

Any help would be great. Thanks

Musculature answered 8/9, 2016 at 1:42 Comment(4)
though this command may succeed if you use sudo, in the long run it'll be easier if you use a version manager like rbenv or rvm.Jochebed
You can only run gem from command line if you have RubyGems installed. So you may have to install manually.Cassidycassie
For MacOS Majave, here's a good guide: gorails.com/setup/osx/10.14-mojaveImperceptible
I found this helpfull #18600389Outofdoor
A
136

I would highly suggest using a package manager and a Ruby Environment Manager.

On Mac, you can run:

brew update
brew install ruby

# If you use bash
echo 'export PATH=/usr/local/Cellar/ruby/2.4.1_1/bin:$PATH' >> ~/.bash_profile 

# If you use ZSH:
echo 'export PATH=/usr/local/Cellar/ruby/2.4.1_1/bin:$PATH' >> ~/.zprofile

However, I suggest using an Environment Manager for Ruby.

You have rbenv and RVM. IMO go for rbenv:

brew install rbenv ruby-build

# bash
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile
echo 'eval "$(rbenv init -)"' >> ~/.bash_profile  

# zsh
echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.zprofile
echo 'eval "$(rbenv init -)"' >> ~/.zprofile  

# list all available versions:
rbenv install -l

# install a Ruby version:
rbenv install 2.4.1

# set ruby version for a specific dir
rbenv local 2.4.1

# set ruby version globally
rbenv global 2.4.1

rbenv rehash
gem update --system
Aharon answered 8/4, 2017 at 11:42 Comment(5)
Good point +1. That is why I am asking questions, I got the last command working by running it in a new shell (without running it as sudo). I am now trying to get my first package oauth installed with the newest gems 2.4.1, getting some odd LoadError here.Vortumnus
the first part of this answer works fine if you also immediately run the export command in the current shell. I have no interest in installing rbenv or rvm; I just need access to a public gem temporarily. Thanks!Alboran
On OSX, don't forget to log out of your OSX session and then log back in for the changes to your bash profile to take effect. So you don't have to search why this doesn't work and endup here.Talos
I added this to my $PATH so I don't wonder what broke when ruby gets updated: /usr/local/Cellar/ruby/$(ls -1rt /usr/local/Cellar/ruby/ | tail -1)/binMonecious
Everybody is telling me to install rbenv. I don't care about Ruby, I can do that. But does it affect how I use Ruby and run the commands in the gem (which are what I actually care about)?Register
T
19

One more variant is to use brew-gem.

https://formulae.brew.sh/formula/brew-gem

https://github.com/sportngin/brew-gem

Just copy/paste from the documentation:

brew install brew-gem

Usage

brew gem install heroku

To install a specific version: brew gem install heroku 3.8.3

To install using a brew installed ruby(/usr/local/bin/ruby): brew gem install heroku --homebrew-ruby

And with a specific version: brew gem install heroku 3.8.3 --homebrew-ruby

To upgrade: brew gem upgrade heroku

To uninstall: brew gem uninstall heroku

To check information: brew gem info heroku

Note: Installed gems are listed in brew list with prefix of gem-, like gem-heroku.

Tribunal answered 27/2, 2019 at 13:10 Comment(2)
Worked for me - no sudo required!Tarbes
As I only require Ruby to run some tooling, I prefer this approach.Manzanares
C
12

After installing cocoapods as Tulon suggested the thing which worked for me was passing the --user-install flag to the gem install command e.g

gem install rubygems-update --user-install

from https://guides.cocoapods.org/using/getting-started.html#sudo-less-installation

Comehither answered 19/6, 2019 at 8:18 Comment(0)
C
4

You may have to install manually in which case (as per the official website):

  1. Go to the official website and download the zip file
  2. Unzip the file in a directory.
  3. Then go to the directory in your terminal and run: ruby setup.rb
Cassidycassie answered 8/9, 2016 at 2:16 Comment(1)
Looks like this answer is not the best route; but I will leave it up on the off chance it helps someone.Cassidycassie
S
3

It is showing that you don't have permission to write something in that directory. Instead, use this directory :

sudo gem install -n /usr/local/bin cocoapods

It works perfectly for me.

(I am using macOS Sierra, version 10.12.6)

Sarmentose answered 29/9, 2017 at 20:31 Comment(1)
I would highly suggest against using the system ruby and sudo.Aharon
C
0

In the case when you see an older version of ruby in the directory that it's trying to access and you have a fresh install you can remove the ruby version folder in .gem directory in your home folder. For me it was ~/.gem/ruby/2.4.1/. Gem will automatically detect the new version (that you can install both using brew or rbenv) and act accordingly.

This will remove all gems hovewer so keep that in mind.

Calciferous answered 28/2 at 13:42 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.