I have too many versions of Ruby and Rails - ruby-on-rails

I have both ruby and rails installed in:
/usr/local/bin/ruby
/usr/bin/ruby
/usr/local/bin/ruby
This is causing problems when I try to run certains commands like ruby script/console
How do I remove some of these installations of both ruby and rails?

RVM will help you for sure. Check out RVM here

Preventing this kind of situations is the very role of RVM and it's gemsets.
I encourage you to check it out.

There are a few options. First one would be to get your ruby system install in good order. There is no harm in renaming removing those multiple ruby file in your diverse bin folders. Get the stable release of 1.9.2 and gem install whatever you need then. Googling around to fill the gaps you may have here
Quick and dirty you can do:
Get the latest stable release of ruby 1.9.2, compile it on your system, I suppose the ruby that is under /usr/bin is the system's and you are using macosx. The one located in /usr/local/bin is probably something you installed yourself and even maybe you don't even need to reinstall ruby 1.9.2. If that's so get you .bashrc in proper order by making sure /usr/local/bin comes before /usr/bin and you won't have to worry so much about which version you might be using. Create a ~/.bash_profile and put this in.
export PATH="/usr/local/bin:$PATH"
Expected output (more or less):
/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin:/usr/X11/bin:/usr/texbin
Others mentioned RVM. I used it, but I don't use it on a production server, I prefer a single sane ruby install to RVM. There is also rbenv and is seems much saner.
Final thought play around in your system remove symlink stuff, set your PATH variable or use RVM/rbenv.

Managing Ruby: Moving From RVM to rbenv
It's time to switch over, http://cantina.co/2011/10/08/managing-ruby-moving-from-rvm-to-rbenv/

Related

How to completely remove rvm from $PATH? Ruby gemfile specifity error

I know this is a very common question with hundreds of answers and guides however I believe my problem is unique.
After removing rvm and installing Rbenv I now get an error when running rails s or similar commands, the error reads Your Ruby version is 2.6.3, but your Gemfile specified 2.6.4
ruby -v = ruby 2.6.4p104 (2019-08-28 revision 67798) [x86_64-darwin19]
rails -v = Your Ruby version is 2.6.3, but your Gemfile specified 2.6.4
my $PATH: /Users/EthanMorganClark/.rbenv/shims:/Users/JohnDoe/.rbenv/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Users/JohnDoe/.rvm/bin
Now I can see .rvm/bin is still there, and it shouldn't. This leads me to believe it could be the source of my ruby version problem.
My process:
I ran rvm implode and then gem uninstall rvm, then I went into .bashrc, .profile and .bash_profile and removed any traces of rvm. (I did the same with zsh, although I currently am using bash)
I also then ran this script found on the second answer, but in the end it also thinks anything rvm related it gone.
edit - I have also deleted the gemfile.lock, bundled and then get the same error.
Any suggestions, links or answers would be much appreciated. Thanks.
Your PATH will be used in-order, so having .rvm/bin at the end shouldn't really affect anything. That being said, figuring out how/where your PATH is modified can definitely be tough if you haven't been keeping your .bashrc/.zshrc/.bash_profile/.zlogin etc organized.
A high level list of things you should do:
find out what shell you're using, bash or zsh, etc
look in all your corresponding rc files for the respective shell for the culprit (try searching all .* files in ~)
use which -a ruby/which -a rails/etc to see which executable is invoked when you call it
check out this answer for a suggested way to manage your ruby & gem environment

I get a bash error message when I run "bundle install" after re-installing ruby

I removed the previous version of ruby I installed, ruby 1.9.1 to be precise, on my Ubuntu, and installed version 2.2.3. When I try to run bundle install on my terminal, I get this error
bash: /usr/local/bin/bundle: /usr/bin/ruby1.9.1: bad interpreter: No such file or directory.
Can anyone advice on how to fix it?
How did you remove the old version of Ruby?
It looks like it left the binary executable for bundler, so I'm guessing you just straight up deleted the usr/bin/ruby folder? Ruby installations (and most installations) tend to create executable binaries in other folders that need to be deleted as well.
I would highly recommend that you use something like rvm or rbenv to manage multiple versions of ruby on your system. Those tools are designed to do that sort of thing and are basically industry standard at this point.
My personal recommendation is rvm, although either is really fine. If you choose to go with that, check out rvm.io for installation instructions.
It'll install rvm with the latest ruby version. In addition to that you are free to install any other version of ruby or patch that you prefer, and you can easily switch between the two using rvm use <ruby version>

Shell script to switch between MRI and jruby

Working on a project which uses jRuby, but locally when I work I often use MRI because it's faster.
Now switching between to is a major annoyance, this is how I switch from jruby to MRI:
rvm use ruby-2.1.5
sed -i.bak 3s/.*/ruby" '2.1.5'"/ Gemfile
Which replaces my Gemfile and specifies the right version of ruby. Now I wanted to create two executables from bin/ folder of my rails project, one called mri and another one jruby so I could switch back and forth between the two using a single command, and so that everyone who work in a team can use the same.
Problem with this is that, when the shell executes, the RVM doesn't change the ruby version in my current session. So naturally I get this message :
Your Ruby version is 2.0.0, but your Gemfile specified 2.1.5
Is this a good approach to what I'm trying to do?
pretty sure I'm not the first person trying this
Do you already have something that works for you?
As the comments pointed out, you need to re-source the rvm scripts.
You probably could just source ~/.bash_profile or ~/.bash_rc again, as they should be sourced there.
That being said, I haven't had this problem with rbenv, but I only swap between MRI versions.
There is also a spec for a .ruby-version file see here that both RVM and rbenv honor.
For bundling bin stubs you can specify a bin path with bundle binstubs --path mri-bin.

