Rbenv for production with passenger - ruby

I'm trying to install rbenv on my Debian 6 system for production. What is the best way to do this? I tried some tuts from the github wiki page (https://github.com/sstephenson/rbenv/wiki/Using-rbenv-in-production and https://github.com/sstephenson/rbenv/wiki/shared-install-of-rbenv), but I'm wondering what is the best practise.
Is it better to install it with a deploy user or system wide? With a deploy user, it seems that the rubies aren't available for all users, even if I install it with rbenv global 1.9.3-p125 and the gem commando isn't available too. This must be something with my paths, cause I only included the
if [[ -d $HOME/.rbenv ]]; then
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init -)"
fi
in the .bashrc file of my deploy user. Does passenger/apache know where my rubies are? Because apache is running with the www-data user.
With system wide install (see shared installation link above) I ran in some strange error, that the rbenv command wasn't found and could not be compiled.
So, what should I do? I want to use it for my production server so that I can easily switch between rubies in the future.

I personally would go the deploy user route and use the rbenv-sudo plugin for edge cases like passenger-install, chef or things like that. I think the majority of people don't use rbenv or rvm for production which is why a lot of people are stuck on 1.8. It's a shame because as you said, it'd be nice to easily switch Ruby versions. That being said, production shouldn't switch Ruby that often right? You can pretty easily manage ruby versions yourself by using configure flags from Ruby source. Just don't install into /usr/bin.
Sstephenson says that the shared install is experimental in that shared-install-of-rbenv link. Rbenv global isn't going to do anything across user accounts. It's global within a user account.
Outside of the deploy user, how many accounts do you need on production?

you should specify the user(that passenger uses) has the access privilege to your rbenv.

Related

How to install ruby system-wide using rbenv

You may think this is a classic "global" command question, but it is not.
I can set ruby globally by this:
rbenv global 2.5.1
However this makes ruby2.5.1 global for all users but not for the whole system. When an application in same server want to call ruby or access/pipe ruby, they get command not found error.
I believe we should have to install or symlink to /usr/bin or /usr/local/bin or something...
I couldn't find anything regarding to this. How can I make selected ruby version to be seen to other applications?
My only solution is to build ruby myself to system dir but this will conflict with rbenv.
Examples:
When postfix want to pipe an email to ruby it cannot find ruby even i set global.
When webmin trying to run a command via ruby it cannot find ruby too.
Workaround:
Adding /root/.rbenv/shims folder to the $PATH on executed script works as expected but in some cases it is not possible to directly modify $PATH. What is my option here?
rbenv is simply not designed to do support this, you can see the discussion surrounding this within this Github Issue. There are many technical considerations to take into account like permissions if you do this. I found another blog post outlining the process - System Wide Install With rbenv. Going to copy it into this answer incase the blog post goes away.
However, in the long run, you may find that it's simply easier to create or use Ruby packages, such as the BrightBox PPA ones.
Installing rbenv
Instead of the usual location of ~/.rbenv for single installs we'll
be installing to /usr/local. You can use a different path if you
want, but this is what I prefer.
cd /usr/local
git clone git://github.com/sstephenson/rbenv.git rbenv
chgrp -R staff rbenv
chmod -R g+rwxXs rbenv
Make sure the users that will use rbenv are part of the group you
associated with the rbenv folder.
Now we want to add the following code into each users ~/.profile,
~/.bash_profile, or ~/.zshenv depending on the environment. You
can also add it in /etc/skel/.profile or /etc/skel/.bash_profile
template files that are copied when new users are created.
export RBENV_ROOT=/usr/local/rbenv
export PATH="$RBENV_ROOT/bin:$PATH"
eval "$(rbenv init -)"
Installing ruby-build (optional)
Optionally, you can install the ruby-build plugin to save yourself
from building it yourself.
cd /usr/local/rbenv
mkdir plugins
cd plugins
git clone git://github.com/sstephenson/ruby-build.git
chgrp -R staff ruby-build
chmod -R g+rwxs ruby-build
Notes
Now you should have rbenv and optionally ruby-build setup so you can
get started installing and using Ruby. This install is the same as the
single user install with two exceptions. The global setting applies to
all users and single user rbenv installs can "override" the system
wide install.
If you have permission issues make sure all the files in the rbenv
folder belong to the proper group and that the users trying to use
rbenv are also members of the group.

