Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Two different ruby versions on mac?

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.

like image 366
Christian Doucette Avatar asked Sep 13 '20 04:09

Christian Doucette


2 Answers

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

like image 86
Hugo V Avatar answered Oct 03 '22 03:10

Hugo V


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.

like image 27
gigalul Avatar answered Oct 03 '22 05:10

gigalul