Gem permission issues with rbenv running? - ruby-on-rails

I am using Rbenv to manage my ruby versions, and don't remember ever having an issue before. It's been a while since I developed at home though.
Basically this is what happens:
➣ gem install rails
ERROR: While executing gem ... (Gem::FilePermissionError)
You don't have write permissions for the /Library/Ruby/Gems/2.0.0 directory.
➣ rbenv local
2.3.1
➣ rbenv global
2.3.1
Really not sure what I'm missing. Never seen this issue before.

#rPat, what does the command
$ which gem
reveal? If it indicates you're using the the system gem command, try
$ rbenv rehash
and see if that resolves the issue.
Also make sure
eval "$(rbenv init -)"
is in your .bash_profile

You're using the system version at the moment, and it sounds like you want to use a private Ruby. First, make sure that you have the current rbenv version by following the Upgrading instructions.
When you have the most recent version of rbenv, pick a version of Ruby that you want to use (e.g. 2.2.5) and run:
rbenv install 2.2.5
This will install that Ruby version privately.
Next, you can set the version to use, by default, run this to set the Ruby version as your local default:
rbenv local 2.2.5
And that should get you up and running.

Related

Permissions error when installing Ruby on OSX as per official docs, with rbenv

I am trying to install Rails on my OSX El Capitan v10.11.3, as per these official instructions. However, after installing ruby, I am getting the following issues with the rails installation:
Unknown-88-63-df-a3-2e-e9:~ username$ gem install rails -v 4.2.4
ERROR: While executing gem ... (Gem::FilePermissionError)
You don't have write permissions for the /Library/Ruby/Gems/2.0.0 directory.
Even though I am using rbenv ? What can I do to workaround / fix this?
EDIT: if I use sudo then I get:
railties's executable "rails" conflicts with /usr/bin/rails
ERROR: While executing gem ... (Errno::EPERM) Operation not permitted - /usr/bin/rails
Any ideas would be much appreciated. Many Thanks. Also, is there somewhere I can feed the resolution of this back into the rails docs community?
You are trying to install gems into the system ruby (i.e. the ruby that comes from Apple with OSX) don't do that.
Try:
rbenv versions
to list the versions of ruby you have.
Then pick one that is NOT system.
[16:55][d#phobos:~]$ rbenv versions
* system (set by /usr/local/var/rbenv/version)
1.8.7-p357
1.9.2-p290
1.9.3-p392
1.9.3-p448
2.1.5
[16:55][d#phobos:~]$ rbenv local 2.1.5
[16:55][d#phobos:~]$ rbenv version
2.1.5 (set by /Users/d/.ruby-version)
[16:56][d#phobos:~]$
If you have nothing but system installed, install the rbenv build and install the ruby you want...
Read this carefully:
https://github.com/rbenv/rbenv
and this
https://github.com/rbenv/ruby-build#readme
Run which ruby check if it was pointing to /Library/...
Run rbenv global x.y.z to set current ruby version
You may need to relaunch terminal

Having trouble with Ruby versions - rbenv

I recently updated my computer to Mac OS X El Capitan and had to reinstall homebrew and rbenv.
When I go to bundle install in one of my rails app and run bundle install I get: "Your Ruby version is 2.0.0, but your Gemfile specified 2.2.0".
However, when I do ruby-v I get: ruby 2.2.0p0 . Why am I seeing two different versions and how can I change it?
rbenv -h will tell you what to do.
"rbenv versions" List all Ruby versions available to rbenv
"rbenv global" Set or show the global Ruby version
"rbenv local" Set or show the local application-specific Ruby version
to set, just do "rbenv local 2.2.0" or "rbenv global 2.2.0"
Also, there is the .ruby-version file that can also set ruby version.
Look at the instructions from rbenv main github page for more info. They do a pretty good job describing how to set the active ruby version.
https://github.com/sstephenson/rbenv
It seems that you must update your executables (bundle install), try this rbenv rehash. Take from here
rbenv rehash Installs shims for all Ruby executables known to rbenv (i.e., ~/.rbenv/versions//bin/). Run this command after you install a new version of Ruby, or install a gem that provides commands.
This is a duplicate of another question and you can find the answer here https://stackoverflow.com/a/53849574/3182171
For convenience I report also here.
Try with this.
In your rails project folder check the presence of .ruby-version file and put inside the same ruby version specified into Gemfile.
(if this file is not present, create it.)
~/your-rails-project/.ruby-version file:
2.2.0
~/your-rails-project/Gemfile file:
source 'https://rubygems.org'
ruby '2.2.0'
...
Then install that version with rbenv:
$ rbenv install 2.2.0
$ rbenv rehash
$ rbenv local 2.2.0
$ rbenv global 2.2.0
Now check that you are using the right version with:
$ ruby -v
You can now execute
$ bundle install

Trying to set up Ruby Rails on Mac

Am trying to set up rails on mac using rbenv and Homebrew.
Currently getting the following message when attempting to 'gem install rails':
ERROR: While executing gem ... (Gem::FilePermissionError)
You don't have write permissions for the /Library/Ruby/Gems/2.0.0 directory.
username-mbp:projects username$ gem install rails
Any ideas??
If you are using rbenv, you should not use sudo to install gems. rbenv very helpfully installs your gems under your home directory in a way that allows you to use different gems for each installed Ruby version. When you change versions of Ruby you will really appreciate this.
To see the current version of Ruby, use rbenv local. For me this prints:
2.2.2
To see all the Ruby versions on your system of which rbenv is aware:
rbenv versions
rbenv stores the version specifier in a file called .ruby-version. This allows you to use different versions of Ruby for different projects, each version having its own set of gems.
When you try to install rails and get the Gem::FilePermissionError, it means that rbenv is not active, or you are deliberately installing into the "system" Ruby. There is nothing wrong with this per se, but you are not taking advantage of rbenv.
I recommend installing Rails again, using rbenv local to ensure that you are adding the gems to the correct path. You'll know this is working when
gem env gemdir
produces something like:
/Users/username/.rbenv/versions/2.2.2/lib/ruby/gems/2.2.0
See https://github.com/sstephenson/rbenv#installation for more info.
This probably means that you used sudoat some point, which means that you run a command that allows you (as a permitted user to execute a command as the superuser or another user) See here: http://linux.about.com/od/commands/l/blcmdl8_sudo.htm.
Can you please paste the commands you used for installing rbenv, ruby, gem, brew, etc.? Also please paste the output of brew doctorto see if environment is correctly configured for Homebrew. Also, please paste the OSX version and rbenv versionsif rbenv is installed.
The steps for installing ruby on rails on OSX are:
Install Homebrew by:
ruby -e "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/master/install)"`` (as seen here: http://brew.sh/). Run brew doctor and brew updateto see if everything is fine.
Install ruby: OS X comes with Ruby installed (Mavericks/Yosemite even gets version 2.0.0, previously it was only 1.8.7).
Install rbenv: it can be done either by GitHub Checkout or Brew. You probably should use brew. Run brew install rbenv ruby-build(this will also install ruby-build -https://github.com/sstephenson/ruby-build#readme-). You can also use this command brew install rbenv ruby-build rbenv-gem-rehash. Then echo 'eval "$(rbenv init -)"' >> ~/.bash_profile (to enable shims and autocompletion). You should problably run this too: echo 'export PATH="$HOME/.rbenv/bin:$PATH"' >> ~/.bash_profile. Close terminal and open it again. Install the preferred version of ruby (if you want): rbenv install 2.0.0-p353.
Install Bundler: gem install bundler.
Install SQLite: gem install sqlite3
Install Rails: gem install rails.
So, the error you are having is due to permissions (you can understand about them here: http://www.tutorialspoint.com/unix/unix-file-permission.htm). Many people suggest fixing the issue with sudo or chown (http://www.cyberciti.biz/faq/how-to-use-chmod-and-chown-command/). I don't recommend that as it messes with system configuration. It will be better that you run:
rbenv install 2.1.2
rbenv global 2.1.2
gem update --system
When I run with this error like a year ago, what I did was uninstall everything and start again... but, probably that'll take too long.
These links might help you:
ruby for mac, ruby rbenv, rbenv githube, rubies and gems, question on stack
Use sudo:
sudo gem install rails
This guide helped me a lot: Setup Ruby On Rails on
Mac OS X 10.10 Yosemite

How do I know if I'm using rbenv

After reading about Ruby gems and having no idea what rbenv or RVM was, I figured I should probably have one of the two.
I tried installing rbenv using Homebrew however it told me I had already installed rbenv. I always seem to have problems adding gems and usually end up using the sudo command to get it to work (which is a horrible idea I assume).
I discovered I had these problems when I was trying to add the braintree API gem and got an error whenever I tried to start the server.
Commands I tried:
gem install "braintree"
bundle install
sudo gem install "braintree"
The error received from running rails server was:
"/config/initializers/braintree.rb:2:in `<top (required)="">': uninitialized constant Braintree::Configuratio (NameError)"
To take it from the top, rbenv and RVM are Ruby version managers. This means that you can have multiple versions of Ruby installed on your computer at once and select which one you would like to use. I have used both and personally like rbenv's approach.
With that said I think you need to remove the quotes from around the name of the gem you are installing.
Also, in your Gemfile do you have the braintree gem listed? It should be as simple as gem 'braintree'.
If you have multiple versions of Ruby installed or even if you just have the system Ruby and a version installed with RVM or rbenv you may be starting your Rails server with the wrong Ruby version (ie it is missing the gem). You can see if it is using rbenv by typing which ruby and it should print something out with .rbenv/ whatever. If not you need to set rbenv as your current ruby. You can do that like rbenv global 2.1.1 where 2.1.1 is the version of ruby you installed with rbenv. If you haven't installed a version with rbenv you can use ruby-build and do rbenv install 2.1.1 or whatever version you want. Then when you launch your Rails server prefixing the command with bundle exec.
You can set a local ruby-version for your directory by executing rbenv local 2.1.1 once again where 2.1.1 is the version you want.
Reading the rbenv docs will go a long way.
To check if you are using rbenv, simply use this
rbenv
*check if 'rbenv' commands were listed
Same with rvm use
rvm
Run the command rbenv in terminal. If you have rbenv installed, it will list some help commands. If rbenv is not installed, it will output something like "No command 'rbenv' found"
Run the command rvm in terminal. If you have RVM installed, it will list some help commands. If rvm is not installed, it will output something like "No command 'rvm' found"

Mavericks, RBENV, Your Ruby version is 2.0.0, but your Gemfile specified 2.1.1

I've read and tried the suggestions in several, other, questions, like mine (all with accepted answers) as well as a few more hours of Google searching, but nothing worked. That leads me to think my issue is something corrupt with a piece of Ruby/RBENV ecosystem on my computer or maybe a dreaded PATH issue. I always have to run sudo to install any gems, which I've never had to do before, so that's puzzling too.
Here are some outputs of files & commands I think are relevant. If you need more information, please let me know:
.zshrc
export PATH="/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin" if which rbenv > /dev/null;
then eval "$(rbenv init - zsh)"; fi
ruby -v
ruby 2.1.1p76 (2014-02-24 revision 45161) [x86_64-darwin13.0]
rbenv local
2.1.1
Gemfile (I've also blown out my gemfile.lock several times too)
source 'https://rubygems.org'
ruby '2.1.1'
gem 'rails', '4.1.4'
... [omitted for brevity]
.ruby-version
2.1.1
echo $PATH
/[user path]/.rbenv/shims:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin
I'd be grateful if anyone has any ideas about what I should try next, short of reformatting my computer.
EDIT 1: Tried option B installing both of them from their git repos below as per Ben Kreeger. type rbenv correctly returned rbenv is a shell function and the original message kept appearing. I reinstalled Mavericks and my setup still isn't working.
EDIT 2: Any time I try to install a gem, I get a Gem::FilePermissionError saying I don't have access to the Ruby 2.0.0 folder (installed as system). Going to fiddle with RBENV some more...
EDIT 3: More debugging (everything below)
Kept erroring when installing Ruby versions with RBENV and found some people mentioning readline issues.
Tried the following steps to fix readline: https://github.com/sstephenson/ruby-build/issues/550#issuecomment-40681557, and got an error that my Xcode command line tool install was bad.
Ran xcode-select --install to reinstall them.
Tried rbenv install 2.1.1 again, and got The Ruby openssl extension was not compiled. Missing the OpenSSL lib?
As per https://coderwall.com/p/n9bnug, I linked my OpenSSL extension with https://coderwall.com/p/n9bnug
Reset versions of Ruby: rbenv local 2.1.1 rbenv global 2.1.1 and rbenv rehash
Everything seems to indicate bundler is trying to use my system Ruby install (2.0.0-p247) instead of what RBENV is specifying. Baffled where the missing link is.
WOW, JUST WOW.
All I had to do was gem install bundler and then rbenv rehash. Everything worked.
The original error message pointed me to a problem with RBENV or my Ruby version when in reality it was just falling back on an old version of bundler.
Why wouldn't I have received the more standard this needs bundler version [xxxx]... error instead of telling me my Ruby version is specified incorrectly?
If you're having to use sudo to install gems, then something's likely wrong with your rbenv installation. I'm of the opinion that if you're on OS X and you have to run sudo to install gems or packages, you're doing it wrong (especially if you've got homebrew installed)! You've got two options —
Option A: Alter your .zshrc to be a little more friendly with your $PATH and your rbenv settings. Note here that /usr/local/bin is just being prepended to $PATH, which itself is going to set to a proper default by your system (Mavericks). Make those two lines you posted look like this:
export PATH="/usr/local/bin:$PATH"
eval "$(rbenv init -)"
Then ensure that you close any and all shells and open new ones to load the new settings. Then when you run echo $PATH it should look similar to...
[home path]/.rbenv/shims:[home path]/.rbenv/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin
Option B: Since I'm more familiar with installing rbenv via git, you may try nuking your homebrew-installed rbenv installation by brew remove rbenv, and follow the directions specified on rbenv's github page (and don't forget to install ruby-build as well). This includes removing any rbenv-specific lines in your .zshrc and setting them to what rbenv recommends.
You can always check the sanity of your rbenv installation by running type rbenv at a command prompt. If all is well, you should at least be getting back "rbenv is a function".
I solved this by running first
rbenv shell --unset
Then I was able to set the version via
rbenv local <ruby_version>

Resources