Ruby and Gems Installation Basics
You can install ruby easily using installers that walks you through a GUI. The problem is that you don't have an understanding of what happens when you install Ruby. This article will provide the basics needed to understand the installation process.
You can download the source for Ruby as a tar.gz file from Ruby home page by using curl or a browser and extract it by clicking it or using the tar command. You can go into the extracted directory and run the configure, make and sudo make install to install Ruby. The configure step can be used to configure the directory where Ruby can be installed. If you don't provide a directory, the installation script will copy it to a default directory. The main job of the configure step is to create a Makefile that has rules. The make utility will read the rules. Some of the Ruby code is written in C. The C code is compiled in the make step and the executable is copied from a temporary directory to the system directory in the make install step. Ruby by default is installed in the system directory so that any user who has an account on your machine can use.
The PATH Variable
If you open a terminal and go to any directory, you are able to run ruby. How does that work? When you installed Ruby, the installation program adds the directory of the Ruby executable to the path variable PATH. You can run:
to see the list of all directories in the path variable. When you run Ruby in the terminal, the OS looks in the PATH variable, it goes from left to right and when it encounters the first occurrence of a Ruby executable, it stops searching. Even if you have a newer version installed that is in the $PATH after the older version, the older version will get picked up.
Location of Gems and Ruby Executable
You can use the gem command to see where the gems are installed on your system.
gem env RubyGems Environment: - RUBYGEMS VERSION: 22.214.171.124 - RUBY VERSION: 2.2.3 (2015-08-18 patchlevel 173) [x86_64-darwin14] - INSTALLATION DIRECTORY: /Users/bparanj/.rvm/gems/ruby-2.2.3 - RUBY EXECUTABLE: /Users/bparanj/.rvm/rubies/ruby-2.2.3/bin/ruby
The INSTALLATION DIRECTORY shows you the directory where all the gems are installed on your system. In my machine:
ls -l /Users/bparanj/.rvm/gems/ruby-2.2.3/gems
shows all the gems installed on my machine. The Ruby executable is in the
/Users/bparanj/.rvm/rubies/ruby-2.2.3/bin/ruby folder displayed as the value of RUBY EXECUTABLE. I can verify this by using the
which ruby /Users/bparanj/.rvm/rubies/ruby-2.2.3/bin/ruby
In this article, you learned the basics of Ruby installation and what happens when you install Ruby from source. If you worked on Rails projects, you may be aware that you use a similar process to install ImageMagick library. This is the basics of Ruby and provides a good foundation to learn about
$LOADED_FEATURES that will be discussed in upcoming articles.