Should rbenv be installed system-wide, or at a use

2019-03-18 08:56发布

问题:

I'm building a vagrant setup, and part of that is installing rbenv. I'm using librarian-chef to manage all my chef cookbooks, and it installs rbenv and ruby-build.

However, when I tried to ssh into my Vagrant VM and type ruby -v I got the standard system-installed ruby 1.8.7 (2012-02-08 patchlevel 358) [x86_64-linux]. Thinking that maybe rbenv was not installed, I tried running rbenv versions, but rbenv was in fact installed:

vagrant@precise64:~$ rbenv versions
* system (set by /opt/rbenv/version)

So then I tried rbenv install [version]:

vagrant@precise64:~$ rbenv install 1.9.3-p327
[...]

BUILD FAILED
[...]

test -z "/opt/rbenv/versions/1.9.3-p327/include" || /bin/mkdir -p "/opt/rbenv/versions/1.9.3-p327/include"
/bin/mkdir: cannot create directory `/opt/rbenv/versions/1.9.3-p327': Permission denied

That failed with Permission denied. I tried installing again with sudo:

sudo rbenv install 1.9.3-p327

And that worked. Then I tried running rbenv versions again:

vagrant@precise64:~$ rbenv versions
* system (set by /opt/rbenv/version)

But it still says only system ruby is installed. However, if I run it with sudo:

vagrant@precise64:~$ sudo rbenv versions
* system (set by /home/vagrant/.rbenv/version)
  1.9.3-p327

rbenv versions now shows 1.9.3 was installed.

So there seems to be a disconnect, in that that rbenv and my ruby version are now installed on a system level and not on the user level.

I am using the rbenv-cookbook. I would like to have rbenv set up with chef, because that saves me from setting it up manually, post-install.

The other issue I'm having is that it seem like everything that is ruby-controlled, such as gem, is also suffering the same disconnect.

vagrant@precise64:~$ gem install bundler
Fetching: bundler-1.3.5.gem (100%)
ERROR:  While executing gem ... (Gem::FilePermissionError)
    You don't have write permissions into the /opt/vagrant_ruby/lib/ruby/gems/1.8 directory.

回答1:

About two years ago, a discussion happened in github about shared installs, which appears to answer the question.

Synopsis: sstephenson (rbenv author) specifically doesn't like encouraging system-wide installs because of complexities with permissions, write access, etc. He believes adding robust support would make rbenv more complex, and simplicity is the goal.

EDIT

I've since come across fnichol's chef-rbenv cookbook, which, if you actually want to install a system-wide rbenv, gives a right and proper method, and you can automate it with Chef (I recommend knife solo).

It installs this to /etc/profile.d/ so it will run for all users, putting the proper ruby into the PATH.



回答2:

rbenv should be installed at a user level.

Unfortunately, this means that when running gem install, you may run into the problem you saw:

You don't have write permissions into the {...} directory

You can solve this by setting the correct permissions on the ~/.rbenv directory.

sudo chown -R yourusername ~/.rbenv

After chowning the directory, you'll be able to run gem install without sudo.



回答3:

Everything in rbenv's Readme is assume a local (per-user) installation in ~/.rbenv/ so i would assume that the official recommendation is to install it on a per-user basis.

Still, it's possible to install it globally. But you'll need sudo for all commands that need to writing rights. Installing new rubies, installing gems* and changing the global ruby version are the ones coming to my mind here, there might be more.

(*) Needing sudo for installing new gems is the default when you install your ruby through the default system routines like apt on debian, not needing sudo for it is actually a positive side effect of using ruby version managers that work on a per-user basis