rbenv: gem commands are not found on Linux Mint - ruby

I'm on Linux Mint 17.2.
I recently removed ruby with apt-get purge ruby.
I then installed rbenv and then did rbenv install 2.3.0 so now, ~/.rbenv/versions/2.3.0/bin/ruby exists.
But now, I can't do gem install rubocop. I get this:
$ gem install rubocop
rbenv: gem: command not found
The `gem' command exists in these Ruby versions:
2.3.0
But I can do ~/.rbenv/versions/2.3.0/bin/gem install rubocop.
However, once I'm done, I can't use it:
$ rubocop --auto-correct
-bash: /usr/local/bin/rubocop: /usr/bin/ruby1.9.1: bad interpreter: No such file or directory
I also can't find out where this ruby came from:
$ dpkg -S /usr/bin/ruby1.9.1
dpkg-query: no path found matching pattern /usr/bin/ruby1.9.1
It's possible it was installed via RVM a long time ago.
Any idea how I can fix my Ruby?

try run rbenv global 2.3.0 && rbenv rehash

It looks like you haven't run the rbenv shell magic to add bin shims to your path.
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"
It's recommended to add that to your .bashrc or .bash_profile so it gets executed on login.
Take a read through this - it covers rbenv and bundler but the initial setup will be what you need to look at.

Related

Can I execute gems from outside an rbenv environment?

I am able to install and execute a gem as follows:
rbenv install 2.4.1
rbenv local 2.4.1 # enter the environment
gem install fpm
fpm --version
I was expecting to be able to execute the gem from outside the environment too, something like:
rbenv local --unset # leave the environment
rbenv rehash # update shims
fpm --version
But instead I get:
rbenv: fpm: command not found
The `fpm' command exists in these Ruby versions:
2.4.1
Have I misunderstood how rbenv shims work? Can I execute a gem from outside an rbenv environment?
From the comments in your Github issue it seems you got the basic idea now; rbenv shims are nothing more than glorified shell scripts that tie a command to a particular currently active Ruby version.
But nothing prevents you from creating your own hard-coded shims. For example, let's say you want this global fpm command that is available outside any particular rbenv environment.
Here's a simple way to do it:
> rbenv local 2.4.1
> ln -s `rbenv which fpm` ~/.rbenv/bin/fpm24
> rbenv local --unset
> fpm24 --version
1.10.2
This will install a "shim" into ~/.rbenv/bin/fpm24 that is a hard-coded pointer to the 2.4.1 gem that rbenv has installed previously. Now this fpm24 command will work anywhere, as long as you have ~/.rbenv/bin in your PATH.

How to get newer version of Ruby into gem environment

I've installed Ruby 2.2.2 with rbenv. However when I run
gem env
it says that I am using 2.0.0. I read on a different question to run
sudo gem install -n /usr/local/bin --no-ri --no-rdoc bundler
rbenv rehash
bundle --path=vendor/bundle
However, when running the last line, I receive the error:
Could not locate Gemfile or .bundle/ directory
How do I get the system to use Ruby 2.2.2?
EDIT:
I printed out the contents of my .bash_profile and it had the following two lines:
export PATH
export PATH="$HOME/.rbenv/bin:$PATH"
Is it bad that there is a blank export PATH? If so, how do I remove it?
If you have Ruby 2.2.2 installed already, you can use renv global
rbenv global 2.2.2
Check out the documentation here.
It turns out RVM was still present in the system. All I had to do was run:
rvmsudo rvm implode

Bundle command not found mac

