Multiple Ruby and Rails Versions on OS X Yosemite - ruby-on-rails

I'm getting started with Rails development and have a dev environment setup on OS X Yosemite, but the Ruby and Rails versions are a bit wonky. I used homebrew to install Rails, and everything seems to have gone well. Although which rails claims it's located in "~/.rbenv/shims/rails", and not /usr/local/bin where homebrew is supposed to place its apps. There's also /usr/bin/rails which when run yields:
Rails is not currently installed on this system. To get the latest
version, simply type:
$ sudo gem install rails
You can then rerun your "rails" command.
rails --version returns Rails 4.2.0.
I can create a rails app fine, but when I run rails server the ruby version is ruby 2.1.5 (2014-11-13) [x86_64-darwin14.0].
I followed these instructions (mac-dev-env.patrickbougie.com/ruby-22/) to compile ruby 2.2.0 and ruby --version returns "ruby 2.2.0p0 (2014-12-25 revision 49005) [x86_64-darwin14]". which ruby returns "/usr/local/rbenv/shims/ruby".
Relevant lines in .bash_profile from brew install of Rails and compiled ruby 2.2.0 are:
if which rbenv > /dev/null; then eval "$(rbenv init -)"; fi
export PATH=/usr/local/openssl/bin:$PATH export
MANPATH=/usr/local/openssl/ssl/man:$MANPATH export
PATH=/usr/local/rbenv/bin:$PATH export RBENV_ROOT=/usr/local/rbenv
eval "$(rbenv init -)"
How do I get the 2.2.0 version of Ruby to run with Rails? Everything I have read thus far suggests rvm (rvm.io). However, I get homebrew errors when I try to install Ruby that way.
From: http://railsapps.github.io/installrubyonrails-mac.html, section: "If You Already Have RVM Installed"
$ rvm get stable --autolibs=enable
$ rvm install ruby
$ rvm --default use ruby-2.2.0
rvm install ruby throws the homebrew error.
rvm seems unnecessary considering Ruby 2.2.0 is compiled on my machine already.
I appreciate any help, thank you!

Related

ruby -v says no such file or directory in mac os

I had the original ruby v2.6.8 I believe on my mac os. I wanted to upgrade to ruby 3.1.2 to get access to rails.
I installed ruby via homebrew, however I decided it woud be easier to manage using rbenv. I went to uninstall ruby via homebrew, and then instaled ruby 3.1.2 with rbenv.
Now when I use the
ruby -v
-bash: /usr/local/opt/ruby/bin/ruby: No such file or directory
using the command which ruby brings me this:
/Users/user/.rbenv/shims/ruby
So the ruby is there, and rbenv claims it installed ruby 3.1.2, but did homebrew install all of ruby including the version osx came with? Im pretty sure thats a yikes moment if it did. I also can't use any ruby commands under these circumstances so I don't really know what to do from here?
How do I get my $PATH to recognize the rbenv path to ruby as the global path? I'm on MacOs 11.3.1 BigSur if thats relevant.
Follow the next steps to set up rbenv, and ruby 3.1.2.
Step 1
brew install rbenv
Step 2 Add the following lines to your .zshrc file
export PATH="$HOME/.rbenv/bin:$PATH"
eval "$(rbenv init - zsh)"
Step 3
brew update && brew upgrade ruby-build
Step 4
rbenv install 3.1.2
rbenv global 3.1.2

Rails is not currently installed on this system - High Sierra, rbenv