How do I install Ruby gems when using RVM?

I set up RVM and used it to install Ruby and a few other libraries. As I was going through various tutorials and set-ups of other technologies like Rails, I began getting confused about what I should do via RVM and what I should just do as the tutorials suggest.
One example is the RubyGems tutorial here:
http://rubygems.org/pages/download
Should I download that tar file they are talking about? Seems unnecessary since that is what I thought RVM was for. Do I even need RubyGems? What is that for really?
Also, how do I actually get Rails? Is there a precise RVM command to actually download and install Rails?
It helps me to think of RVM as a layer of abstraction between you and the ruby ecosystem.
Without RVM: ruby, gems, and ruby related binaries (like rake, spec, gem, etc) are all installed directly into your operating system directories.
With RVM: ruby related stuff is intercepted by rvm so that ruby, gems, and ruby related binares are "installed" into ~/.rvm dir in a nice, clean, organized way. RVM sits between ruby, gems, and related binaries and the operating system. It provides a way to have multiple ruby environments (with different gems and binaries) on the same machine.
So, no matter whether you have rvm installed or not, you should be able to run the commands almost exactly(*) as they appear in any tutorials out there on the web. In other words, you can sort of "forget" that RVM is installed; the ruby ecosystem should work just as if it wasn't installed.
So, yep, you're gonna have to run gem install rails, etc.
Hope that helps clear the confusion.
(*) There are some small differences. For example: you shouldn't run commands as sudo when RVM is installed.
Should I download that tar file they are talking about?
No. Ruby 1.9+ includes gems. RVM retrofits it for 1.8+.
In general, be careful with any directions you find on the internet explaining how to install anything, unless you have enough experience to understand completely what they want you to do. In particular, any time they want you to install something using sudo or as root.
Specifically, when working with RVM, you do NOT want to use sudo to install Ruby, or any gem. RVM works by setting up a sandbox for your development, and relies on your account's environment, modifying your path so any Ruby requests go to the currently selected RVM-managed Ruby or gems or any commands they install. sudo pushes your normal environment to the side, substituting root's temporarily, installs whatever you asked it to do with root's permissions, then reverts to your environment.
When you go to run the command, or find the gem, as you, it can't be found by RVM's Ruby, because the file was installed outside RVM's sandbox, or, it can't be read or modified, because it's owned by root. Whatever the actual cause, the end result will be weeping and gnashing of teeth.
RVM doesn't subvert the gem functionality. gem is used to install and manage Ruby gems, and RVM tweaks it to use the sandbox for all its machinations. You get added functionality because of RVM's support of gemsets, but gem works as it always has, only it has "big brother", RVM, controlling its world.
No need to install rubygems. RVM should have already installed rubygems. RVM is (in my opinion) useful for managing different installations of ruby (say 1.8.7 and 1.9.2) or different gemsets. If you just have one version of ruby and don't care about different gemsets, RVM isn't really that much of a change. If you want to just install rails, just use gem install rails. If you have an existing rails 3 app, install bundler first gem install bundler and then bundle install to get rails and other gem dependencies.

Running Ruby from usr/bin instead of usr/local/bin?

So I was following the steps on Hivelogic to get ruby on rails setup on my machine. After compiling and installing, I used the which ruby command to double check it installed in the right place. It did not. I was able to install the Rails and MySQL gems though, but both POW and localhost:3000 don't work.
Is there anything wrong with having it in the usr/bin directory instead of usr/local/bin? My usr/local/bin is practically empty, but usr/bin has a ton of files in it.
Edit: I reinstalled ruby to usr/local/bin but now Rails won't install. Is it a problem that it's already installed in usr/bin?
That page is a few years old. The current best practice for Mac and Linux is to use rvm. You can see, even Hivelogic uses rvm now (though I'm not sure how I feel about installing it system wide).
Anything that recommends installing from source as the first option instead of a last-resort fallback is probably bad advice. It's better to install using a package manager like MacPorts or Homebrew both of which have current versions of Ruby 1.8 and 1.9.
MacPorts installs everything in /opt specifically to avoid conflict with system files. From time to time Apple will distribute an update that patches ruby and this can mess up anything you have in the way of dependencies if you've been using a modified /usr/bin/ruby. Generally the system ruby is supposed to be left as-is.
You can replace the "compile ruby" step with an installer like that and save yourself a lot of trouble in the future. Just be sure to have /opt/local/bin as one of the first items in your PATH just as you would for other solutions.
Since installing Ruby and Rails and some kind of database can be a confusing process, it seems like there should be a meta-installer to help you through the process.
So I finally managed to get rails installed in the correct spot. I had to change the line in my .profile to export PATH="/usr/local/bin:$PATH" and then do a sudo gem update --system after installing rubygems before rails could actually be installed.
RVM and Homebrew did not help at all.
EDIT: Since people have a problem with this answer, I'm giving an update. I have since installed RVM and am liking it, but only because I have a friend I ask all my questions to, as RVM is really confusing if you're not used to doing a lot on the command line. I needed help from him to install it because I didn't have any of the bash files, and thought they were hiding somewhere. If you're missing the bash files, just create them yourself. This took a long time to get working, but now I apparently have it setup right.

Resources