Cannot find rvm.sh in /etc/profile.d/

I am new to RVM and looking to use it as I deploy my Rails app to a Digital Ocean server. I am following this tutorial and trying to get my Mina script to run (similar to Capistrano). The script includes a set up section with these lines:
source /etc/profile.d/rvm.sh
rvm use || exit 1
Unfortunately, rvm.sh does not exist in /etc/profile.d (or anywhere else on my server). rvm seems to be installed just fine (I can set list rubies, set my Ruby default version, etc). I'm not even sure what rvm.sh would contain if it existed.
Thanks in advance for any help or suggestions you can provide.
#mpapis got me on the right path here. I had installed rvm under one user, but not for this user. I wound up uninstalling rvm and starting from scratch, installing rvm for multiple users.

Managing ruby in different locations on development and production servers

looking for some recommendations on best practices here.
I've been tinkering with a small CGI script. I have RVM installed via Homebrew on my Macbook Air.
I was using the old #! /usr/bin/env ruby trick in my script, which worked fine on my local development environment because I configured all the PATHs to use RVM ruby.
However, when I deploy on the production web host (Bluehost), I must use their ruby located in the standard location at /usr/bin/ruby.
How should I set up my development environment so that I can maintain the same shebang link between development and production? I thought of symlinking /usr/bin/ruby on my local development machine to RVM, and then using the #! /usr/bin/ruby in all my scripts, but that seems like a dirty hack which would not be easily maintained as I swap between different rubies.
Has anyone else run into this problem? If so, was there a graceful way to manage it? Thx.

Using rbenv doesn't work with sudo?