I'm using ruby, and I was given a zip file with some ruby programs and it says: inside the folder, run bundle install to install the packages required.
When I run the command in my terminal, it says bundle command not found.
Can someone please give me a detailed description of how I can fix this?
gem install bundler
is how to do it.
You may want to use a tool such as rbenv to manage gems.
Just reiterating that for those (at least on OSX) for whom
gem install bundler
Gives a permissions error, an option that seems to have worked for many people is to use rbenv, which kind of adds a shim between your ruby commands (like gem install) and your environment (if my understanding is correct).
Definitely check out this answer.
The process is laid out fairly well under the above link. I chose to install via homebrew:
brew update
brew install rbenv
Then you have to add an argument command to your profile, which if you're using the common ~/.bash_profile, can be done with:
echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
Which it looks like is adding a command to initialize rbenv via your shell.
Don't for get to start a new shell, possibly by opening a new terminal or using the source ~/.bash_profile command.
Make sure your $PATH has this .rbenv/shims BEFORE any other directory where your shell might be looking for Ruby (OSX comes with it's own version that we don't want to fiddle with): echo $PATH.
which ruby
/Users/mikekilmer/.rbenv/shims/ruby
#GOOD!
Now install a version of Ruby:
rbenv install 2.2.3
(See all possible versions with rbenv install -l).
Now we can use rbenv global 2.2.3 to switch to a use the newer version of Ruby globally. (Hmm. I thought we didn't want to mess with the system version.) You could also try it with rbenv local 2.2.3 or rbenv shell 2.2.3.
Finally run:
rbenv rehash
Now ruby -v should return 2.2.3 and gem install bundler should work.
Did here.
Just run gem install bundler in your terminal.
There is a link to bundler you can take a look:bundler
Some ruby version managers like chruby and rbenv store gems separately for each version, so when you install a different version of ruby, you'll need to gem install bundler.
Tried every solution here but didn't work out. Eventually I got this to work in two different methods:
Set alias bundle=/path/to/bundle in .bashrc if you don't care the nastiness.
Recreate a fresh dev env via rbenv and do bundle install rails will fix it (fixed my issue).
Terminal -
sudo su
then your password:
change directory :
cd command .
if you do not have permissions to write to drive.
chmod 755 foldername.
And you can also mkdir command in terminal
mkdir /Library/Ruby/Gems/2.3.0.1
copy and paste: gem install bundler paste to the terminal.
Fetching: bundler-1.16.2.gem (100%)
bundler's executable "bundle" conflicts with /usr/local/bin/bundle
Overwrite the executable? [yN] y
bundler's executable "bundler" conflicts with /usr/local/bin/bundler
Overwrite the executable? [yN] y
Successfully installed bundler-1.16.2
Parsing documentation for bundler-1.16.2
Installing ri documentation for bundler-1.16.2
Done installing documentation for bundler after 7 seconds
1 gem installed
works for OS X High Sierra.

Ruby Version Inconsistent [duplicate]

I installed rbenv according to the github directions. I am running OSX but I have tried this on a Ubuntu 12.04 VM and got the same results. The following is what i get in my terminal when I try to change ruby versions:
rbenv versions
* 1.9.3-p0 (set by /Users/user/.rbenv/version)
1.9.3-p125
rbenv global
1.9.3-p0
rbenv rehash
ruby -v
ruby 1.8.7 (2011-12-28 patchlevel 357) [universal-darwin11.0]
which ruby
/usr/bin/ruby
Anyone have any ideas as to why rbenv isn't switching the ruby version like it thinks it is? Also there is no .rbenv file in the local directory that would be causing the ruby version to default to 1.8.7
rbenv local
rbenv: no local version configured for this directory
Check that PATH contains $HOME/.rbenv/shims and $HOME/.rbenv/bin
$ env | grep PATH
Also check that you have the following in your ~/.bash_profile if using bash or ~/.zshenv if using zsh
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"
NOTE:
Make sure it's the last setting in your ~/.bash_profile . I ran into an issue where I installed a program that updated my .bash_profile and reset PATH.
Finally, make sure your $HOME folder doesn't have a .ruby-version file that you may have created by accident if you were to have done $ rbenv local <ruby-version> in your $HOME folder. Doing $ rbenv global <ruby-version> modifies the $HOME/.rbenv/version file, and the existence of a .ruby-version file in the $HOME folder would override the version set by $HOME/.rbenv/version.
From the docs:
Choosing the Ruby Version
When you execute a shim, rbenv determines which Ruby version to use by reading it from the following sources, in this order:
The RBENV_VERSION environment variable, if specified. You can use the rbenv shell command to set this environment variable in your current shell session.
The first .ruby-version file found by searching the directory of the script you are executing and each of its parent directories until reaching the root of your filesystem.
The first .ruby-version file found by searching the current working directory and each of its parent directories until reaching the root of your filesystem. You can modify the .ruby-version file in the current working directory with the rbenv local command.
The global ~/.rbenv/version file. You can modify this file using the rbenv global command. If the global version file is not present, rbenv assumes you want to use the "system" Ruby—i.e. whatever version would be run if rbenv weren't in your path.
I fixed this by adding the following to my ~/.bash_profile:
#PATH for rbenv
export PATH="$HOME/.rbenv/shims:$PATH"
This is what is documented at https://github.com/sstephenson/rbenv.
From what I can tell there isn't ~/.rbenv/bin directory, which was mentioned in the post by #rodowi.
This may be an old question, but Google led me here and, for posterity sake, thought I'd share.
My problem persisted after many of the recommended solutions above. Like the OP, I installed rbenv and then a ruby version, but my laptop defaulted to system. What I had overlooked was that when I ran:
[~/.rbenv] $ rbenv versions
* system (set by /Users/alphadogg/.rbenv/version)
2.0.0-p247
IOW, it was still defaulting to system. A quick
[~/.rbenv] $ rbenv local 2.0.0-p247
switched it to the new version.
First step is to find out which ruby is being called:
$ which ruby
Your system says:
/usr/bin/ruby
This is NOT the shim used by rbenv, which (on MacOS) should look like:
/Users/<username>/.rbenv/shims/ruby
The shim is actually a script that acts like a redirect to the version of ruby you set.
I recommend that for trouble shooting you unset the project specific "local" version, and the shell specific "shell" version and just test using the "global" version setting which is determined in a plain text file in ~/.rbenv/version which will just be the version number "1.9.3" in your case.
$ rbenv global 1.9.3
$ rbenv local --unset
$ rbenv shell --unset
You can do ls -laG in the root of your project folder (not the home folder) to make sure there is no longer a ".ruby-version" file there.
You can use rbenv versions to identify which version rbenv is set to use (and the location and name of the file that is setting that):
$ rbenv versions
NONE OF THAT MATTERS until you set the path correctly.
Use this to make sure your *MacOS will obey you:
$ rbenv init -
Followed by:
$ which ruby
To make sure it looks like:
/Users/<username>/.rbenv/shims/ruby
Then run this to add the line to your profile so it runs each time you open a new terminal window:
$ echo 'eval "$(rbenv init -)"' >> ~/.bash_profile
There are other ways to modify the path, feel free to substitute any of them instead of running the rbenv init.
NOTE: reinstall Rails with:
$ gem install rails
If you were trying to run Ruby on Rails, then you need to have this all working first, then install the rails gem again. A previous install of Rails will use a hard coded path to the wrong ruby and several other things will be in the wrong place, so just install the gem again.
P. S. If your MacOS won't obey you (*mentioned above) then you may have to find another way to modify your path, but that's unlikely to be a problem because "Macs just work" ;)
I had this issue when setting up Ruby using rbenv on MacBook Pro Catalina OS and MacBook Pro Big Sur.
Here's how I fixed:
First run the command below to initialize rbenv:
rbenv init
This will give you some instruction on what to do. Basically you will have to open the ~/.zshrc file and add this to the file eval "$(rbenv init -)". You can accomplish this by running the command below:
echo 'eval "$(rbenv init -)"' >> ~/.zshrc
Next, run the command below to set your Ruby version:
rbenv local <your-desired-ruby-version>
In my case, my desired Ruby version was 3.0.1, so it was:
rbenv local 3.0.1
When you are done, quit your terminal using Command + Q, and then open a new terminal, this time when you run the command:
ruby -v
rbenv versions
You will see that your desired Ruby version has already been set up for you.
In my case changing the ~/.zshenv did not work. I had to make the changes inside ~/.zshrc.
I just added:
# Include rbenv for ZSH
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"
at the top of ~/.zshrc, restarted the shell and logged out.
Check if it worked:
➜ ~ rbenv install 2.4.0
➜ ~ rbenv global 2.4.0
➜ ~ rbenv global
2.4.0
➜ ~ ruby -v
ruby 2.4.0p0 (2016-12-24 revision 57164) [x86_64-darwin16]
I just found this same problem. What I did was uninstall rbenv (via homebrew) and reinstall it. I also added
if which rbenv > /dev/null; then eval "$(rbenv init -)"; fi
into ~/.bash_profile when I reinstalled rbenv. Works perfectly now.
Run this command
Add rbenv to bash so that it loads every time you open a terminal
echo 'if which rbenv > /dev/null; then eval "$(rbenv init -)"; fi' >> ~/.bash_profile
source ~/.bash_profile
this will solve your problem Reference
If you are using bash, go to
~/.bash_profile
and add the following line (if it's not already there)
if which rbenv > /dev/null; then eval "$(rbenv init -)"; fi
If you are using Zsh, go to
~/.zshrc
and add the same line of code, at the end of the .zshrc file.
Then simply restart your terminal and it should be fine now.
rbenv help shell
"Sets a shell-specific Ruby version by setting the 'RBENV_VERSION' environment variable in your shell. This version overrides localapplication-specific versions and the global version. should be a string matching a Ruby version known to rbenv.The special version string 'system' will use your default system Ruby. Run rbenv versions' for a list of available Ruby versions."
Provided rbenv was installed correctly, ruby -v will correspond to
rbenv shell 1.9.3-p125
I had the same problem, but caused by Homebrew:
[~]$ rbenv version
2.3.0 (set by /Users/user/.rbenv/version)
[~]$ ruby -v
ruby 2.3.3p222 (2016-11-21 revision 56859) [x86_64-darwin16]
[~]$ which ruby
/usr/local/bin/ruby
Somehow I had installed Ruby via Homebrew too, and the Homebrew path was ahead of the rbenv path in my $PATH. Running brew uninstall ruby fixed it for me.
run:
rbenv init
After I ran that, when i set my local rbenv version:
rbenv local 2.4.0
then my ruby -v and my rbenv local versions coincided.
Note: You might also want to exit the directory you're in and then go back into it, i've noticed that was necessary for me in order to get things to work.
for fish shell user
set --universal fish_user_paths $fish_user_paths ~/.rbenv/shims/
As for me the easiest way to use rbenv along with zsh is adding rbenv to plugins section in .zshrc config. In my case it looks similar to:
# Which plugins would you like to load? (plugins can be found in ~/.oh-my-zsh/plugins/*)
# Custom plugins may be added to ~/.oh-my-zsh/custom/plugins/
# Example format: plugins=(rails git textmate ruby lighthouse)
# Add wisely, as too many plugins slow down shell startup.
plugins=(git bower rails ruby rbenv gulp npm node nvm sublime)
After that there're no problems when installing, switching, using ruby versions with help of rbenv.
Mind to restart your terminal session after made changes.
The accepted answer suggests to add the following:
export PATH="$HOME/.rbenv/bin:$PATH"
This will not work on Mac OSX, which the OP references. In fact, if you install rbenv via brew install rbenv, which is really the only installation method in Mac OSX, since curl -fsSL https://github.com/rbenv/rbenv-installer/raw/master/bin/rbenv-installer | bash will FAIL in OSX, then the rbenv executable will be installed in:
$ which rbenv
/usr/local/bin/rbenv
However, even in OSX, the rbenv root will remain in the $HOME directory:
~ viggy$ rbenv root
/Users/viggy/.rbenv
What does this mean? It means when you install rubies, they will install in the given home directory under .rbenv:
$ rbenv install 2.6.0
$ ls ~/.rbenv/versions
2.6.0
Now the brew installation did some work that you would have to perform manually in Linux. For example, in Linux, you would have to install ruby-build manually as a plugin:
$ mkdir -p "$(rvbenv root)/plugins"
$ git clone https://github.com/rbenv/ruby-build.git "(rbenv root)"/plugins/ruby-build
This is already done with the homebrew installation. There is one important step that must be done in the homebrew installation, as in the Linux installation. You must add the rbenv shims to your path. In order to do that, when your shell starts, you have to evaluate the following command (which will in turn add the rbenv shims to the beginning of your $PATH):
$ vim ~/.bash_profile
eval "$(rbenv init -)"
$ source ~/.bash_profile
Now when you run echo $PATH, you will see the rbenv shims:
$ echo $PATH
/Users/viggy/.rbenv/shims:
Now check your ruby version and it will reflect the rbenv ruby installed:
ruby -v
ruby 2.6.0p0
When I had these symptoms, the problem turned out to be that install had failed halfway through for the new Ruby version I was trying to switch to, without me noticing. The fix was to delete & reinstall it.
(This meant that even though ruby 1.9.3 showed up in the rbenv list of available versions, it didn't have an executable on the path where rbenv assumed it would. Since rbenv tries to change your ruby version just by prepending a location to your path, if there's nothing actually in the location it prepends, your OS will happily continue searching your default path and find your system version, in my case like the question's 1.8.7.)
Make sure the last line of your .bash_profile is:
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"
I came to the same problem.
Fixed this by run the rbenv global command with sudo.
I think it was something permission problem.
update:
I finally found the solution.
There was one same file "version" on my mac, which is under /usr/local/Cellar/rbenv/0.3.0/.
I think it was created by mistake occasionally. you should remove it.
Adding the following to .bashrc works for me -
export PATH="$HOME/.rbenv/shims:$PATH"
This happened to me right after I reinstalled rbenv. Apparently I had a .ruby-version file in my home directory, specifying a version that no longer existed. Once I deleted the file, everything worked.
Update 2023 (Macbook Ventura+)
Even if you have rbenv installed, your computer could still serve the default ruby version. To fix this, just run the following:
rbenv init
This might give an output like this:
# Load rbenv automatically by appending
# the following to ~/.zshrc:
eval "$(rbenv init - zsh)"
Just open your .zshrc file:
sudo nano ~/.zshrc
(Or you can also open this file with any other text editor)
Add this line:
eval "$(rbenv init - zsh)"
Save the file (if used sudo nano, you can do so like this:)
CTRL + X then Y then Enter (For Mac)
Now, refresh your .zshrc profile like this:
source ~/.zshrc
Now type ruby --version, this must give you the ruby version that was provided by rbenv.
There a lot of of misleading answers that work. I thought it was worth mentioning the steps from the rbenv README.
$ brew install rbenv
$ rbenv init and follow the instructions it gives you. This is what I got:
~ $ rbenv init
# Load rbenv automatically by appending
# the following to ~/.bash_profile:
eval "$(rbenv init -)"
I updated my ~/.bash_profile...
Close terminal and open it again
Verify it's working correctly by running:
$ curl -fsSL https://github.com/rbenv/rbenv-installer/raw/master/bin/rbenv-doctor | bash
Now just install the version you want by doing: rbenv install <version>
Linux / Ubuntu Users
Step 1:
$ rbenv versions
system
2.6.0
* 2.7.0 (set by /home/User/Documents/sample-app/.ruby-version) #Yours will be different
2.7.2
Step 2:
$ nano /home/User/Documents/sample-app/.ruby-version
Step 3:
$ ruby -v
ruby 2.7.2p137 (2020-10-01 revision 5445e04352) [x86_64-linux]
I tried every solution, but nothing worked for me. Even I was including the path in the .zshrc file.
In my case, I'm using MacBook Pro M2 with macOS Ventura 13.2. And I'm using React Native 0.71.2, which needed Ruby 2.7.6 version.
I simply placed the following code in the .zprofile instead of .zshrc, and it worked:
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init - zsh)"
You could try using chruby? chruby does not rely on shims, instead it only modifies PATH, GEM_HOME, GEM_PATH.
I forgot to delete rvm before installing rbenv. I deleted rvm and re-installed rbenv, but system still wasn't using the version of Ruby being designated as global by rbenv. I tried shutting Terminal, but it only worked once I restarted the whole machine.
All the other answers here give good advice for various situations, but there is an easier way.
The rbenv docs point us to the rbenv-doctor diagnostic tool that will quickly verify all these potential pitfalls on your system:
curl -fsSL https://github.com/rbenv/rbenv-installer/raw/master/bin/rbenv-doctor | bash
When all is well, you'll see this:
$ curl -fsSL https://github.com/rbenv/rbenv-installer/raw/master/bin/rbenv-doctor | bash <aws:hd-pmp-developer>
Checking for `rbenv' in PATH: /usr/local/bin/rbenv
Checking for rbenv shims in PATH: OK
Checking `rbenv install' support: /usr/local/bin/rbenv-install (ruby-build 20201005)
Counting installed Ruby versions: 1 versions
Checking RubyGems settings: OK
Auditing installed plugins: OK
Now, if we break one of those expectations (e.g. remove rbenv-install), the tool will point us directly to the problem, with a link to how to fix it:
$ mv /usr/local/bin/rbenv-install rbenv-install-GONE
$ curl -fsSL https://github.com/rbenv/rbenv-installer/raw/master/bin/rbenv-doctor | bash
Checking for `rbenv' in PATH: /usr/local/bin/rbenv
Checking for rbenv shims in PATH: OK
===> Checking `rbenv install' support: not found <===
Unless you plan to add Ruby versions manually, you should install ruby-build.
Please refer to https://github.com/rbenv/ruby-build#installation
Counting installed Ruby versions: 1 versions
Checking RubyGems settings: OK
Auditing installed plugins: OK
Strangely, rbenv version did not set the .rbenv file.
Check with: ls -ltra --> to see that a rbenv was written.
Adding eval "$(rbenv init -)" to the .bash_profile on my mac resolved this issue.
rbenv local
ruby -v gave the set
Apparently, it worked for me when I used the Rosetta terminal
You can try doing the same thing using Rosetta terminal
by going into
Finder > Utilities > Terminal > Get Info

How to install a gem or update RubyGems if it fails with a permissions error

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

Resources