Two different ruby versions on mac? - ruby-on-rails

I am setting up a simple ruby on rails app locally. It seems I have two different versions of ruby on mac and I would like to only use one. When I ran bundle install, it says Your Ruby version is 2.7.1, but your Gemfile specified 2.6.3. Then, I changed the line ruby '2.6.3' in my gemfile to ruby '2.7.1'. With this it ran bundle install properly. However, when I run rails server it says Your Ruby version is 2.6.3, but your Gemfile specified 2.7.1.
Why is it saying two different values for my Ruby version?
How do I get it to only use one version of Ruby?
If its relevant, I am on a mac and installed ruby using homebrew. If I run ruby -v in the terminal it says ruby 2.7.1p83 (2020-03-31 revision a0c7c23c9c) [x86_64-darwin19]. I'm not sure why it says I have ruby 2.6.3.

There are to different versions because Mac OSX already includes one by default for system scripts (2.6). Homebrew install another one that never overrides o reemplace the System Wide version.
You are looking for a "Ruby Version Manager", are tools that allow you to install and use different versions of Ruby, even per project.
The popular ones are RVM and rbenv. Personally, i choose rbenv and I think that is the most widely used of both. Example of use:
# Install ruby 2.7
rbenv install 2.7.1
# Make ruby 2.7 the default version
$ rbenv global 2.7.1
# Or make 2.7 the default versiĆ³n only on a specific project
$ cd myproject
$ rbenv local 2.7.1
# this create a ".ruby-version" file
This webpage always have the most recent and easy to use tutorial for setup a Ruby environment, depending on the OS and version.
https://gorails.com/setup/osx/10.15-catalina#overview

You have two different versions of Ruby installed, because MacOS natively comes with a standard installation of Ruby.
You also have rails pointing to the system version of Ruby. That version is usually under /usr/bin/ruby. The Homebrew installed version of Ruby (which is what you want) is located under /usr/local/bin/ruby unless you specified a completely different root path to install your brew packages.
Running brew config will give you a short list of data about your Homebrew configuration. Among them is an environment variable called HOMEBREW_PREFIX, which should look something like this:
$ brew config
....
HOMEBREW_PREFIX: /usr/local
....
I recommend placing /usr/local/bin first on your PATH environment variable so that you can easily use your brew packages via the CLI:
export PATH="/usr/local/bin:$PATH"
You may also want to look into setting the following environment variables for whichever shell you are using (examples given):
RUBY_ENGINE=ruby
RUBY_VERSION=2.7.1
GEM_ROOT=/usr/local/etc/ruby-2.7.1/lib/ruby/gems/2.7.1 (alias for GEM_HOME)
gem env gives a lot of great information on how Gems is configured.

I I had this exact problem and managed to fix it by running this command:
CFLAGS="-Wno-error=implicit-function-declaration" rbenv install 2.6.7
Note - I needed that version (2.6.7) please change it to the one you need
I found this on this blog post here - https://dev.to/rbazinet/fix-installation-of-ruby-using-rbenv-on-macos-big-sur-3432

Related

Upgrading Rails with rbenv

I want to upgrade an application to the latest Rails version (4.2).
I'm using rbenv as a version manager.
Can I just install the rails 4.2 gem without affecting my other Rails applications?
thanks for your advice,
Anthony
rbenv is a tool for managing different ruby versions in your system. While it's possible to install different rails versions for a single ruby version installed using rbenv, installing rails is bundler's job.
Quoting from a really good answer by Nathan from here, where it's explained how would one install different rails versions for a single ruby version (go and upvote the answer if you find this helpful) :
So once you get rbenv installed, and you use it to install a specific
ruby version, you can install multiple versions of rails to for that
ruby.
STEP 1. Install whatever version(s) of rails you want per ruby version
% RBENV_VERSION=1.9.2-p290 rbenv exec gem install rails --version
3.0.11
By using the "RBENV_VERSION=1.9.2-p290" prefix in your command line,
you're specifying which ruby rbenv should be concerned with.
Then following that with the "rbenv exec" command, you can install
rails. Just use the version flag as in the example to specify which
version you want. Not sure if you can install multiple versions in one
shot, but I just run this command as many times as needed to install
each version I want.
Note: This will all be managed within your rbenv directory, so it's
perfectly safe and contained.
STEP 2. Build a new rails project by specifying the rails version you
want.
% RBENV_VERSION=1.9.2-p290 rbenv exec rails _3.0.11_ new my_project
STEP 3. Don't forget to go into that project and set the local rbenv
ruby version.
% cd my_project
% rbenv local 1.9.2-p290
And here is Michael's blog post explaining how one can manage different rails versions installed this way. You would basically modify the Gemfiles and specify the version you want, and then let bundler take care of installation. Here's the answer where he talks about this.
Also, I haven't tried rbenv-gemset but it looks promising at a glance. I would use it if I were you, at least to try it once. :)