Any commands that use sudo don't seem to work with RBenv.
I'm trying to install ActiveRecord and it says I don't have write permission, so when I try this:
ERROR: While executing gem ... (Gem::FilePermissionError)
You don't have write permissions into the /usr/local/rbenv/versions/1.9.3-p194/lib/ruby/gems/1.9.1 directory.
It says:
sudo: gem: command not found
How can I get around this?
The idea behind tools like rbenv and RVM is that you don't need to use sudo, because your entire Ruby environment exists inside your own workspace as a sandbox.
RVM allows multi-user configurations though it was originally designed for single users.
As far as I've ever seen or read, rbenv is single-user only. At no time should you need to use sudo to manipulate or change your Ruby environment when using rbenv. If you do, something is wrong. If you try to use sudo, you'll screw things up. You might not find out immediately but eventually something will pop up and you'll need to change the ownership of the files back to you.
On Linux and Mac OS you can do that pretty easily using:
sudo chown -R <your_user_name>:<your_group> ~/.rbenv
You have to run that as sudo because only the super-user can change ownership of files owned by root. sudo escalates your privileges to allow you to change those things.
I realise this is kind of old now, but this may help people in future:
rbenv-sudo is a plugin for rbenv that allows you to run rbenv-provided Rubies and Gems from within a sudo session.
https://github.com/dcarley/rbenv-sudo
My answer in "Installing Ruby 2.0 and Rails 4.0.0beta on AWS EC2" might be useful to you.
In short, the root user needs to have rbenv loaded in its environment for you to use the gems installed by rbenv. This can be done by adding the following
# /etc/profile.d/rbenv.sh
export RBENV_ROOT=/usr/local/rbenv
export PATH="${RBENV_ROOT}/bin:$PATH"
eval "$(rbenv init -)"
This should be sufficient for sudo to work. If you are writing a shell script, you might need to use
. /etc/profile.d/rbenv.sh
before using executables from other gems.
My answer is a little bit late but I have simple solution for this issue. Use symbolic links to be able use binstubs and other Ruby stuff.
ln -s ~/.rbenv/bin/rbenv /usr/local/bin/rbenv
ln -s ~/.rbenv/shims/* /usr/local/bin
I hope that helps to other users who is having the same issue.

RVM causing path issues with installed gems on Ruby 1.9.2 - unable to annotate, etc, do I need to reinstall RVM?

I'm on Mac OSX Snow Leopard. I tried to post a similar question to the RVM Google group but it did not seem to get posted.
I'm worried that I've done something fundamentally wrong with my RVM install that's causing these errors, that seems to be related to paths, at each step of the way. Have any of you seen this behavior before?
I started to teach myself Rails programming as of about two months ago with a working environment of Ruby 1.9.1 and Rails 3.0.3, based on a hivelogic install tutorial that had me modify my ~/.profile file and install the relevant bits to ~/usr/local/src/. For reference, the line in my ~/.profile file was this when I installed RVM, if that makes any difference:
export PATH="/usr/local/bin:/usr/local/sbin:/usr/local/mysql/bin:/usr/local/src:$PATH"
In my terminal I installed RVM as a user using the standard user github bash script.
I tried to install Ruby 1.9.2, which kept running into a weird error about a libfile somewhere. After much Googling I found someone on Stack Overflow that recommended renaming the ~/usr/local directory while performing the Ruby install -- I did this, and the install complete.
Then I did gem install rails and tried to do bundle install in my app, which gave an error when trying to install the SQLite3 gem (even though I already had SQLite3 installed and working). Again, I spent a day Googling this and eventually found "Unable to install sqlite3-ruby gem" that said if I used Macports to sudo port install sqlite3 it would work.
I tried that from the base directory, and Macports did its thing but it didn't fix the problem. Then I did the same thing from my app directory and it fixed the SQLite3 error I was getting.
Now I am able to run rails server and rails generate again, which is great, but then I tried to "annotate" my new model, and I get this error: http://pastie.org/1481570
I have not yet solved this issue, and have looked at many threads of similar issues. This, for example, did not solve my problem: https://github.com/james2m/annotate_models/commit/5997da9692c9b222e8d1be22dfad6ed8638c16a1
I even tried copying my source code directly into the rvm/user/ directory in case that relative path was causing problems, but it doesn't seem to have fixed anything. Maybe I need to uninstall RVM and re-install it as root instead of a user-level thing?
What do you think is the best way to get annotate to work and hopefully get RVM to play nice with my gems going forward?
I'm unfortunately REALLY new to terminal, code, etc, so any help would be much appreciated.
On Snow Leopard you should modify either ~/.bashrc or ~/.bash_profile, preferably the later. Also, RVM will not need anything in ~/usr/local since it's entirely self-contained in ~/.rvm.
RVM uses a nice little shell function to sense the needed directories and desired default Ruby. I suspect either the instructions you followed were very out of date, or poor recommendations. The current RVM installation requests you add:
[[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm" # This loads RVM into a shell session.
to your ~/.bash_profile
The RVM site has lots of troubleshooting tips for things like MySQL. I'd strongly recommend backing out of the things those other tutorials had you do, and refer to the instructions on RVM's site. It is very easy to get things working right if you do it the RVM-way.
Download and install Apple's latest version of XCode from their Developer site if you haven't already. There have been some broken versions shipped on the DVDs.
Install. In particular follow the "Post Installation" section.
Following that, do whatever rvm notes says to do as far as libraries. Following that, you should be able to use rvm info to gather useful info about your installation. It is your best friend.
Database integration will point you to how to fix MySQL's wagon.
RVM development happens fast. Keep it updated, at least once a week using rvm get head.
At that point you should be in a good place to start reinstalling gems.

Resources