Use RVM Ruby instead of MAMP Ruby - ruby

I've used RVM to install the latest stable version of Ruby, however, I can't set it to be the default version. Running which ruby always returns the path of Ruby installed with MAMP on my system eg /Applications/MAMP/Library/bin/ruby
I've tried various rvm commands with no effect. I'd rather not have to run an rvm use command every time I open my terminal.
I'm using oh-my-zsh and iTerm2 if it makes a difference.

As Dave Newton suggested, this smells like a PATH issue, but in the case of MAMP, it's also an alias issue.
If you open ~/.profile and ~/.zprofile, in one of them you will probably see these lines:
alias erb='/Applications/MAMP/Library/bin/erb'
alias gem='/Applications/MAMP/Library/bin/gem'
alias irb='/Applications/MAMP/Library/bin/irb'
alias rake='/Applications/MAMP/Library/bin/rake'
alias rdoc='/Applications/MAMP/Library/bin/rdoc'
alias ri='/Applications/MAMP/Library/bin/ri'
alias ruby='/Applications/MAMP/Library/bin/ruby'
alias rails='/Applications/MAMP/Library/bin/rails'
You'll want to remove them all, save the file, then quit and restart iTerm2.
If you don't know how to open and edit dotfiles, read my guide that explains various ways to read and edit dotfiles on a Mac.
If removing those aliases doesn't fix it, then it's a PATH issue.
For your Mac to know about a command or other executable program, it has to be told where to look for it. It wouldn't be efficient for the computer to search the entire hard drive for the program.
Instead, it looks in a specific list of locations, which are stored in an environment variable called PATH, separated by a colon. You can view this list by running this command in your terminal:
echo $PATH
When you install new programs, such as Ruby, they might get installed in a location that is not already included in the PATH. If you don't add this new location to the PATH, the computer won't know to look for it there, and so it thinks it doesn't exist.
Similarly, if the location of the new program did get added to the PATH, but you have another location for the same program earlier in the PATH, then it will always use the first one it finds.
Most Ruby version managers use a script to automatically update the PATH, and they instruct you to add a line to your shell file to call that script, or they might add it for you. That line should come after any modifications of PATH.
It's been a while since I've used RVM (I no longer recommend it), but I think it's supposed to automatically add this line to your shell file:
source $HOME/.rvm/scripts/rvm
Assuming you installed RVM properly, here's what I would try:
Open your shell file. It should be ~/.zshrc if you're using oh-my-zsh.
Look for any lines that start with export PATH=, and if they mention /Applications/MAMP/Library/bin/ruby, remove that directory from the PATH. Also make sure any PATH lines come before the lines added by RVM.
Quit and restart iTerm2
If you don't see any RVM-related lines in ~/.zshrc or ~/.zprofile or ~/.profile, then RVM was not properly installed.
If you really want to use RVM, try uninstalling and reinstalling it. If all you care about is having a working Ruby environment, I would recommend chruby and ruby-install. You can install them by following my step-by-step guide to install Ruby on Mac.

Related

Redoing links in bash terminal

So I just installed emacs version 24 on OS X 10.8 by means of brew package manager, it installed in /usr/local/Cellar/emacs. I regularly use the builtin version of emacs version 22, but its in /usr/bin.
So there are two things I'd like to know how to do
Change it so that the first place that bash looks for is in
/usr/local/Cellar and their bins
Just change it so that the bin for emacs in /usr/bin calls the bin for
emacs in /usr/local/Cellar/emacs/bin.
These are two separate but related questions.
EDIT: So I did a hack job and just edited the .bash_profile and did an export for /usr/local/Cellar/emacs/24.3/bin ..but still would like to know how to get one bin to call another one.
It's probably better to not touch anything in /{usr/,}{s,}bin. You can use homebrew versions in bash by adding something like this to .bash_profile:
export PATH=/usr/local/bin:/usr/local/sbin:/usr/bin:/bin:/usr/sbin:/sbin
Putting /usr/local/bin and /usr/local/sbin first in /etc/launchd.conf might have some unexpected side-effects. For example in TextMate many commands stop working if env ruby is Ruby 2.0 or 1.9.
Or for example set PATH to ~/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/local/sbin and add symlinks to ~/bin/. See this answer.

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.

Can't get ruby 1.9.3 installed on OSX Mountain Lion

I have home brew installed and rbenv. I've followed these instructions:
http://octopress.org/docs/setup/rbenv/
Everything seems to work flawlessly but when I type
ruby -v
it still says 1.8.7...
Not sure what I'm doing wrong.
Unix will run the copy of Ruby that it finds first in your search path. Type these commands in Terminal:
echo $PATH
which ruby
The first will show you the order in which Unix is looking for the ruby command. The second will tell you which ruby it's finding. You may need to edit the PATH= command in your .bash_profile or .bashrc to change the order of the search path, e.g.:
PATH=/usr/local/bin:/usr/local/sbin:$PATH
prepends /usr/local/bin and /usr/local/sbin to your existing PATH to tell Unix to search your /usr/local/bin directory before /usr/bin
I have tried MacPorts and Homebrew many times the last couple of years. When it comes to ruby no other system can match RVM.
There are numerous guides online. Google, pick one and go.
NOTE: Octopress bloging system, comes with an rvm-ready bundle which will install and use ruby-1.9.2x, last time I checked :-)

