Wilson Mar bio photo

Wilson Mar

Hello. Hire me!

Email me Calendar Skype call 310 320-7878

LinkedIn Twitter Gitter Google+ Youtube

Github Stackoverflow Pinterest

Switch among multiple versions of Ruby


Overview

First of all, know that there is a war going on within the Ruby community between rvm and rbenv.

To install rbenv, one must first remove RVM because it’s incompatible with rbenv.

The major commands:

  1. ruby
  2. gem
  3. rvm
  4. bundle
  5. rbenv

http://jonathan-jackson.net/rvm-and-rbenv

View Ruby version

A Ruby language compiler is included in Mac OSX. So we can jump straight to view version information.

CAUTION: Don’t touch the system Ruby that comes with your Mac.

  1. Open a Terminal Shell Window and type:

    ruby --version

    The response for the Sierra default:

    ruby 2.3.0p0 (2015-12-25 revision 53290) [x86_64-darwin15]

    The response for the Yosemite default:

    ruby 2.0.0p481 (2014-05-08 revision 45883) [universal.x86_64-darwin14]

    The response after updating on 2016-06-16:

    ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-darwin15]

CAUTION: The Apple Mac operating system makes use of Ruby, so don’t delete the .rvm folder.

Gem version numbers

gem env

The response on 2016-06-16:

RubyGems Environment:
  - RUBYGEMS VERSION: 2.6.4
  - RUBY VERSION: 2.3.1 (2016-04-26 patchlevel 112) [x86_64-darwin15]
  - INSTALLATION DIRECTORY: /Users/mac/.rvm/gems/ruby-2.3.1
  - USER INSTALLATION DIRECTORY: /Users/mac/.gem/ruby/2.3.0
  - RUBY EXECUTABLE: /Users/mac/.rvm/rubies/ruby-2.3.1/bin/ruby
  - EXECUTABLE DIRECTORY: /Users/mac/.rvm/gems/ruby-2.3.1/bin
  - SPEC CACHE DIRECTORY: /Users/mac/.gem/specs
  - SYSTEM CONFIGURATION DIRECTORY: /Users/mac/.rvm/rubies/ruby-2.3.1/etc
  - RUBYGEMS PLATFORMS:
    - ruby
    - x86_64-darwin-15
  - GEM PATHS:
     - /Users/mac/.rvm/gems/ruby-2.3.1
     - /Users/mac/.rvm/gems/ruby-2.3.1@global
  - GEM CONFIGURATION:
     - :update_sources => true
     - :verbose => true
     - :backtrace => false
     - :bulk_threshold => 1000
  - REMOTE SOURCES:
     - https://rubygems.org/
  - SHELL PATH:
     - /Users/mac/.rvm/gems/ruby-2.3.1/bin
     - /Users/mac/.rvm/gems/ruby-2.3.1@global/bin
     - /Users/mac/.rvm/rubies/ruby-2.3.1/bin
     - /Users/mac/depot_tools
     - /Users/mac/.npm-packages/bin
     - /Users/mac/miniconda2/bin
     - /Users/mac/.rbenv/shims
     - /Users/mac/.rbenv/bin
     - /usr/local/bin
     - /usr/bin
     - /bin
     - /usr/sbin
     - /sbin
     - /Users/mac/gits
     - /Users/mac/.rvm/bin
     - /Users/mac/.rvm/bin
   
  1. View each component individually:

    gem --version

    The response for the Sierra default:

    2.0.14

    The response for the Yosemite default:

    2.0.14

    After updating on 2017-02-16:

    2.6.10

    QUESTION: List of versions?

Clean

gem clean

Cleaning up installed gems...
Clean Up Complete
   

View rvm version number

rvm info

List

rvm list known

For a smaller response: rvm --version

The response on 2016-06-16:

