Related
I am fresh to Linux Ubuntu and I have messed up my Ruby on Rails install.
Therefore, I uninstalled Ruby and attempted to follow the rbenv installation.
However, I am getting this stack trace:
martin#martyux:~$ curl -fsSL https://github.com/rbenv/rbenv-installer/raw/master/bin/rbenv-doctor | bash
Checking for `rbenv' in PATH: multiple
You seem to have multiple rbenv installs in the following locations.
Please pick just one installation and remove the others.
/home/martin/.rbenv/bin/rbenv
/usr/bin/rbenv
/bin/rbenv
Checking for rbenv shims in PATH: not found
The directory `/home/martin/.rbenv/shims' must be present in PATH for rbenv to work.
Please run `rbenv init' and follow the instructions.
Checking `rbenv install' support: /home/martin/.rbenv/plugins/ruby-build/bin/rbenv-install (ruby-build 20210119)
Counting installed Ruby versions: none
There aren't any Ruby versions installed under `/home/martin/.rbenv/versions'.
You can install Ruby versions like so: rbenv install 2.2.4
Checking RubyGems settings: OK
Auditing installed plugins: OK
I am assuming that there's instances of rbenv I attempted to install earlier and somehow is still linked to $PATH. I didn't find anyone with this exact issue and I am not competent at navigating the Unix shell yet, so I would appreciate some thorough help.
I don't know how you got rbenv to end up in /bin/rbenv and usr/bin/rbenv, but for what I can see from their installation guide you only need ~/.rbenv/bin/rbenv.
Assuming you use bash, I think these are all added to your $PATH in ~/.bashrc. You should find something like:
export PATH="$PATH:/bin/rbenv"
And
export PATH="$PATH:/usr/bin/rbenv"
You can remove those lines. To clean up properly you can also remove the rbenv installs they refer to as well.
Lastly, as the error message says, you want to run rbenv init after doing this to create the shims folder.
I did not have export PATH="$PATH:/bin/rbenv" and export PATH="$PATH:/usr/bin/rbenv" in my ~/.bashrc
I just ran
sudo apt-get purge ruby
sudo apt-get purge rbenv
and then
curl -fsSL https://github.com/rbenv/rbenv-installer/raw/main/bin/rbenv-doctor | bash
showed only one path.
If you set up a ~/.gemrc with gem: --user-install in it, then the gemrc will configure rbenv to install gems in ~/.gem, which is not how rbenv is expecting it - rbenv wraps the gem call via a shim, so you can try rename / remove ~/.gemrc and then try run gem install <whatever> again and it should all work
below is what I need to do.
To run the specs, you'll need to install RSpec. First, run gem install bundler in the root directory of your project. Then, run bundle install. To run a single spec file, run a command like this: bundle exec rspec spec/00_hello_spec.rb. To run all of the specs at once, run bundle exec rspec.
So, I typed gem install bundler in Terminal, and got the error:
You don't have write permissions for the /Library/Ruby/Gems/2.3.0 directory.
and this was in the project file in atom
source "https://rubygems.org"
gem "rspec", "~> 3.2.0"
My question is:
It seems like terminal is giving me the response because I'm not supposed to change anything on ruby, and I need to bundle install inside of atom? Could anyone tell me how to use atom or run anything in atom?
Update:
I now have a paid script that will set up a proper Ruby environment for you with a single command!
I also keep my free step-by-step guide for installing Ruby on a Mac up to date (more often than I update this answer).
You are correct that macOS won't let you change anything with the Ruby version that comes installed with your Mac. However, it's possible to install gems like bundler using a separate version of Ruby that doesn't interfere with the one provided by Apple.
Using sudo to install gems, or changing permissions of system files and directories is strongly discouraged, even if you know what you are doing. Can we please stop providing this bad advice? I wrote a detailed article that shows why you should never use sudo to install gems.
The solution involves two main steps:
Install a separate version of Ruby that does not interfere with the one that came with your Mac.
Update your PATH such that the location of the new Ruby version is first in the PATH. Some tools do this automatically for you. If you're not familiar with the PATH and how it works, it's one of the basics that you should learn, and you'll understand why you sometimes get "command not found" errors and how to fix them.
There are several ways to install Ruby on a Mac. The best way that I recommend, and that I wish was more prevalent in the various installation instructions out there, is to use an automated script like Ruby on Mac that will set up a proper Ruby environment for you.
The main reason is that it saves each person a ton of time. Time is our most limited and valuable resource. Why make people do things manually when they can be automated with a perfect result every time?
Another reason is that it drastically reduces the chance of human error, or errors due to incomplete instructions.
If you want to do things manually, keep on reading. First, you will want to install Homebrew, which installs the prerequisite command line tools, and makes it easy to install other necessary tools.
Then, the two easiest ways to install a separate version of Ruby are:
If you would like the flexibility of easily switching between many Ruby versions [RECOMMENDED]
Choose one of these four options:
chruby and ruby-install - my personal recommendations and the ones that are automatically installed by the Ruby on Mac script. These can be installed with Homebrew:
brew install chruby ruby-install
rbenv - can be installed with Homebrew
RVM
asdf
If you chose chruby and ruby-install, you can then install the latest Ruby like this:
ruby-install ruby
Once you've installed everything and configured your .zshrc or .bash_profile according to the instructions from the tools above, quit and restart Terminal, then switch to the version of Ruby that you want. In the case of chruby, it would be something like this:
chruby 3.1.3
Whether you need to configure .zshrc or .bash_profile depends on which shell you're using.
If you know for sure you don't need more than one version of Ruby at the same time (besides the one that came with macOS) [NOT RECOMMENDED]
Even if you think you won't need another version now, you will eventually and you won't be able to easily switch. This will cause confusion and headaches, which is why I don't recommend installing and managing Ruby with Homebrew.
If you choose to use Homebrew to install Ruby despite my warnings, you'll be on your own if you run into any issues.
Install ruby with Homebrew:
brew install ruby
Then update your PATH by running this command:
echo 'export PATH="/usr/local/opt/ruby/bin:/usr/local/lib/ruby/gems/3.1.0/bin:$PATH"' >> ~/.zshrc
The 3.1.0 in the command above assumes Homebrew installed a Ruby version that starts with 3.1. If it installed a different version, replace 3.1 with the first two digits of your Ruby version.
If you're on an M1/M2 Mac, replace /usr/local with /opt/homebrew
Then "refresh" your shell for these changes to take effect:
source ~/.zshrc
Or you can open a new terminal tab, or quit and restart Terminal.
Replace .zshrc with .bash_profile if you are using Bash. If you're not sure, read my guide to find out which shell you're using.
To check that you're now using the non-system version of Ruby, you can run the following commands:
which ruby
It should not be /usr/bin/ruby
ruby -v
It should be 3.1.3 or later.
Once you have this new version of Ruby installed, you can now install bundler (or any other gem):
gem install bundler
Worked for me using the parameter --user-install running following command:
gem install name_of_gem --user-install
Edit
There was one gem I still could not install (it required the Ruby.h headers of the Ruby development kit or something), then I tried the different version managers, but somehow that still did not really work as it was stated in the documentations how to just install and switch (it did just not switch the versions).
Then I removed all the installed version managers and installed afterwards with brew install ruby the latest version and did set the PATH variable, too. (It will be mentioned after the installation of ruby from brew), which worked.
If you don't want to run sudo then install ruby using homebrew
brew install ruby
export GEM_HOME="$HOME/.gem"
gem install rails
You may want to add export GEM_HOME="$HOME/.gem" to your ~/.bash_profile or .zshrc if you're using zsh
Note: RubyGems keeps old versions of gems, so feel free to do some cleaning after updating:
gem cleanup
Just export GEM_HOME:
export GEM_HOME="$HOME/.gem"
And then try:
gem install cocoapods
As #idleberg mentions, on Mac OS, it is best to install rbenv to avoid permissions errors when using manually installed ruby.
Installation
$ brew update
$ brew install rbenv
Add the following in .bashrc file:
eval "$(rbenv init -)"
Now, we can look at the list of ruby versions available for install
$ rbenv install -l
Install version 2.3.8 for example
$ rbenv install 2.3.8
Now we can use this ruby version globally
$ rbenv global 2.3.8
Finally run
$ rbenv rehash
$ which ruby
/Users/myuser/.rbenv/shims/ruby
$ ruby -v
ruby 2.3.7p456 (2018-03-28 revision 63024) [x86_64-darwin17]
Go for it
Now install bundler
$ gem install bundler
All done!
It's generally recommended to use a version manager like rbenv or rvm. Otherwise, installed Gems will be available as root for other users.
If you know what you're doing, you can use sudo gem install.
Try this:
sudo gem install cocoapods --user-install
Worked for me
PLEASE USE SUDO WITH CARE!!!!!! ONLY IF YOU KNOW WHAT YOU ARE DOING!!!!!!!!!!!!
I have faced same issue after install macOS Catalina. I had try below command and its working.
sudo gem update
Run this
$ rbenv init
# Load rbenv automatically by appending
# the following to ~/.zshrc:
eval "$(rbenv init -)"
Follow instructions, (in my case add to ~/.zshrc) ;)
Also important: Changes only take effect if you reboot your console. Two options
Enter source <modified file>
close and open again
Try 1 or 2
1 - $ gem install cocoapods
2 - $ sudo gem install cocoapods
if it doesn't work, then export GEM_HOME:
export GEM_HOME="$HOME/.gem"
And try again:
gem install cocoapods
Remember the oficial doc says you can use sudo (https://guides.cocoapods.org/using/getting-started.html#getting-started).
To fix this, I ran
brew reinstall ruby
which showed me this message
==> Caveats
==> ruby
By default, binaries installed by gem will be placed into:
/opt/homebrew/lib/ruby/gems/3.1.0/bin
You may want to add this to your PATH.
ruby is keg-only, which means it was not symlinked into /opt/homebrew,
because macOS already provides this software and installing another version in
parallel can cause all kinds of trouble.
If you need to have ruby first in your PATH, run:
echo 'export PATH="/opt/homebrew/opt/ruby/bin:$PATH"' >> ~/.profile
So I added these two lines to my ~/.bashrc file
export PATH="/opt/homebrew/opt/ruby/bin:$PATH"
export PATH="/opt/homebrew/lib/ruby/gems/3.1.0/bin:$PATH"
Then I opened a new Terminal.app window and ran my gem install command again and it worked.
Tested on M1 MacBook Air (assuming Homebrew installed)
Following to the top answer, we can run:
brew install chruby ruby-install
To install the latest stable ruby:
ruby-install ruby
Then get the version number by running:
chruby
In your ~/.zshrc file:
export PATH=/opt/homebrew/bin:$PATH
source /opt/homebrew/opt/chruby/share/chruby/chruby.sh
source /opt/homebrew/opt/chruby/share/chruby/auto.sh
chruby 3.1.2
The "3.1.2" is the output I got when running chruby. Make sure you add that one line at the end.
Remember to restart the terminal each time you install a new gem.
If you have installed ruby separately and installed ruby using rbenv/rvm you budler might point to different versions.
try
gem env home
and
ruby -v
both should point to same version.check you have installed ruby using rbenv/rvm, If so delete the ruby version you installed separately.
In order for gem to work, you must invoke rbenv,
rbenv shell <ruby version>
and
rbenv global <ruby version>
I am not sure how RVM works.
Let me know if this works.
After install rbenv ,I also have this problem , add this line in my .bashrc :
eval "$(rbenv init -)"
solved my problem.
You don't have write permissions for the /Library/Ruby/Gems/2.3.0 directory.
In my case, the issue was related to ruby access some how the ruby path was messed up in my system.
The below steps helped me resolve the problem
Open the terminal
Install ruby using homebrew
[for fresh install] brew install ruby
[for reinstalling] brew reinstall ruby
Check the path of ruby using the below command
which ruby
It should be installed in the below path
/usr/bin/ruby
To change the ruby path to the user path
To check which shell is used by your system
echo $0
-zsh
For zshrc
echo 'export PATH="/usr/local/opt/ruby/bin:$PATH"' >>~/.zshrc
For bash
echo 'export PATH="/usr/local/opt/ruby/bin:$PATH"' >>~/~/.bashrc
Quit and relaunch the terminal
After changing the path with step 5
Check for the path of the ruby again (execute step 3 - please make sure the path displays as given below)
/usr/local/opt/ruby/bin/ruby
[if you don't quit and launch the terminal, step 4 path will be shown]
This step may not be applicable to everyone can skip step 10 & 11, if you have the correct Cocoapods version installed
Check the version of the pod installed
pod --version
Uninstall the specific version of Cocoapods using the below command
In case the version installed is 1.11.0
gem uninstall cocoapods -v 1.11.0
Install the Cocoapods of the specific version
gem install cocoapods -v 1.11.0
Change the path to the Project directory cd {path of the project directory}
Install the bundler in the project directory
bundle install
Execute pod install
pod install
I deleted those directories by using the below command
sudo rm -rf \
/Library/Ruby/Gems/2.6.0/{build_info,cache,doc,extensions,gems} \
/Library/Ruby/Gems/2.6.0/specifications/*.gemspec \
/Library/Ruby/Site
then installed cocoa pods using sudo gem install cocoapods
and it worked for me. Thanks
TL;DR
In several occasions, I've solved this kind of errors by just closing my terminal session and opening a new one before retrying the failing command.
Long explanation
In some SOs (such as MacOS) there is already a pre-installed, system-wide version of ruby. If you are using a version manager, such as rbenv or asdf, they work by playing with the environment of your current session so that the relevant commands point to the binaries installed by the version manager.
When installing a new binary, the version manager installs it in a special location, usually somewhere under the user's home directory. It then configures everything in your PATH so that you get the freshly installed binaries when you issue a command, instead of the ones that came with your system. However, if you don't restart the session (there are other ways of getting your environment updated, but that's the easiest one) you don't get the new configuration and you will be using the original installation.
Had the same error because I forgot to run the following after installing ruby:
source ~/.zshrc - or other ~/...rc file depending on your terminal
rbenv global 2.6.3 helped me solve this problem.
A different installation of ruby should be used. I use rbenv for that purpose.
# install your version of ruby
$ rbenv install 2.0.0-p247
# modify .ruby_version on current directory
$ rbenv local 2.0.0-p247
# proceed installing gems
$ gem install bundler
Disclamer: I am not a ruby person. This worked for me and if you are a ruby expert and see things to change in this answer, please, go ahead or comment!
I was using the below command to install fastlane but didn't worked
gem install fastlane -NV
So using sudo to install gems worked for me and it would be like
sudo gem install fastlane -NV
I try it, and work to me
export PATH=/opt/homebrew/opt/ruby/bin:/opt/homebrew/lib/ruby/gems/3.0.0/bin:$PATH
export LDFLAGS="-L/opt/homebrew/opt/ruby/lib"
export CPPFLAGS="-I/opt/homebrew/opt/ruby/include"
gem install ffi
For latest OS versions
First run sudo xcode-select --switch /
sudo gem install cocoapods --user-install
this did it for me
for Mac OS 12 and above also for 13
Xcode 14 and above
first you call
export GEM_HOME="$HOME/.gem"
after that
gem install cocoapods
home its help you ☺️
Solution for Mac
Install/update RVM with last ruby version
\curl -sSL https://get.rvm.io | bash -s stable
Install bundler
gem install bundler
after this two commands (sudo) gem install .... started to work
Solution for MAC. run the command
sudo gem update
then type your Mac password when prompted
After trying the previous approaches, this worked for me on Big Sur:
sudo gem install -n /usr/local/bin cocoapods
Install homebrew by passing this into your terminal
/bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
Install cocoapods using brew
brew install cocoapods
After install ruby with rbenv you also need to set global ruby. For that you can do like, rbenv global 3.2.1 then install bundler with gem install bundler. It will work.
This worked for me on Mac
sudo chown -R $(whoami) $(brew --prefix)/*
I messed up my configuration. I am running Ubuntu 14.04 and had some issues when I tried to move from a project to an other. I tried to uninstall and reinstall rbenv and then installed Ruby 2.1.7 following this guide: https://gorails.com/setup/ubuntu/14.04.
Now, when I run gem install bundler nothing happens and I can't install any gems. When I type bundler or bundler -v I see this:
/usr/local/bin/bundler: /usr/bin/ruby1.9.1: bad interpreter: No such file or directory
I spent many hours trying to solve this but I really don't know what to do.
Find the bundle executable (in a non project directory - default system ruby) and delete it:
which bundle
rm <path_obtained_above>
Now go to your project directory, or switch to your ruby version and install bundler:
gem install bundler
Then execute rbenv rehash
and now try executing bundle install and it should work. You will need to do this once for each ruby version installed.
The fact that gem is being executed by your /usr/local/bin means that rbenv isn't in your $PATH correctly.
Step 1:
Please delete any references you have of rbenv in the following areas:
~/.profile
~/.bashrc
~/.bash_profile
~/.gemrc
Step 2:
Reinstall rbenv using these instructions. It won't override, it will simply update changes to your ~/.bash_profile. Once reinstalled close your terminal and reopen. Everything should work correctly at that time.
Step 3:
Verify that everything was installed correctly by executing cat ~/.bash_profile. You should see the following. If you do, you're good to go.
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"
Many cudos to #theTinMan for recently helping me sort through my rbenv installation.
I'm trying to install a gem using gem install mygem or update RubyGems using gem update --system, and it fails with this error:
ERROR: While executing gem ... (Gem::FilePermissionError)
You don't have write permissions for the /Library/Ruby/Gems/2.0.0 directory.
Does anyone have an idea how to solve this?
Try adding --user-install instead of using sudo:
gem install mygem --user-install
You don't have write permissions into the /Library/Ruby/Gems/1.8 directory.
means exactly that, you don't have permission to write there.
That is the version of Ruby installed by Apple, for their own use. While it's OK to make minor modifications to that if you know what you're doing, because you are not sure about the permissions problem, I'd say it's not a good idea to continue along that track.
Instead, I'll strongly suggest you look into using either rbenv or RVM to manage a separate Ruby, installed into a sandbox in your home directory, that you can modify/fold/spindle/change without worrying about messing up the system Ruby.
Between the two, I use rbenv, though I used RVM a lot in the past. rbenv takes a more "hands-off" approach to managing your Ruby installation. RVM has a lot of features and is very powerful, but, as a result is more intrusive. In either case, READ the installation documentation for them a couple times before starting to install whichever you pick.
You really should be using a Ruby version manager.
Using one properly would prevent and can resolve your permission problem when executing a gem update command.
I recommend rbenv.
However, even when you use a Ruby version manager, you may still get that same error message.
If you do, and you are using rbenv, just verify that the ~/.rbenv/shims directory is before the path for the system Ruby.
$ echo $PATH will show you the order of your load path.
If you find that your shims directory comes after your system Ruby bin directory, then edit your ~/.bashrc file and put this as your last export PATH command: export PATH=$HOME/.rbenv/shims:$PATH
$ ruby -v shows you what version of Ruby you are using
This shows that I'm currently using the system version of Ruby (usually not good)
$ ruby -v
ruby 1.8.7 (2012-02-08 patchlevel 358) [universal-darwin12.0]
$ rbenv global 1.9.3-p448 switches me to a newer, pre-installed version (see references below).
This shows that I'm using a newer version of Ruby (that likely won't cause the Gem::FilePermissionError)
$ ruby -v
ruby 1.9.3p448 (2013-06-27 revision 41675) [x86_64-darwin12.4.0]
You typically should not need to preface a gem command with sudo. If you feel the need to do so, something is probably misconfigured.
For details about rbenv see the following:
https://github.com/sstephenson/rbenv
http://robots.thoughtbot.com/post/47273164981/using-rbenv-to-manage-rubies-and-gems
Why don't you do:
sudo gem update --system
This will fix the issue on MacOS Mojave and Catalina in a clean way:
brew install ruby
Then set GEM_HOME to your user directory. On the terminal:
Bash:
echo '# Install Ruby Gems to ~/gems' >> ~/.bashrc
echo 'export GEM_HOME=$HOME/gems' >> ~/.bashrc
echo 'export PATH=$HOME/gems/bin:$PATH' >> ~/.bashrc
source ~/.bashrc
OR if on Zsh:
echo '# Install Ruby Gems to ~/gems' >> ~/.zshrc
echo 'export GEM_HOME=$HOME/gems' >> ~/.zshrc
echo 'export PATH=$HOME/gems/bin:$PATH' >> ~/.zshrc
source ~/.zshrc
For me the problem was due to using rbenv and forgetting to set the proper version globally.
So I had to set it with rbenv global xxx
In my case I installed 2.0.0-p247 so I had to issue the command:
rbenv global 2.0.0-p247
rbenv rehash
Then all was working fine.
You need to correct your paths.
To determine if this fix will work, run the following:
which gem
This should output a directory you do not have permissions to:
/usr/bin/gem
To fix this perform the following steps:
Determine the path you need to copy to your profile:
rbenv init -
The first line of the output is the line you need to copy over to your profile:
export PATH="/Users/justin/.rbenv/shims:${PATH}" #path that needs to be copied
source "/usr/local/Cellar/rbenv/0.4.0/libexec/../completions/rbenv.zsh"
rbenv rehash 2>/dev/null
rbenv() {
typeset command
command="$1"
if [ "$#" -gt 0 ]; then
shift
fi
case "$command" in
rehash|shell)
eval `rbenv "sh-$command" "$#"`;;
*)
command rbenv "$command" "$#";;
esac
}
Copy the path to your profile and save it.
Reload your profile (source ~/.zshenv for me).
Run rbenv rehash.
Now when you run which gem you should get a local path that you have permissions to:
/Users/justin/.rbenv/shims/gem
This worked for me. Plus, if you installed gems as root before, it fixes that problem by changing ownership back to you (better security-wise).
sudo chown -R `whoami` /Library/Ruby/Gems
Try nathanwhy's answer before using my original answer below. His recommendation of --user-install should accomplish the same purpose without having to muck with your .bash_profile or determine your Ruby version.
If you are not concerned about a specific ruby version, you can skip the heavy-lift Ruby environment manager options, and just add these lines to ~/.bash_profile:
export GEM_HOME="$HOME/.gem/ruby/2.0.0"
export GEM_PATH="$HOME/.gem/ruby/2.0.0"
The path is stolen from the original output of gem env:
RubyGems Environment:
- RUBYGEMS VERSION: 2.0.14
- RUBY VERSION: 2.0.0
- INSTALLATION DIRECTORY: /Library/Ruby/Gems/2.0.0
- RUBY EXECUTABLE: /System/Library/.../2.0/usr/bin/ruby
- EXECUTABLE DIRECTORY: /usr/bin
- RUBYGEMS PLATFORMS:
- ruby
- universal-darwin-14
- GEM PATHS:
- /Library/Ruby/Gems/2.0.0
- /Users/mylogin/.gem/ruby/2.0.0 # <---- This line, right here. -----
- /System/Library/.../usr/lib/ruby/gems/2.0.0
...
No sudoing is required, and you can use the already-installed Ruby, courtesy of Apple.
sudo gem update --system
sudo gem install (gemfile)
There are two routes: Use either rbenv or RVM. There are recipes for both below. Before you do, you probably want to turn off the installation of local documents for gems.
echo "gem: --no-ri --no-rdoc" >> ~/.gemrc
Then:
install rbenv
install ruby-build
run:
rbenv install 2.1.2 (or whatever version you prefer)
rbenv global 2.1.2
gem update --system
This installs an up-to-date version of the gem system in your local directories. That means you don't interfere with the system configuration. If you're asking this question, you shouldn't be messing with system security, and you'll spend longer understanding what issues you may run into, than just having an easy way to avoid the problem you started with. Learn InfoSec later, when you know more about the operating system and programming.
For an alternative use 'RVM' instead: To install rvm run:
rvm install 2.1.2
rvm use 2.1.2
gem update --system
This has the same result, you end up with a local Ruby and Gem system that doesn't interfere with the system versions. There is no need for Homebrew, or over-riding system libs, etc.
I found this how-to for sudoless gem:
brew install rbenv ruby-build
sudo gem update --system
add exports to .bashrc:
export RBENV_ROOT="$(brew --prefix rbenv)"
export GEM_HOME="$(brew --prefix)/opt/gems"
export GEM_PATH="$(brew --prefix)/opt/gems"
And finally add this to your ~/.gemrc:
gem: -n/usr/local/bin
gem update --system
sudo chown -R $USER /Library/Ruby/Gems/
I needed to do a rbenv rehash so it would point to my local Gem library.
It looks like you've got your gem manager pointing to the System Library, so, instead of messing with permissions, do the equivalent of "rehash" for your manager to get things pointing locally.
I had formatted my Mac and many suggested solutions did not work for me.
What worked for me are these commands in the correct order:
Install Homebrew:
/usr/bin/ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"
Install Ruby:
brew install ruby
Install Compass:
sudo gem install compass
Older and wiser
Don't do what I say here, just know to be wary any time you use sudo. You probably want to use something like rbenv to isolate whatever work you're doing.
a way
learn about chown
I don't know if you like the command line, but this will make working on any project with any tool that installs packages to your system a breeze.
chown as far as I can tell, stands for change ownership.
The reason I came looking for this answer is because gem install threw this error at me today:
ERROR: While executing gem ... (Gem::FilePermissionError)
You don't have write permissions into the /var/lib/gems/1.9.1 directory.
This is a perfect opportunity to use chown. You see Ruby has given us the directory it needs access to, and it seems like it's a directory it will use pretty often.
In this case, there are only three things one needs to know to solve the problem, but chown is much more powerful, and grants you a lot more flexibility than I will demonstrate now. Please refer to the source at the bottom for more information.
The Two Things
Username
Directory
If you're in a shell finding the username is easy. Just look at the prompt. Mine looks like:
breadly#breadly-desktop:~\Desktop
The current user is just the name before the #. We know the directory from the error messages, but you have two choices. You can either limit your permission to the current version by using ../gems/1.9.1, or give yourself write permission for gems of all version by using ../gems.
The command to actually change ownership would look like this.
chown -R $(whoami) /absolute/path/to/directory
The -R is known as a flag and the -R flag typically tells a command to do something recursively, or in other words perform the command on every thing that is contained in the directory, and all the things contained in the directories contained within, and so on till there isn't anything else.
Work for me:
sudo gem uninstall cocoapods
sudo gem install cocoapods
Install rbenv by brew install rbenv;
Then put eval "$(rbenv init -)" at the end of ~/.bash_profile (or ~/.zshrc
of MacOS);
Open a new terminal and run gem install *** will work!
Check to see if your Ruby version is right. If not, change it.
This works for me:
$ rbenv global 1.9.3-p547
$ gem update --system
A 2021 solution (using rvm):
If you type which ruby in terminal, and it shows /usr/bin/ruby, you can try this solution.
install rvm
curl -L https://get.rvm.io | bash -s stable
install ruby using rvm
rvm install "ruby-3.0.0"
use your installed version of ruby
rvm use ruby-3.0.0
type which ruby again, which will show /Users/mac_user_name/.rvm/rubies/ruby-3.0.0/bin/ruby.
It's a new path to use ruby.
Tested on MacOS Mojave WITH SUCCESS:
Uninstall all your old ruby versions (let's say you have 2.00 and 2.3.0):
$ rvm uninstall 2.0.0
$ rvm uninstall 2.3.0
Install brand new ruby version:
$ brew install ruby
Set a default alias to your version:
$ rvm alias create default ruby
Reboot your system because this is the safest way your computer loads the new ruby version, recently installed.
AFTER you done above procedure, you can successfully run any gem command.
As pointed out by bobbdelsol, rehash worked for me :
==> which ruby
/usr/bin/ruby
==> rbenv install 1.9.3-p551
Downloading ruby-1.9.3-p551.tar.bz2...
-> https://cache.ruby-lang.org/pub/ruby/1.9/ruby-1.9.3-p551.tar.bz2
Installing ruby-1.9.3-p551...
Installed ruby-1.9.3-p551 to /Users/username/.rbenv/versions/1.9.3-p551
==> which ruby
/Users/username/.rbenv/shims/ruby
==> which gem
/Users/username/.rbenv/shims/gem
==> gem install compass
ERROR: While executing gem ... (Gem::FilePermissionError)
You don't have write permissions for the /Library/Ruby/Gems/2.0.0 directory.
==> ruby -v
ruby 2.0.0p648 (2015-12-16 revision 53162) [universal.x86_64-darwin15]
==> rbenv global 1.9.3-p551
==> ruby -v
ruby 2.0.0p648 (2015-12-16 revision 53162) [universal.x86_64-darwin15]
==> rbenv global 1.9.3-p551
==> rbenv rehash
==> ruby -v
ruby 1.9.3p551 (2014-11-13 revision 48407) [x86_64-darwin15.4.0]
==> gem install compass
Fetching: sass-3.4.22.gem (100%)
Fetching: multi_json-1.11.3.gem (100%)
Fetching: compass-core-1.0.3.gem (100%)
Fetching: compass-import-once-1.0.5.gem (100%)
Fetching: chunky_png-1.3.5.gem (100%)
Fetching: rb-fsevent-0.9.7.gem (100%)
Fetching: ffi-1.9.10.gem (100%)
Building native extensions. This could take a while...
Fetching: rb-inotify-0.9.7.gem (100%)
Fetching: compass-1.0.3.gem (100%)
Compass is charityware. If you love it, please donate on our behalf at http://umdf.org/compass Thanks!
Successfully installed sass-3.4.22
Successfully installed multi_json-1.11.3
Successfully installed compass-core-1.0.3
Successfully installed compass-import-once-1.0.5
Successfully installed chunky_png-1.3.5
Successfully installed rb-fsevent-0.9.7
Successfully installed ffi-1.9.10
Successfully installed rb-inotify-0.9.7
Successfully installed compass-1.0.3
9 gems installed
Installing ri documentation for sass-3.4.22...
Installing ri documentation for multi_json-1.11.3...
Installing ri documentation for compass-core-1.0.3...
Installing ri documentation for compass-import-once-1.0.5...
Installing ri documentation for chunky_png-1.3.5...
Installing ri documentation for rb-fsevent-0.9.7...
Installing ri documentation for ffi-1.9.10...
Installing ri documentation for rb-inotify-0.9.7...
Installing ri documentation for compass-1.0.3...
Installing RDoc documentation for sass-3.4.22...
Installing RDoc documentation for multi_json-1.11.3...
Installing RDoc documentation for compass-core-1.0.3...
Installing RDoc documentation for compass-import-once-1.0.5...
Installing RDoc documentation for chunky_png-1.3.5...
Installing RDoc documentation for rb-fsevent-0.9.7...
Installing RDoc documentation for ffi-1.9.10...
Installing RDoc documentation for rb-inotify-0.9.7...
Installing RDoc documentation for compass-1.0.3...
You can change GEM_HOME. You have also under your home directory a gem folder to check it use
$ gem env
result is as follows. Unrelated parts are omitted.
...
- GEM PATHS:
- /Users/xxx/.gem/ruby/2.6.0
- /Library/Ruby/Gems/2.6.0
- /System/Library/Frameworks/Ruby.framework/Versions/2.6/usr/lib/ruby/gems/2.6.0
...
You can use your /Users/xxx/.gem/ruby/2.6.0 folder.
vim ~/.bash_profile
add the following line
export GEM_HOME=~/.gem/ruby/2.6.0/
After that you can use
source ~/.bash_profile
The issue for me was that I switched from zshell to bash earlier and was not logged in:
/bin/bash --login
Although I had rvm installed, it was not able to switch to my newly rvm-installed ruby version and was still trying to use the default Mac-installed ruby binary. Hence my confusion (user error!!!) and the continued permissions issues...
Steps to resolve issue:
Step 1: check and install cocoapods with home brew
brew install cocoapods
Step 2: This is most important, to resolve all the issues, installing
rvm package with stable ruby version, without this you will be
accessing ruby version inside System folder where you don't have
permission.
\curl -sSL https://get.rvm.io | bash -s stable --ruby
Step 3: Add rvm command path to .zshrc file to access it globally
export PATH="$PATH:$HOME/.rvm/scripts/rvm"
Step 4: check if rvm is running properly inside command line globally
rvm --version
Step 5: Now you can install cocoapods package inside user bin as this will
access ruby file from rvm folder, without any permission needed.
sudo gem install -n /usr/local/bin cocoapods
Step 6: Now you can check pod also by using below command
pod install
ERROR: While executing gem ... (Gem::FilePermissionError)
You don't have write permissions for the /Library/Ruby/Gems/2.6.0
[2022 Solution]
The detailed reason for the issues has been added here
Summary of the issue -> The issue was related to ruby access, M1 MAC comes with its own ruby. We don't have permission to use that for our purpose. Instead, we install a separate instance of ruby and use it for our purpose.
The below steps helped me resolve the problem, hope this might help some
We don't need to install ruby with rvn or chruby.
My solution uses homebrew to install ruby.
Open the terminal
Install ruby using homebrew
[for fresh install] brew install ruby
[for reinstalling] brew reinstall ruby
Check the path of ruby using the below command
which ruby
It should be installed in the below path
/usr/bin/ruby
To change the ruby path to the user path
To check which shell is used by your system
echo $0
-zsh
For zshrc
echo 'export PATH="/usr/local/opt/ruby/bin:$PATH"' >>~/.zshrc
For bash
echo 'export PATH="/usr/local/opt/ruby/bin:$PATH"' >>~/~/.bashrc
Quit and relaunch the terminal
After changing the path with step 5
Check for the path of the ruby again (execute step 3 - please make sure the path displays as given below)
/usr/local/opt/ruby/bin/ruby
[if you don't quit and launch the terminal, step 4 path will be shown]
This step may not be applicable to everyone can skip step 10 & 11, if you have the correct Cocoapods version installed
Check the version of the pod installed
pod --version
Uninstall the specific version of Cocoapods using the below command
In case the version installed is 1.11.0
gem uninstall cocoapods -v 1.11.0
Install the Cocoapods of the specific version
gem install cocoapods -v 1.11.0
Change the path to the Project directory cd {path of the project directory}
Install the bundler in the project directory
bundle install
Execute pod install
pod install
cd /Library/Ruby/Gems/2.0.0
open .
right click get info
click lock
place password
make everything read and write.
Installing gem or updating RubyGems fails with permissions error Then Type This Command
sudo gem install cocoapods
You can use: gem install cocoapods --pre --user
give the user $whoami to create somethin in those folder
sudo chown -R user /Library/Ruby/Gems/2.0.0
I have installed ruby 1.9.3 using hombrew
brew install ruby
But default 1.8.7 is still used. How can I switch osx to use 1.9.3 as default ruby?
I suggest you take a look at rvm.
You can then set it as default with rvm use 1.9.3 --default
But if you are happy with your homebrew install.
Then just change the precedence of directories in the PATH
Here is my /etc/paths
# homebrews should always take precedence
/usr/local/bin
# the default stack
/usr/bin
/bin
/usr/sbin
/sbin
This is important generally for homebrew, else the system version of git, ruby, pg_admin,... will all be used instead of the brew version.
if you say which -a ruby you'll see all the installed rubies, and the precedence in the PATH
eg.
$ which -a ruby
/Users/matthew/.rvm/rubies/ruby-1.9.3-p0/bin/ruby
/Users/matthew/.rvm/bin/ruby
/usr/bin/ruby
UPDATE: I now don't think you should change /etc/paths
Instead you need to check which of .profile, .bashrc, or .bash_login is being loaded in your shell, and just add /usr/local/bin to your path.
For me, I only have a .profile. You can create that file if none of those files already exist in your home directory.
# homebrews should always take precedence
export PATH=/usr/local/bin:$PATH
SHORT ANSWER:
after installing ruby via homebrew just do this:
brew link --overwrite ruby
and restart or reopen your Terminal
LONG ANSWER
So I did a normal install of ruby using homebrew
brew install ruby
that installed fine BUT it was still using the system's default ruby.
which I verified by doing:
which ruby
#/usr/bin/ruby
So as per Matthew Rudy's suggestion, I checked the order of my /etc/paths, and all was good.
Then I decided to do:
which -a ruby
#/usr/bin/ruby
#usr/local/bin/ruby
so nothing was broken as such.
tried to reinstall ruby again using the homebrew method, and then i found it.
Homebrew mentioned:
Warning: ruby-2.3.1 already installed, it's just not linked
so had to do:
brew link --overwrite ruby
If you'd like to use homebrew to install 1.9.3, you can follow these steps:
$ brew update
$ brew install rbenv
$ brew install ruby-build
Once you have rbenv and ruby-build installed, you can run the following command to get Ruby 1.9.3 installed.
$ rbenv install 1.9.3-p125
Now if you’d like to use 1.9.3 by default, you can run the following command:
$ rbenv global 1.9.3-p125
I had similar situation. I installed ruby using Homebrew. which -a ruby gave me the following output:
#usr/local/bin/ruby
#/usr/bin/ruby
Which means that newly installed version should have been used, but ruby --version still returned the old system version.
I quit terminal (Cmd+Q), and after restart ruby --version returned the correct version. So make sure you restart terminal after installing before trying any other (potentially unnecessary) fixes.
Ruby was installed by Homebrew at /usr/local/opt/ruby. So, we need to add this path to bash or Zsh.
# Type this to find out which shell you're using (e.g., bash, Zsh)
echo $SHELL
# If you're using Bash (e.g., echo $SHELL returns /bin/bash)
echo 'export PATH="/usr/local/opt/ruby/bin:$PATH"' >> ~/.bash_profile
# If you're using Zsh
echo 'export PATH="/usr/local/opt/ruby/bin:$PATH"' >> ~/.zshrc
Then, source the file
# E.g., if you're using bash
source ~/.bash_profile
Finally, verify ruby's version
ruby -v
SHORT:
Do note what you want to change it for.
If you're on OS X and trying to use Ruby for something like Jekyll, then don't use homebrew because that's what Apple is using for Ruby for and it might not be good to use if you're not sure what you're doing. Instead, use rbenv or RVM.
LESS SHORT:
I was trying to switch from the default version to an updated version (from 2.0) to use Jekyll because it required Ruby version 2.2.5 and above. I updated it and version 2.5 was installed, but when I checked "ruby -v", it was still 2.0. Once I finally got around to changing the default version, I wasn't able to install the package I needed because I didn't have write permission. For example, if you come across something like this, then you probably are having the same problem
$ gem install jekyll bundler
ERROR: While executing gem ... (Gem::FilePermissionError)
You don't have write permissions for the /Library/Ruby/Gems/2.0.0 directory.
In OSX you can change the path using:
sudo nano /etc/paths
And then add a path or change the order.
After seeing Craig Wayne's answer I thought maybe I had missed a warning when installing ruby with Homebrew. So I reinstalled it with brew reinstall ruby#2.7 and there it was:
==> Caveats
By default, binaries installed by gem will be placed into:
/usr/local/lib/ruby/gems/2.7.0/bin
You may want to add this to your PATH.
ruby#2.7 is keg-only, which means it was not symlinked into /usr/local,
because this is an alternate version of another formula.
If you need to have ruby#2.7 first in your PATH, run:
echo 'export PATH="/usr/local/opt/ruby#2.7/bin:$PATH"' >> ~/.zshrc
For compilers to find ruby#2.7 you may need to set:
export LDFLAGS="-L/usr/local/opt/ruby#2.7/lib"
export CPPFLAGS="-I/usr/local/opt/ruby#2.7/include"
For pkg-config to find ruby#2.7 you may need to set:
export PKG_CONFIG_PATH="/usr/local/opt/ruby#2.7/lib/pkgconfig"
So the solution for me was adding /usr/local/opt/ruby#2.7/bin: to the beginning of PATH in ~/.zshrc.
Just as an alternative approach for anyone else looking for an answer to this - you can set an alias in your .bash_profile e.g
ruby="/usr/local/bin/ruby"
this is how i got around the issue