Managing ruby in different locations on development and production servers - ruby

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.

Related

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.

How to load the RVM part of user's .bashrc to run Ruby CGI scripts under Apache?

I've configured a new server on Ubuntu 12.04 and I started to use RVM.
I've installed RVM under my user (as myself, not as root with sudo) by following the Ryan Bigg's guide, with no previous system-wide installed Ruby. So, I didn't have any Ruby under /usr/bin. My first task then was to replace the shebang line of all my CGI scripts, from
!#/usr/bin/ruby
to
!#/usr/bin/env ruby
However my scripts didn't run under Apache. In the terminal I could run them (by typing ./index.cgi, for example), but not over a browser. A relevant note: in both the user is the same, i.e., the Apache user is the same as the one logged on terminal. Through php tests, I've checked the RVM enviroment (last lines of .bashrc) was not loaded under Apache.
I saw this tip for running CGI scripts with RVM, which suggests to put the complete path of specific version of Ruby in the shebang line. That can be useful if you have scripts which run on different versions of Ruby. But that solution doesn't work for me, because my scripts must run on different machines, with different users and different paths.
The solution which works for me is to put a symlink of the desired Ruby version under /usr/bin:
sudo ln -s /home/apache_user/.rvm/rubies/ruby-1.8.7-p370/bin/ruby /usr/bin/ruby
But I want to know if there's a better solution, because I guess that rvm --default use is better than sudo ln -s.
I am thinking about:
loading the RVM Environment on startup (but I don't know how to achieve that);
loading the RVM Environment for each web request (which can degrade performance, and I don't know how to configure Apache to do that);
maybe the RVM Environment is loaded and all I must do is to guess the name of relevant variables to pass with PassEnv directive. But I doubt that. (Why Apache would run the .bashrc instead of another shell like csh or ksh?)
you can source the ruby environment, I'm not sure if it's enough to source it in $HOME for apache or if you need to modify /etc/init.d/apache2, but the line is:
source /path/to/rvm/environments/<name>
where for <name> you can either use full ruby name or an alias name
You can create aliases with:
rvm alias create veve 1.9.3-p125#my-project
which for RVM installed in /home/app/.rvm will allow you to use:
source /home/app/.rvm/environments/veve
in .bashrc or /etc/init.d/apache2 (just near top, bellow shebang).
you could always do
sudo ln -s /usr/local/rvm/rubies/default/bin/ruby /usr/bin/ruby
That will link the default version of the rvm-managed Ruby to /usr/bin/ruby and you will never have to do anything. set it and forget it.

Rbenv for production with passenger

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.

Upgrading Ruby on Mac OS X

I am reading the book Programming Ruby and am looking to upgrade the version of Ruby on my computer. I run Mac OS X Snow Leopard and doing ruby -v in the command line shows I am running Ruby 1.8.7.
I installed MacPorts and ran the sudo port install ruby19 command but whenever I type ruby -v, it still shows Ruby 1.8.7.
Could anyone help with this? Thank you.
The tool "rvm" is pretty good. It stands for Ruby Verson Manager I think, and the developer is good at support in the irc channels. You can install multiple versions of ruby along side each other pick the ones you want to use.
http://rvm.io
You have to use ruby19 to run ruby if you installed it that way. You can also use irb19, and ri19.
I updated the link to RVM in one of the answers, but also a quick poll of some friends doing Ruby dev currently revealed a preference for rbenv as a ruby environment manager: http://github.com/sstephenson/rbenv/
As mentioned in hvgotcodes answer, RVM seems to be a great way of handling this.
I recently started learning Ruby (and Rails) myself, and this[1] tutorial has a great section on using RVM. I'd highly recommend you give it a go, it's especially handy so that you can follow different tutorials on different versions of Ruby if you wish.
[1] http://railstutorial.org/book#sec:rubygems
As #Andrew Grimm eluded, the problem you are having is likely due to a problem in your $PATH variable. Since macports typically installs stuff in /opt, modifying your $PATH variable to:
$ export PATH=/opt/local/bin:$PATH
Will most likely help. However, I would urge you to look into RVM as #hvgotcodes recommends.

RubyGems via MacPorts a good idea?

Upon upgrading from a MB to a new MBP, Apple's migration assistant didn't move my gems.
I'm installing rubygems via macports this time, hoping to avoid this upon next upgrade.
Are there any pitfalls I should be aware of?
If you want your gems installed in your home directory (which should copy over during a transfer, and, as a side benefit, will let you run gem install as yourself, rather than root), set the gemhome: key to a path in your home directory in your ~/.gemrc.
Where were your gems installed? The migration assistant only moves files in certain areas; I'm not sure if it copies files from places like /usr/local or /opt/local, you may have to do that manually (in which case, you can just copy the entire tree over to your new machine).
That said, you shouldn't have a problem installing gems using the version of RubyGems installed by MacPorts.
Installing a package manager via a package manager seems like it should be wrong.
Assuming you are not going to use the system ruby, and will only be using ruby installed via MacPorts; it should not be a problem.
If you plan on using gems with system ruby, you might have problems when Apple upgrades the system version of ruby, unless you are installing your gems in /Library/Ruby/Gems/
I'm going to assume you can just copy the gems to /Library/Ruby/Gems/1.8/gems from /opt/local/lib/ruby/gems but I wouldn't count on it working 100% with every gem.
The migration assistant doesn't move a lot of unix level stuff. This will be true of the macports installed gems as well (which typically live in /opt/local/lib/ruby/gems).
I use macports gem, and this generally works fine with the macports ruby. Be sure you know which ruby executable your shell ends up using, and perhaps use symlinks to control it specifically. I prefer using the macports version so that I'm not beholden to Apple's update schedule. Sadly macports iteself is somewhat inferior to package managers offered on bsd/linux distributions.
I would say don't use MacPorts. People that use their operating systems version of RubyGems often end up with an out of date gem version and then run into problems when they try to update their version of rubygems. Installing Rubygems from source is easy, and it's not hard to install gems when you get a new machine, I mean how often does that really happen????
I just installed ruby 1.8.7 with macport and when I launch scripts with gems, it complains that it doesn't know them.
Anyway to add cleanly the gems in the path of my macport ruby?
Gam
PS: I installed the gems via system gem
What no one mentioned rvm?
Use rvm to manage all of your ruby issues - seriously!

Resources