What is the purpose of the RVM binary if it can be run as a function?

I've recently started using Ruby and was told to look into using RVM. I'm currently trying to understand how it operates but as far as I can tell from the website it can be run either as a binary or as a function in the shell by modifying .bash_profile.
What are the binaries for? I noticed they got installed to ~/bin, which felt sort of messy, and they did not seem to be invoked when using the rvm command. Are the binaries even needed when running RVM as a shell function?
It is normal and desirable for users of unix to put binaries in ~/bin (you would normally add that to your $PATH), so don't feel like it's messy to install stuff there.
That said, ~/bin/rvm is a script that will let you run rvm commands (like install), but is unable to edit your shell's environment (like all programs). Commands that need to edit the environment (like use), would not be possible without the rvm function. Not being super familiar with rvm, I am guessing the ~/bin/rvm exists for people that don't want to include the rvm stuff in all their shells. AFAICT, you don't need any of the stuff rvm puts in ~/bin if you are going to alter your ~/.bashrc as recommended.

How does MacPorts install packages? How can I activate a Ruby installation done via MacPorts?

After trying to install ruby19 on my machine (PPC, Mac OSX 10.5.7) using the following commandline
sudo port install ruby19
the version of ruby didn't change
ruby -v => ruby 1.8.6 (2008-08-11 patchlevel 287) [universal-darwin9.0]
I assume that i have two versions of it installed on my mac, but how do i use the latest one now?
By default, the Ruby 1.9 port in MacPorts installs the Ruby binary in /opt/local/bin/ruby1.9. It appends a 1.9 to avoid stomping on Ruby 1.8.7 libraries and gems, since not all gems are compatible with 1.9 yet. So you have to launch Ruby 1.9 with ruby1.9 (and irb1.9, etc.)
If you don't want to have to do this, you have two options:
Alias ruby to ruby1.9 in your shell config file.
Install the Ruby 1.9 port with the +nosuffix variant. Be warned, however, that if you have installed Ruby 1.8 via MacPorts, installing Ruby 1.9 via MacPorts without the 1.9 suffix may cause conflicts (with gems, etc.).
To use a specific ruby version if you have two versions installed you can either specify an absolute path to the one you want. E.g. /your/path/to/ruby Or you can change your PATH setting in your .profile
you can type
which ruby
to see the path to the ruby executable that is used at the moment.
using
echo $PATH
You can see the current PATH setting. You have to prepend the path to your new ruby binary to the PATH so that it will be found before the other one.
As ayaz already mentions, the default location of your macports stuff is in /opt/local. If you add /opt/local/bin in front of your path it should be fine. (Make sure to start a new terminal window after the change - they will not be picked up in your current session unless you explicitely 'source' the .profile file again)
One note of caution: after prepending /opt/local/bin to your path the shell will always prefer binaries in there to binaries found later, this can be an issue if you depend on specific versions in /bin, /sbin or /usr/sbin -- depending on your situation this means that you should not do it (if your computer is processing sensitive data and/or in a bank or something) or just have to remember that it could be an issue (if your computer is a normal development machine).
See http://www.tech-recipes.com/rx/2621/os_x_change_path_environment_variable/ if you need some more hints on how to set your PATH on osx.
Just a quick clarification about MacPorts. Ayaz is right that, by default, MacPorts will install things in /opt/local. (This makes it easy to globally uninstall later, if you want, and it keeps MacPorts packages out of the way of OS X packages.)
When you install MacPorts, it will normally edit your $PATH (and your $MANPATH) for you by updating your user's .profile (creating it, if it doesn't already exist).
As a precaution, the installer will create a backup of the original .profile in case you want to roll back the changes (or if you completely uninstall MacPorts later). Here's an example from a random machine at work.
admin ~ $ ls .profile*
.profile .profile.macports-saved_2009-08-03_at_14:55:56
If you look in .profile you should see something like this:
##
# Your previous /Users/admin/.profile file was backed up as /Users/admin/.profile.macports-saved_2009-08-03_at_14:55:56
##
# MacPorts Installer addition on 2009-08-03_at_14:55:56: adding an appropriate PATH variable for use with MacPorts.
export PATH=/opt/local/bin:/opt/local/sbin:$PATH
# Finished adapting your PATH environment variable for use with MacPorts.
# MacPorts Installer addition on 2009-08-03_at_14:55:56: adding an appropriate MANPATH variable for use with MacPorts.
export MANPATH=/opt/local/share/man:$MANPATH
# Finished adapting your MANPATH environment variable for use with MacPorts.
If your $PATH hasn't been updated, you should adjust it, since otherwise, you will have trouble using the port tool and the software you install via MacPorts.
I am inclined to think that macports usually keeps all of its stuff inside the /opt/local directory. I am using Leopard, and I have it inside that directory. You may want to look in there, particularly inside /opt/local/bin, to find the ruby binary you are looking for.

Resources