To start I follow the exact steps from Setup Ruby On Rails on
macOS 10.13 High Sierra, just like I have with every other version of mac/ubuntu.
So basically install homebrew, install rbenv ruby-build
add the following to bash profile and source it:
if which rbenv > /dev/null; then eval "$(rbenv init -)"; fi
then do
rbenv install 2.5.1
rbenv global 2.5.1
ruby -v
outputs:
ruby 2.5.1p57 (2018-03-29 revision 63029) [x86_64-darwin17]
do a which ruby to to make sure:
/Users/pdavies/.rbenv/shims/ruby
run gem install
gem install rails -v 5.2.0
which outputs:
Successfully installed rails-5.2.0
Parsing documentation for rails-5.2.0
Done installing documentation for rails after 0 seconds
1 gem installed
then a rehash and check version:
rbenv rehash
rails -v
outputs:
Rails is not currently installed on this system. To get the latest version, simply type:
$ sudo gem install rails
You can then rerun your "rails" command.
found anther issue that suggested adding:
export PATH="$HOME/.rbenv/bin:$HOME/.rbenv/shims:$PATH"
eval "$(rbenv init -)"
to the bash profile, did that sourced it and tried again still nothing. to check the output of echo $PATH was:
/Users/pdavies/.rbenv/shims:/Users/pdavies/.rbenv/bin:/Users/pdavies/.rbenv/shims:/usr/local/bin:/usr/bin:/usr/sbin:/bin:/sbin
Just to make sure i also did which gem incase i wasnt using the right one, output:
/Users/pdavies/.rbenv/shims/gem
when I do a which rails i get:
/usr/bin/rails
I found some blog that said to delete that, but I cant seem to do that either, i just get:
sudo rm -f /usr/bin/rails
Password:
rm: /usr/bin/rails: Operation not permitted
This is a fresh install of OSX and the only way I can get it work is to downgrade the version of OSX that is installed, which is a pain in the ass!
Any help would be grateful
Thanks

Ruby version issues with Rbenv

I am setting up my new machine (Mac Yosemite) and when I run the command "bundle" I get the following error:
Your Ruby version is 2.2.1, but your Gemfile specified 2.2.0
I'm trying to change the version w Rbenv but I think it's not working.
ruby -v
// Ruby 2.2.1p85
rbenv version
// 2.2.0
Can anyone help me? Thanks in advance!
Try to use rbenv local [version number].Refer to this for more understanding.
I had a similar problem but with ruby -v 2.2.0 and Mac OSX El Capitan. What finally worked for me was:
gem install bundler (apparently it was no longer installed)
rbenv rehash
bundle install
I was able to find the answer on the github for rbenv.
https://github.com/sstephenson/rbenv/issues/697
Assuming you have Ruby -v 2.2.0 installed (rbenv install 2.2.0 otherwise), change your ruby version with rbenv:
rbenv global 2.2.0
after install rails you must do this: rbenv rehash, in order to tell rbenv to see rails as executable.
follow this tutorial to set up Ruby and Ruby on Rails in your machine: https://gorails.com/setup/osx/10.10-yosemite
Note: In my case. after install rbenv the command rbenv global 2.2.0 doesn't affect my current ruby version (ruby -v still be the previous one). I fix this issue reinstalling rbenv and run this command:
# Add rbenv to bash so that it loads every time you open a terminal
echo 'if which rbenv > /dev/null; then eval "$(rbenv init -)"; fi' >> ~/.bash_profile
source ~/.bash_profile
immediately afterwards, as explained above in the tutorial.

Rails is not using my global Ruby version

I want to use Rails with Ruby 2.1.0, but it's using Ruby 1.9.3 (the system's version).
I'm using rbenv to manage my Ruby versions. My steps were something along the lines of:
$ rbenv install 2.1.0
$ rbenv global 2.1.0
$ sudo gem install rails -v 4.0.2
$ rbenv rehash
$ rbenv versions
system
* 2.1.0 (set by /home/dennis/.rbenv/version)
$ ruby -v
ruby 2.1.0p0 (2013-12-25 revision 44422) [x86_64-linux]
$ rails new app && cd app
$ rails server
Rails is using is Ruby 1.9.3 (x86_64-linux), according to localhost:3000/rails/info/properties. A log message from rails server gives: INFO ruby 1.9.3 (2012-04-20) [x86_64-linux].
I think Rails is using the system version of Ruby because the versions match.
$ rbenv local system
$ ruby -v
ruby 1.9.3p194 (2012-04-20 revision 35410) [x86_64-linux]
$ rbenv local --unset # Unset local Ruby version, go back to 2.1.0
Some things I've tried with no luck:
setting the local and shell versions of Ruby to 2.1.0 with rbenv
rehash rbenv shims (rbenv says to do this after installing executables)
made new Rails projects after switching to 2.1.0 just in case I made the projects while using 1.9.3
putting 2.1.0 in a .ruby-version file in the root of my project (suggested by #Agis)
restarting terminal session and starting a login bash shell (suggested by #Russel)
specifying the desired Ruby version in the project's Gemfile (suggested by #rlecaro2)
FWIW, I'm using Ubuntu 13.10 with the fish shell.
Sounds silly, but did you restart terminal session?
Otherwise try and type
/bin/bash --login
You didn't tell how you installed rbenv, but I think it is per-user installation (which is default). In this case you should install gems without using sudo. When you did sudo gem install rails, it was installed in system ruby, not rbenv's selected one.
Solution - install rails without sudo:
rbenv global 2.1.0
gem install rails
rbenv rehash
Try creating a .ruby-version file in the root of your project with the following contents:
2.1.0

"rails s" using Ruby 1.8.7 instead of Ruby 1.9.3 (OS X 10.7.2)

Odd problem, trying to figure out what is going on here with my fresh install.
I installed Ruby 1.9.3 using homebrew. I installed Rails 3.2.0 using ruby gems. I then created a test application with rails new test_app to make sure everything looked okay in the test environment. Somehow, the rails environment is using 1.8.7 instead of 1.9.3.
When doing a ruby -v, I get this:
overcast:test_app joe$ ruby -v
ruby 1.9.3p0 (2011-10-30 revision 33570) [x86_64-darwin11.2.0]
But when running rails s I get this:
overcast:test_app joe$ rails s
=> Booting WEBrick
=> Rails 3.2.0 application starting in development on http://0.0.0.0:3000
=> Call with -d to detach
=> Ctrl-C to shutdown server
[2012-01-21 23:21:35] INFO WEBrick 1.3.1
[2012-01-21 23:21:35] INFO ruby 1.8.7 (2010-01-10) [universal-darwin11.0]
And, obviously, when looking at the rails environment webpage at localhost:3000 I am showing 1.8.7 instead of 1.9.3. I've edited /etc/paths to have /usr/local/bin at the top based on some answers I've found here, but it doesn't seem to make a difference.
Any help would be greatly appreciated!
Addendum: The same situation happens when installing and attempting to use Ruby through RVM.
If you are running mac os x you have ruby 1.8 preinstalled. Use RVM or rbenv if you want to have more than one ruby version installed.
Homebrew not so bad :)
You may use rbenv. It is ruby version manager.
My success story for MAC OS X 10.8.2 is:
install Homebrew
change PATH in /etc/paths (/usr/local/bin need to be first)
install rbenv and ruby-build (it is plugin for rbenv) using homebrew
create ~/.profile with content:
export RBENV_ROOT=/usr/local/opt/rbenv
if which rbenv > /dev/null; then
eval "$(rbenv init -)"
fi
install last stable ruby version using rbenv install 1.9.3-p374
change default ruby version using rbenv global 1.9.3-p374
update gem using gem update --system (optional)
install rails gem install rails
...
Profit :)
bundle exec rails server
Should work just fine. It also worked for me to close the terminal and open a new one.
if you have multiple versions of ruby, I recommend you use RVM
Install it https://rvm.io/rvm/install
Use: https://github.com/wayneeseguin/rvm#usage
To view all versions of ruby use "rvm list"
and set default version "rvm --default version"
sudo update-alternatives --config gem
and select your verison!

Resources