rvm 1.27.0 (master) by Wayne E. Seguin wayneeseguin@gmail.com, Michal Papis mpapis@gmail.com [https://rvm.io/]

  1. Visit the RVM.io website

    https://rvm.io/support/troubleshooting

Update RubyGems and Bundler:

  1. Update:

    sudo gem update --system

    The response on 2016-06-16:

    Password:
    Updating rubygems-update
    Fetching: rubygems-update-2.6.4.gem (100%)
    Successfully installed rubygems-update-2.6.4
    Parsing documentation for rubygems-update-2.6.4
    Installing ri documentation for rubygems-update-2.6.4
    Installing darkfish documentation for rubygems-update-2.6.4
    Done installing documentation for rubygems-update after 2 seconds
    Parsing documentation for rubygems-update-2.6.4
    Done installing documentation for rubygems-update after 0 seconds
    Installing RubyGems 2.6.4
    RubyGems 2.6.4 installed
    Parsing documentation for rubygems-2.6.4
    Installing ri documentation for rubygems-2.6.4
     
    === 2.6.3 / 2016-04-05
    ...
    Minor enhancements:
    ...
    Bug fixes:
    ...
    RubyGems installed the following executables:
    /Users/mac/.rvm/rubies/ruby-2.3.1/bin/gem
     
    Ruby Interactive (ri) documentation was installed. ri is kind of like man 
    pages for ruby libraries. You may access it like this:
      ri Classname
      ri Classname.class_method
      ri Classname#instance_method
    If you do not wish to install this documentation in the future, use the
    --no-document flag, or set it as the default in your ~/.gemrc file. See
    'gem help env' for details.
     
    RubyGems system software updated
    
  2. List local gems:

    gem list b

    A sample example (on 2016-06-16):

    bigdecimal (default: 1.2.8)
    bundler-unload (1.0.2)
    executable-hooks (1.3.2)
    rubygems-bundler (1.4.4)
    rubygems-update (2.6.4)
    ...
    

    Update Bundler:

  3. If bundler is not on the list above:

    gem install bundler

    A sample response:

    Fetching: bundler-1.12.5.gem (100%)
    Successfully installed bundler-1.12.5
    Parsing documentation for bundler-1.12.5
    Installing ri documentation for bundler-1.12.5
    Done installing documentation for bundler after 5 seconds
    1 gem installed
    
  4. Navigate to a folder containing :

    bundle install

    A sample response:

    Fetching gem metadata from https://rubygems.org/
    Fetching version metadata from https://rubygems.org/
    Fetching dependency metadata from https://rubygems.org/
    Installing rake 11.1.2
    ...
    Bundle complete! 5 Gemfile dependencies, 39 gems now installed.
    Use `bundle show [gemname]` to see where a bundled gem is installed.
    
  5. Run

    sudo gem install --no-rdoc --no-ri bundler

    The response:

    Fetching: bundler-1.9.4.gem (100%)
    Successfully installed bundler-1.9.4
    1 gem installed
    

Update gem:

  1. Update latest version of gem:

    sudo gem update

    The response takes several minutes becuase it touches every gem:

    Updating installed gems
    Updating CFPropertyList
    
  2. Press y and Enter if you see:

    rake's executable "rake" conflicts with /usr/bin/rake
    rdoc's executable "rdoc" conflicts with /usr/bin/rdoc
    rdoc's executable "ri" conflicts with /usr/bin/ri
    
  3. To rebuild any gems using native extensions:

    gem pristine –all

  4. Cleanup gem:

    sudo gem cleanup

    The response:

    Cleaning up installed gems...
    Clean Up Complete
    

Ruby Version Manager (rvm)

  1. Get on the latest version of RVM:

    rvm get stable
    
  2. Uninstall all versions of Ruby:

    rvm uninstall ruby

  3. Have the rvm shell configuration loaded:

    source ~/.rvm/scripts/rvm
    type rvm | head -n 1

    Alternately, wipe out the folder and start over:

    rm -rf ~/.rvm
    curl -L https://get.rvm.io | bash -s stable

    The response should be:

    rvm is a function

    If so, try rvm get head again.

    rvm get head

    If the response is:

    -bash: rvm: command not found
    
  4. Get stable

    rvm get stable --auto-dotfiles
    

To upgrade

  1. Run:

    rvm autolibs homebrew
    rvm install ruby

    The response:

    Searching for binary rubies, this might take some time.
    No binary rubies available for: osx/10.11/x86_64/ruby-2.3.1.
    Continuing with compilation. Please read 'rvm help mount' to get more information on binary rubies.
    Checking requirements for osx_brew.
    Installing requirements for osx_brew.
    Updating system.....
    Installing required packages: autoconf, automake, libtool, readline, libksba, openssl........
    Certificates in '/usr/local/etc/openssl/cert.pem' are already up to date.
    Requirements installation successful.
    Installing Ruby from source to: /Users/mac/.rvm/rubies/ruby-2.3.1, this may take a while depending on your cpu(s)...
    ruby-2.3.1 - #downloading ruby-2.3.1, this may take a while depending on your connection...
      % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                  Dload  Upload   Total   Spent    Left  Speed
    100 13.7M  100 13.7M    0     0   320k      0  0:00:44  0:00:44 --:--:--  266k
    perl: warning: Setting locale failed.
    perl: warning: Please check that your locale settings:
    LC_ALL = (unset),
    LC_CTYPE = "en_US.utf-",
    LANG = "en_US.UTF-8"
     are supported and installed on your system.
    perl: warning: Falling back to the standard locale ("C").
    ruby-2.3.1 - #extracting ruby-2.3.1 to /Users/mac/.rvm/src/ruby-2.3.1.....
    ruby-2.3.1 - #configuring......................................................|
    ruby-2.3.1 - #post-configuration.
    ruby-2.3.1 - #compiling........................................................-
    ruby-2.3.1 - #installing.........
    ruby-2.3.1 - #making binaries executable..
    Installed rubygems 2.5.1 is newer than 2.4.8 provided with installed ruby, skipping installation, use --force to force installation.
    ruby-2.3.1 - #gemset created /Users/mac/.rvm/gems/ruby-2.3.1@global
    ruby-2.3.1 - #importing gemset /Users/mac/.rvm/gemsets/global.gems.............|
    ruby-2.3.1 - #generating global wrappers........
    ruby-2.3.1 - #gemset created /Users/mac/.rvm/gems/ruby-2.3.1
    ruby-2.3.1 - #importing gemsetfile /Users/mac/.rvm/gemsets/default.gems evaluated to empty gem list
    ruby-2.3.1 - #generating default wrappers........
    ruby-2.3.1 - #adjusting #shebangs for (gem irb erb ri rdoc testrb rake).
    Install of ruby-2.3.1 - #complete 
    Ruby was built without documentation, to build it run: rvm docs generate-ri
    
  2. View versions again.

  3. To install the latest stable rvm release:

    \curl -ssL https://get.rvm.io | bash -s stable

    A sample response:

    Downloading https://github.com/rvm/rvm/archive/1.26.11.tar.gz
    Downloading https://github.com/rvm/rvm/releases/download/1.26.11/1.26.11.tar.gz.asc
    Found PGP signature at: 'https://github.com/rvm/rvm/releases/download/1.26.11/1.26.11.tar.gz.asc',
    but no GPG software exists to validate it, skipping.
     
    Installing RVM to /Users/wilsonmar/.rvm/
    Adding rvm PATH line to /Users/wilsonmar/.profile /Users/wilsonmar/.mkshrc /Users/wilsonmar/.bashrc /Users/wilsonmar/.zshrc.
    Adding rvm loading line to /Users/wilsonmar/.profile /Users/wilsonmar/.bash_profile /Users/wilsonmar/.zlogin.
    Installation of RVM in /Users/wilsonmar/.rvm/ is almost complete:
     
           * To start using RVM you need to run `source /Users/wilsonmar/.rvm/scripts/rvm`
    in all your open shell windows, in rare cases you need to reopen all shell windows.
     
    # Wilson Mar,
    #
    #   Thank you for using RVM!
    #   We sincerely hope that RVM helps to make your life easier and more enjoyable!!!
    #
    # ~Wayne, Michal & team.
     
    In case of problems: http://rvm.io/help and https://twitter.com/rvm_io
     
           * WARNING: You have '~/.profile' file, you might want to load it,
    to do that add the following line to '/Users/wilsonmar/.bash_profile':
     
    source ~/.profile
    
  4. Some say at this point close the terminal and open again.

Sample Ruby Tree program

Here is a Ruby script to produce a nice Unicode tree along with metadata to its left:

#!/usr/bin/env ruby
def tree_hierarchy( root, &children )
  queue = [[root,"",true]]
  [].tap do |results|
    until queue.empty?
      item,indent,last = queue.pop
      kids = children[item]
      extra = indent.empty? ? '' : last ? '└╴' : '├╴'
      results << [ indent+extra, item ]
      results << [ indent, nil ] if last and kids.empty?
      indent += last ? '  ' : '│ '
      parts = kids.map{ |k| [k,indent,false] }.reverse
      parts.first[2] = true unless parts.empty?
      queue.concat parts
    end
  end
end
def tree(dir)
  cols = tree_hierarchy(File.expand_path(dir)) do |d|
    File.directory?(d) ? Dir.chdir(d){ Dir['*'].map(&File.method(:expand_path)) } : []
  end.map do |indent,path|
    if path
      file = File.basename(path) + File.directory?(path) ? '/' : ''
      meta = `ls -lhd "#{path}"`.split(/\s+/)
      [ [indent,file].join, meta[0], meta[4], "%s %-2s %s" % meta[5..7] ]
    else
      [indent]
    end
  end
  maxs = cols.first.zip(*(cols[1..-1])).map{ |c| c.compact.map(&:length).max }
  tmpl = maxs.map.with_index{ |n,i| "%#{'-' if cols[0][i][/^\D/]}#{n}s" }.join('  ')
  cols.map{ |a| a.length==1 ? a.first : tmpl % a }
end
puts tree(ARGV.first || ".") if __FILE__==$0
   
  1. Copy and paste the above into a text editor program.
  2. Save the file named tree.rb.
  3. In a Terminal window, navigate to the folder holding the script.
  4. Mark the file as executable:

    chmod +x tree.rb

    This only needs to be done once.

  5. Run the program:

    tree.rb

    ERROR: The response:

    ./tree.rb:24:in `+': no implicit conversion of true into String (TypeError)
      from ./tree.rb:24:in `block in tree'
      from ./tree.rb:22:in `map'
      from ./tree.rb:22:in `tree'
      from ./tree.rb:35:in `<main>'
    

    http://superuser.com/users/57219/phrogz

Remove rvm

  1. Run the program which removes the rvm/ directory and all the rubies built within it:

    
    rvm repair
    rvm cleanup
    rvm implode --force
    
  2. Remove folders:

    rm -rf /usr/local/rvm
    sudo rm /etc/profile.d/rvm.sh
    sudo rm /etc/rvmrc
    sudo rm ~/.rvmrc
    
  3. Check references to rvm in files:

    vim .bash_profile

    vim .bashrc

    vim .profile

  4. Restart Terminal sessions.

See http://karloespiritu.com/replacing-rvm-with-rbenv-in-os-x/

Install rbenv


   brew update
   brew install rbenv ruby-build
   

The response:

==> Installing dependencies for rbenv: openssl, ruby-build
==> Installing rbenv dependency: openssl
==> Downloading https://homebrew.bintray.com/bottles/openssl-1.0.2k.sierra.bottl
######################################################################## 100.0%
==> Pouring openssl-1.0.2k.sierra.bottle.tar.gz
==> Using the sandbox
==> Caveats
A CA file has been bootstrapped using certificates from the SystemRoots
keychain. To add additional certificates (e.g. the certificates added in
the System keychain), place .pem files in
  /usr/local/etc/openssl/certs
 
and run
  /usr/local/opt/openssl/bin/c_rehash
 
This formula is keg-only, which means it was not symlinked into /usr/local.
 
Apple has deprecated use of OpenSSL in favor of its own TLS and crypto libraries
 
If you need to have this software first in your PATH run:
  echo 'export PATH="/usr/local/opt/openssl/bin:$PATH"' >> ~/.bash_profile
 
For compilers to find this software you may need to set:
    LDFLAGS:  -L/usr/local/opt/openssl/lib
    CPPFLAGS: -I/usr/local/opt/openssl/include
For pkg-config to find this software you may need to set:
    PKG_CONFIG_PATH: /usr/local/opt/openssl/lib/pkgconfig
 
==> Summary
🍺  /usr/local/Cellar/openssl/1.0.2k: 1,696 files, 12M
==> Installing rbenv dependency: ruby-build
==> Downloading https://github.com/rbenv/ruby-build/archive/v20170201.tar.gz
==> Downloading from https://codeload.github.com/rbenv/ruby-build/tar.gz/v201702
######################################################################## 100.0%
==> ./install.sh
🍺  /usr/local/Cellar/ruby-build/20170201: 334 files, 178.8K, built in 4 seconds
==> Installing rbenv 
==> Downloading https://homebrew.bintray.com/bottles/rbenv-1.1.0.sierra.bottle.t
######################################################################## 100.0%
==> Pouring rbenv-1.1.0.sierra.bottle.tar.gz
🍺  /usr/local/Cellar/rbenv/1.1.0: 36 files, 63.2K
   

Resources:

This also provides instructions on installation of Ruby:

http://www.createdbypete.com/articles/ruby-on-rails-development-setup-for-mac-osx/

More on OSX

This is one of a series on Mac OSX: