Should I use the pre-installed Ruby on a Mac? - ruby

I am using macOS Big Sur. When I check if Ruby is available, I see:
$ ruby -v
ruby 2.6.3p62
Checking with which:
$ which ruby
/usr/bin/ruby
I can see Ruby is pre-installed on macOS. But I've been told, "Do not use the system Ruby." Why?

Don't use the system Ruby!
Here are reasons not to use it for developing with Ruby:
the default location for gems is the system Ruby directory /Library/Ruby/Gems/2.6.0 so you will need to be superuser to install gems (and you really shouldn't alter this folder)
you could use sudo to install gems but that's a security risk (gem installation can run malicious code)
using Bundler is a best practice to manage gem versions and dependencies (projects might use different gem versions; or a project might use different gems that need different versions of a common dependency)
you could install Bundler with the --user-install argument to set the gem directory but that's not a common practice
it's best to start projects with the newest version of Ruby and the system Ruby is 2.6.3
if you've got multiple projects and can't update them all at once, you'll need multiple Ruby versions and a version manager (see my guide Install Ruby on a Mac that compares version managers and shows how to install Ruby with asdf or chruby or Homebrew)
the pre-installed Ruby is deprecated by Apple and may disappear in future macOS releases
Some developers use the system Ruby for running sysadmin scripts. That's fine, as long as you don't alter the system Ruby by attempting to update or adding gems. Remember, the system Ruby is there for macOS, not for you. For development, install Ruby with Homebrew or a version manager.

Related

Where does rbenv install ruby?

I installed ruby 2.7.1 using rbenv.
How can I find where on Mac OS that version of ruby is located?
Note: I need its location for updating the PATH
Also note: which ruby returns /usr/bin/ruby (the old version)
Rbenv by itself does not install Ruby implementations at all. You simply give it the path to an already installed Ruby implementation.
There are several projects that make installing Ruby implementations easier. One of those is the ruby-build project, which also provides an optional plugin for Rbenv that provides an rbenv install subcommand. This plugin will typically install Ruby implementations in ~/.rbenv/versions/*, but that is configurable.
Note, however, that it is generally not required that you set up the $PATH yourself. rbenv init can do that for you, and it knows the correct path anyway, so you don't have to think about it.

How to have rbenv include a Ruby version that was not installed thru rbenv

My command had created tool chains for a number of different tool including Ruby; it is an older version (1.9.3) and available from a network drive /company-tool/bin
I like to explore and learn some of the newer features of Ruby, so I installed v2.0.0 and v2.3.0 and I am using rbenv to manage them.
However, for the my company's tools/systems, I will need to use Ruby that is from /company-tool/bin and not from my rbenv.
Is there a way that I can configure/inject this 1.9.3 version of Ruby into rbenv such that I can use rbenv to switch between the locally installed Ruby and to my company's provided Ruby?
Or is there a better approach for this?
rbenv simply looks for Ruby versions in dirname $(rbenv prefix) (usually ~/.rbenv/versions). You can just symlink your Ruby into that directory.

reinstall every gem for each ruby version?

I just installed Ruby 2.0.0 using rbenv and set it to the global ruby version for my system. Since 2.0 is compatible with 1.9.3, I tried to start up a Rails project with it, but got the following error. I did rbenv rehash after installing 2.0
The `rails' command exists in these Ruby versions:
1.9.3-p327
Does this mean that every gem I installed on my system with 1.9.3 has to be reinstalled if I wish to use it with 2.0?
As seen here:
You need to reinstall bundler for each version of Ruby you use. See Ruby versions where you have it installed:
rbenv whence bundle
See your current version:
rbenv version
Install bundler for that version, if missing:
gem install bundler
Yes. Rbenv (and RVM) have separate "gem home" directories for each installed version of Ruby. There may be ways to symlink certain directories to get them to share, but this will likely lead to problems, particularly with gems that include native C extensions, which may or may not compile and run cleanly in multiple versions.
If you have a Gemfile, easiest thing is to just bundle install again for Ruby 2.0, giving you duplicate copies of many gems and Ruby-2.0 compiled versions of any native gems.
Another solution to this is to copy (or reinstall) the gems from your previous version to the newly installed version. How to do that is answered in detail in this question, which has two scripts -- one to install from local cache, one to reinstall from the internet (mine).

What is the point of installing the same version of Ruby through RVM as the system Ruby?

I've been using the default system ruby version 1.8.7 without RVM for a few rails projects and have not run into any problems. I just recently installed RVM, and after running rvm requirements I get this output:
To use an RVM installed Ruby as default, instead of the system ruby:
rvm install 1.8.7 # installs patch 357: closest supported version
rvm system ; rvm gemset export system.gems ; rvm 1.8.7 ; rvm gemset import system.gems # migrate your gems
rvm alias create default 1.8.7
I believe what these commands do are to install the same gems that have already been installed using the system ruby under the RVM installed ruby.
My questions are, am I right in what these commands do? and if I am right, why is it important to do this, because if I wanted to use an RVM installed Ruby of a different version like 1.9.2, wouldn't it already separate gems in that version from the system's ruby?
The one thing that springs to mind is, if you use the system Ruby, you'll use it slightly differently that RVM's Rubies--for example, you'll likely need to use sudo to install gems. Furthermore, you won't be able to use many of RVM's features, like gemsets, with the system Ruby.
Well one reason I can think of is that you don't wanna worry about your system not working even if the system ruby gets updated.
My questions are, am I right in what these commands do?
You are right in what they do. The first command installs Ruby 1.8.7, the second command installs all the gems currently install on your system Ruby installation in the new RVM Ruby 1.8.7 installation, and the third command sets your default version of Ruby to be the RVM Ruby 1.8.7.
and if I am right, why is it important to do this, because if I wanted to use an RVM installed Ruby of a different version like 1.9.2, wouldn't it already separate gems in that version from the system's ruby?
The second command is actually more of about convenience than necessity. Yes, the gems install in the RVM 1.8.7 will be completely separate from the ones installed in the system version of Ruby; however, if you didn't run the second command, you're RVM 1.8.7 would start out with almost no gems (only the defaults). That means that you would need to go through and manually install the gems that you need to get your project up and running. Instead of doing that, the second command allows you to just install the same gems you've already installed in the system version of Ruby to the RVM 1.8.7 version—it doesn't migrate them, it just makes a copy of them. After the second command, there are two distinct sets of the exact same gems: one in the system Ruby and one in the RVM 1.8.7 Ruby. So, if you were to update gems in either of the Rubies, they would get updated, but the other version's gems would be unaffected.
Hope this helps answer your question.

update ruby to 1.8.7

I'm working on a project that i have to use ruby 1.8.7. I'm using today, ruby 1.8.6 + Mac OSX Darwin. This ruby 1.8.6 was installed with the OS, it's a developer package from Apple. My question is: how can i update this package? if i run ports, it dont find my current installed package and install a new ruby version, with different paths and as effect it breaks my rubygems (i know how to fix it, but it is always workarounds over workarounds).
There is a clean way to update the default ruby that comes with the OS or its better to remove it and just manage it by Port? Please answer like this one How to update to Ruby 1.8.7 don't helps me
Have you tried rvm gem? It manages Ruby versions installed, allow to compile most (if not all) actual (and archival) Ruby versions, sets proper environment variables pointing to actually used ruby version.
$ gem install rvm
$ rvm install 1.8.7
$ rvm use 1.8.7 --default
On my OS X machines I have several versions of Ruby installed.
I install then, from source, under /usr/local/ruby-1.X.X
Once you have multiple versions of Ruby installed you will need your environment to know which one to use.
I do this by setting the PATH, like so
export PATH="/usr/local/ruby-1.8.7/bin:$PATH";
See Hive Logic's article on installing Ruby

Resources