Installing Ruby on Rails Application with Ruby Enterprise Edition 1.8.7

I want to install one Application on my CentOS 5.5 which requires Ruby Enterprise Edition 1.8.7 and Rails 2.3.5.
I have installed REE from rubyenterpriseedition.com. To install my application I need to do some settings.
In my .bash_profile I should change PATH = $PATH:$HOME/bin to
PATH=/opt/ruby-enterprise-1.8.7-2012.02/bin:$PATH:$HOME/bin
then to reload
source .bash_profile
To check whether changes have been reflected, we must run which gem. This has to output output:
/opt/ruby-enterprise-1.8.7-2012.02/bin/gem
But Unfortunately it is outputting
[root#local ~]# which gem
/usr/bin/which: no gem in (/opt/ruby-enterprise-1.8.7-2012.02/bin:/usr/kerberos/sbin:/usr/kerberos/bin:/usr/local/bin:/usr/bin:/bin:/usr/X11R6/bin:/home/college/bin:/root/bin)
After that when i do
[root#local ~]# gem update --system
bash: gem: command not found
I am getting above error.
But Look at the Actual PATH Below...
[root#local college]# echo $PATH
/usr/kerberos/sbin:/opt/ruby-enterprise-1.8.7-2012.02/bin:/usr/kerberos/bin:/usr/local/bin:/usr/bin:/bin:/usr/X11R6/bin:/home/college/bin:/root/bin:/opt/ruby-enterprise-1.8.7-2012.02/bin
Please help me out to install this application on my CentOS.
I think you are mixing up versions. I see both 2011.03 and 2012.02 mentioned. Please check which version you actually installed and make sure your PATH variable has this version as well.

How do you use multiple rails versions with rbenv?

Is it possible to use multiple versions of rails using rbenv (e.g. 2.3 and 3.1)? This was easy with gemsets in rvm, but I'm wondering what the best way is to do it now that I've switched to rbenv (also, I'm looking for a way to do it without rbenv-gemset).
not sure if you got an answer to this, but I thought I'd offer what I did and it seemed to work.
So once you get rbenv installed, and you use it to install a specific ruby version, you can install multiple versions of rails to for that ruby.
STEP 1. Install whatever version(s) of rails you want per ruby version
% RBENV_VERSION=1.9.2-p290 rbenv exec gem install rails --version 3.0.11
By using the "RBENV_VERSION=1.9.2-p290" prefix in your command line, you're specifying which ruby rbenv should be concerned with.
Then following that with the "rbenv exec" command, you can install rails. Just use the version flag as in the example to specify which version you want. Not sure if you can install multiple versions in one shot, but I just run this command as many times as needed to install each version I want.
Note: This will all be managed within your rbenv directory, so it's perfectly safe and contained.
STEP 2. Build a new rails project by specifying the rails version you want.
% RBENV_VERSION=1.9.2-p290 rbenv exec rails _3.0.11_ new my_project
STEP 3. Don't forget to go into that project and set the local rbenv ruby version.
% cd my_project
% rbenv local 1.9.2-p290
Now if you want to delete this project, just delete it as normal.
If you want to delete / manage a rails version from rbenv gems, you can use regular gem commands, just prefix your command line with:
% RBENV_VERSION=1.9.2-p290 rbenv exec gem {some command}
And of course, you can delete a complete ruby version and all its shims, etc that are managed within rbenv pretty easily. I like how self contained everything is.
Hope this helps.
For reference, this is a pretty good walk through of at least some of this stuff:
http://ascarter.net/2011/09/25/modern-ruby-development.html
There is a rbenv plugin called rbenv-gemset which should behave similar to the rvm gemset-command but since rbenv was never intended to work this way, I haven't tried it.
I usually manage Rails versions with Bundler as Nathan suggested in the comments of one of the other answers. I create a Gemfile with my desired Rails version, run bundle install, create the Rails application, let it replace the Gemfile and let Bundler take over:
mkdir my-rails-app
cd my-rails-app
echo "source 'https://rubygems.org'" > Gemfile
echo "gem 'rails', '3.2.17'" >> Gemfile
bundle install
bundle exec rails new . --force --skip-bundle
bundle update
If you want more detail, I wrote an article on my blog about it.
Hope it helps!
If you have setup ruby using rbenv the following will work.
Installing rails, the latest version (7.x as of Oct 2022)
gem install rails -v 7.0.2.4
# Find exe
rbenv rehash
To create a rails project with the latest rails version,
rails new project_1
This will create a rails application with the latest version, to verify we can see the rails version in the Gemspec file (or) see the logs during the installation,
Installing rails, 6.x.x.x version
Assuming we are going to install rails 6.0.4.8, then issue the following commands
gem install rails -v 6.0.4.8
rbenv rehash
Now, to create a rails project with 6.0.4.8 version (which is installed previously), specify the rails version along with the rails command.
rails _6.0.4.8_ new project_2
This will create a rails application with the 6.x version, to verify we can see the rails version in the Gemspec file (or) see the logs during the installation,
Other notes
Similarly, we can manage any no of rails versions in any number of
projects.
rbenv rehash Installs shims for all Ruby executables known to
rbenv
In this approach, you don't need to set or modify any ruby
environment variables.
You don't need to modify Gemspec file by yourself.
The instructions work as of Oct 2022.

ruby 1.9 and 1.8.7 installed how do I make app use 1.8.7

I have two version of ruby installed 1.9 and 1.8.7(via macports) how do I make my app run with 1.8.7?
And How can i uninstall 1.9?
TIA
Firstly: switch to rvm it's excellent for ruby management
If you don't want to then I believe you can remove the ruby package with (if installed with macports) with the following:
sudo port uninstall ruby19
To start using the new version of ruby you need to find where it is installed (it's something like /opt/local/<...>)
Then go to the Binary folder and add it to path.
export PATH=/opt/local/<...>:$PATH
Removing the old one, you might need to change some env variables as well.
Use RVM
EDIT: added sudo to port command and changed standard location

Ruby & Rails installation on a Mac

I used railstutorial.org to install the latest version of ruby and the latest version of rails on my machine.
at the end of the installation I checked
ruby -v ==> 1.9.2 (great)
rails -v ==> 3.0.1 (great)
this morning I opened up terminal
ruby -v
ruby 1.8.6 (2009-06-08 patchlevel 369) [universal-darwin9.0]
rails -v
Rails 1.2.6
what happened?
My advice for people who are installing ruby is to use RVM. It makes managing your ruby versions and gem versions really simple and you can install multiple ruby versions side by side.
You might want to read this post:
http://rubylearning.com/blog/2010/12/20/how-do-i-keep-multiple-ruby-projects-separate/
You can use Cinderalla to the whole ruby/rvm/mysql/redis/git/... stack set up properly. Cinderella installs everything in ~/Developer and fixes up your PATH as well. I had some issues with a corrupt git mirror last time I used Cinderalla though so YMMV.
With many unix variants, you are likely to have multiple versions of Ruby--particularly if you installed Ruby 1.9 and the system already had 1.8 installed. Essentially, the 1.8 version of Ruby has a higher precedence in your PATH than the 1.9 version. The Ruby Gems command keeps the libraries separate between 1.8 and 1.9 so that the platform will be reasonably stable.
To correct the problem, you have to find where ruby 1.9 is installed. Once you do that, you'll need to override your PATH variable. Assuming 1.9 is installed in the path: /opt/ruby-1.9.2, you will need to set your PATH like this:
PATH=/opt/ruby-1.9.2/bin:$PATH
export PATH
To make the path respect what you want every time, add that to your ~/.profile file (create it if necessary). Once the path has been set, it sould be able to find the correct version of Rails again.
I cannot say for certain because I cannot debug you OSX machine from here, however, I had a very similar occurrence. The problem was caused because I had installed ruby and then rails on my machine using sudo or from the root account. Then when I discovered rvm I installed everything in my user account. When I logged off and back in I appeared to lose everything. I was pulling out my hair. I was pissed that I was going to have to reinstall everything again... when I found the magic.
from the command line execute the command:
rvm list
you'll see that your new version of ruby is there. you'll also notice the tokens that indicate that it is just a normal version. It is not current or default. (see it yet)
Now if you run the command:
rvm use 1.9.2 --default
then every time you login/off and restart your machine your user account will default to that version of ruby and all of the gems that you installed against that version.

Resources