Use RVM to manage Ruby and gem under nix, rvmruby
Reprinted: http://www.ibm.com/developerworks/cn/aix/library/au-aix-manage-ruby/
Although Internet Relay Chat, forums, and version control tools (such as Git and Github) make distributed development very simple, it will be very difficult to rebuild another developer's environment. Supporting existing code bodies on a new development machine requires matching at least part (or possibly all) of the original coder's stack, or matching the software components necessary to enhance the application. The stack may require an operating system (such as UNIX®, Linux®, Mac OS, or Windows®); a specific operating system version (Ubuntu 10 or 11? Mac OS X 10.6 Snow Leopard or 10.7 Lion?); Built-in and auxiliary The minimum collection of system libraries and header files; supporting facilities for daemons and services (MySQL or PostgreSQL?); Of course, various language-specific resources, including supporting open source from compilers to specific patch levels Code.
In the worst case, some code is locked, or depends on specific modifications of each layer of the stack, and may even depend on specific hardware. Ideally, the standards and abstractions in the stack will also simplify portability. Portable Operating System Interface (POSIX) compliance is a useful standard: in this way, various systems that support the POSIX standard will provide the same software interface and produce the same results. Interpreted languages such as Ruby can support abstraction because the code does not need to be compiled, and the code can (conceivably) run on any platform with an interpreter.
In other words, even application code written in interpreted languages such as Perl, PHP, and Ruby requires adaptation to new machines to some extent. In fact, Ruby usually requires considerable setup work, largely because Ruby continues to evolve rapidly and is pushed into more changing computing environments. That said, at the time of writing, there are two popular versions of the language in use, at least 5 Ruby interpreters are based on three different computer languages, and each interpreter has several different versions, not to mention It is said that there are thousands of third-party libraries:
Ruby version 1.8 is still in use, and Ruby version 1.9 is more popular in recent coding work.
Ruby interpreters include Matz's Ruby Interpreter (MRI: written in C, named after the creator of Ruby, that is, Japanese computer scientist Yukihiro Matz Matsumoto); JRuby (written in Java ™ language, driven by Java Virtual Machine [JVM]); Rubinius (Written in Ruby and run on top of the C ++ bytecode virtual machine [VM]); Ruby Enterprise Edition (a fork of [REE] Ruby code and enhanced to improve performance and reduce memory consumption); A fast and stable 64-bit Ruby implementation based on VMware's GemStone / S 3.1 VM) (see Resources for a link). All interpreters are open source. Each interpreter has its own advantages, and the specific solution to use depends on the application and developer preferences.
According to RubyGems.org (the official library of Ruby code already provided), there are currently 41780 gem instances available.
Not all Ruby interpreters can replace each other, which complicates the situation. Although most code migrations that occur between different implementations will not cause problems, there is no guarantee (in all cases) that one interpreter will produce the same results as another interpreter. Considering so many types and the need to compare results, Ruby developers need to quickly access multiple stacks (just like developers who code in C), but maintaining different Ruby development environments is not like using two on a computer. Different C compilers. Shell environment variables (including PATH, GEM_PATH, and GEM_HOME) help to separate the environment, but to achieve a consistent, clear, and correct division between the two stacks, a lot of work is required and strict discipline is followed.
Today, Ruby developers have a nearly magical alternative: Ruby Version Manager (RVM), whose creator is Wayne E. Seguin (see Resources for a link).
Introduction to Ruby Version Manager
RVM provides some important features:
RVM can currently build more than 35 implementations. You can download, build, and install any Ruby interpreter with a single command.
It creates and manages any number of Ruby environments, each of which provides a separate Ruby interpreter. Therefore, one project can use JRuby, while another project developed on the same computer can specify the latest MRI.
In addition, each environment can have any number of scattered gem collections. RVM appropriately refers to each collection as a gemset. You can have a gemset named auto_parts_store associated with JRuby, and a completely different gemset named auto_parts_store associated with MRI version 1.9.3. Each gemset must use a unique name in the interpreter context.
The combination of the interpreter and gemset (providing an engine that runs Ruby code and the body of the library code) can be easily referenced through the interpreter @ gemset symbol, where interpreter is the name of a known interpreter, and gemset is the name of an existing gemset. For example, 1.8.7p302@acme refers to the 1.8.7 version of the Ruby MRI patch level 302 for the language, and the gem collection for the Acme website. Any name can be used for gemset. The name of each interpreter is provided by RVM.
Switching between different environments is very simple. You need to type the following command: rvm interpreter @ gemset.
Usually, in your home directory, each environment is self-contained and siloed. In fact, the entire RVM system is located in your home directory, ensuring that another user cannot force rewrite or destroy the content you have collected, and similarly, you cannot destroy the content that other users have collected. (You can also install RVM centrally, but this practice is less common).
You can run the application's test suite against one, some, or all of the environments. Run MRI locally, but use REE on the server. Use RVM to test the code for both and then use these modifications formally.
RVM allows you to quickly try out code combinations so that you can keep your application stable and up to date. If a new MRI is released, you can build it in RVM, create a new environment, run the test, and if everything goes well, you can use the new MRI.
Back to top
getting Started
RVM installation is very simple. Just use one command and make a few changes to your shell startup script.
However, RVM needs to meet some prerequisites. Before continuing, please check your system and make sure that the bash, git, tar, curl, gunzip, and bunzip2 utilities are installed locally. In addition, your system must have Readline, IConv, zLib, OpenSSL, and Autoconf packages to build the Ruby interpreter. You must use the Bash shell to install RVM, but you can use it with Z sehll version 4.3.5 or later after installing RVM. You need to use git to update RVM automatically, which will be introduced later.
If your system lacks all the packages listed above, you can quickly install them through a package manager, such as using the Advanced Packaging Tool for various Linux distributions or brew for Mac OS X. You must also have the GNU C compiler to build the application.
After preparing the system, go to a shell prompt and enter (or copy and paste) the following commands:
$ bash <<(curl http://rvm.beginrescueend.com/releases/rvm-install-head)
Note: There is a space between the two less-than signs (<). The special Bash syntax <(...) runs the commands in parentheses and saves the output to a temporary file. The first <is a typical input redirection. Therefore, the command runs bash and provides input from the temporary file created by curl. Think of the sequence as running a shell script, although it is stored on a remote server.
After preparing the system, go to a Shell prompt and enter (or copy and paste) the command.
$ curl -L https://get.rvm.io | bash -s stable
This command uses git to clone and create a local RVM instance in your home directory. Check that the bin subdirectory under $ HOME / .rvm itself contains the rvm utility, and rubies is the final directory of the Ruby interpreter. You will find that RVM organizes the accumulated software into a predictable hierarchy based on the type, version, and gemset of the interpreter.
After completing the script installation, you must edit the dot or startup file of the shell so that the RVM shell functions can be loaded when the new shell starts. Usually, this behavior:
[[-s "$ HOME / .rvm / scripts / rvm"]] && source "$ HOME / .rvm / scripts / rvm"
Must be the last line of the file. If using bash, append this line of code to the end of the $ HOME / .bashrc file. For the changes to take effect, you must start a new shell or enter source $ HOME / .bashrc to reload the file.
Back to top
Choose any Ruby version
Now that RVM is installed, one or more Ruby interpreters are downloaded, built, and installed. There are many Ruby versions available: counting language versions, patch levels, and basic implementations. You can use the rvm list known command to list all known versions, as shown in Listing 1.
Listing 1. List all known Ruby interpreter versions
$ rvm list known
# MRI Rubies
[ruby-] 1.8.6 [-p420]
[ruby-] 1.8.6-head
[ruby-] 1.8.7 [-p352]
[ruby-] 1.8.7-head
[ruby-] 1.9.1-p378
[ruby-] 1.9.1 [-p431]
[ruby-] 1.9.1-head
[ruby-] 1.9.2-p180
[ruby-] 1.9.2 [-p290]
[ruby-] 1.9.2-head
[ruby-] 1.9.3-preview1
[ruby-] 1.9.3-rc1
[ruby-] 1.9.3 [-p0]
[ruby-] 1.9.3-head
ruby-head
# GoRuby
goruby
# JRuby
jruby-1.2.0
jruby-1.3.1
jruby-1.4.0
jruby-1.6.1
jruby-1.6.2
jruby-1.6.3
jruby-1.6.4
jruby [-1.6.5]
jruby-head
# Rubinius
rbx-1.0.1
rbx-1.1.1
rbx-1.2.3
rbx-1.2.4
rbx [-head]
rbx-2.0.0pre
# Ruby Enterprise Edition
ree-1.8.6
ree [-1.8.7] [-2011.03]
ree-1.8.6-head
ree-1.8.7-head
# Kiji
kiji
# MagLev
maglev [-head]
maglev-1.0.0
# Mac OS X Snow Leopard Only
macruby [-0.10]
macruby-nightly
macruby-head
# IronRuby-Not implemented yet.
ironruby-0.9.3
ironruby-1.0-rc2
ironruby-head
The patch level 352 MRI for Ruby 1.8.7 provides a good foundation for Ruby and Rails development. Let's install the patch first.
Enter rvm install and engine name ruby-1.8.7-p352. You can also enter rvm install 1.8.7. Some interpreter names appearing in square brackets ([]) are optional, so rvm install 1.8.7 is the same as rvm install ruby-1.8.7-p352. Generally, if only one version is specified, RVM will install the latest code for that version.
After installing the interpreter, RVM will download and build the code for you. Listing 2 shows the code.
Listing 2. Building the interpreter
$ rvm install 1.8.7
Installing Ruby from source to: /Users/strike/.rvm/rubies/ruby-1.8.7-p352, this
may take a while depending on your cpu (s) ...
ruby-1.8.7-p352-#fetching
ruby-1.8.7-p352-#downloading ruby-1.8.7-p352,
this may take a while depending on your connection ...
ruby-1.8.7-p352-#extracting ruby-1.8.7-p352 to /Users/strike/.rvm/src/ruby-1.8.7-p352
ruby-1.8.7-p352-#extracted to /Users/strike/.rvm/src/ruby-1.8.7-p352
Applying patch 'stdout-rouge-fix'
located at /Users/strike/.rvm/patches/ruby/1.8.7/stdout-rouge-fix.patch)
ruby-1.8.7-p352-#configuring
ruby-1.8.7-p352-#compiling
ruby-1.8.7-p352-#installing
Retrieving rubygems-1.8.10
Extracting rubygems-1.8.10 ...
Removing old Rubygems files ...
Installing rubygems-1.8.10 for ruby-1.8.7-p352 ...
Installation of rubygems completed successfully.
ruby-1.8.7-p352-adjusting #shebangs for (gem irb erb ri rdoc testrb rake).
ruby-1.8.7-p352-#importing default gemsets (/Users/strike/.rvm/gemsets/)
Install of ruby-1.8.7-p352-#complete
To switch to the 1.8.7 interpreter you just installed, enter rvm 1.8.7. To list the specific content of the current environment, enter rvm info. Listing 3 shows the code.
Listing 3. Switch to 1.8.7 interpreter
$ rvm 1.8.7
$ rvm info
rvm:
version: "rvm 1.10.0-pre by Wayne E. Seguin
(wayneeseguin@gmail.com) [https://rvm.beginrescueend.com/] "
ruby:
interpreter: "ruby"
version: "1.8.7"
date: "2011-06-30"
platform: "i686-darwin11.2.0"
patchlevel: "2011-06-30 patchlevel 352"
full_version: "ruby 1.8.7 (2011-06-30 patchlevel 352) [i686-darwin11.2.0]"
homes:
gem: "/Users/strike/.rvm/gems/ruby-1.8.7-p352"
ruby: "/Users/strike/.rvm/rubies/ruby-1.8.7-p352"
binaries:
ruby: "/Users/strike/.rvm/rubies/ruby-1.8.7-p352/bin/ruby"
irb: "/Users/strike/.rvm/rubies/ruby-1.8.7-p352/bin/irb"
gem: "/Users/strike/.rvm/rubies/ruby-1.8.7-p352/bin/gem"
rake: "/Users/strike/.rvm/gems/ruby-1.8.7-p352/bin/rake"
environment:
PATH: "/Users/strike/.rvm/gems/ruby-1.8.7-p352/bin:
/Users/strike/.rvm/gems/ruby-1.8.7-p352@global/bin:
/Users/strike/.rvm/rubies/ruby-1.8.7-p352/bin:/Users/strike/.rvm/bin:
/Users/strike/.gem/ruby/1.8.7/bin:/Users/strike/.ruby_versions/ruby-1.8.7-p174/bin:
/ Users / strike / bin / rds: / usr / local / bin: / usr / bin: / bin: / usr / sbin: / sbin: / usr / X11 / bin "
GEM_HOME: "/Users/strike/.rvm/gems/ruby-1.8.7-p352"
GEM_PATH: "/Users/strike/.rvm/gems/ruby-1.8.7-p352:
/Users/strike/.rvm/gems/ruby-1.8.7-p352@global "
MY_RUBY_HOME: "/Users/strike/.rvm/rubies/ruby-1.8.7-p352"
IRBRC: "/Users/strike/.rvm/rubies/ruby-1.8.7-p352/.irbrc"
RUBYOPT: ""
gemset: ""
The most important information is located in the interpreter, version, patchlevel, and gemset lines, which reflect the type, language version, modification, and current gemset of the interpreter, respectively. If you switch to a new environment, for example, to ree @ auto_parts_store, these lines of code will change accordingly. In fact, most of the information displayed will change each time the environment is switched. In addition, RVM will automatically modify shell settings and variables to create an island for each RVM environment. Note that PATH has been changed to point to the new Ruby. Enter which ruby to confirm.
$ which ruby
/Users/strike/.rvm/rubies/ruby-1.8.7-p352/bin/ruby
As you can see, the first executable Ruby in the path is the one you just installed.
To install a different Ruby interpreter, run rvm install again to specify a different name. Enter rvm list at any time to find the ruby you have installed locally.
$ rvm list
jruby-1.6.4 [x86_64]
ree-1.8.7-2009.10 [i686]
ree-1.8.7-2010.02 [i686]
ruby-1.8.7-p334 [i686]
=> ruby-1.8.7-p352 [i686]
ruby-1.9.2-p0 [x86_64]
ruby-1.9.2-p290 [x86_64]
The arrow in the output indicates the Ruby interpreter currently in use.
Similarly, if you want to know the names of all gemsets defined in the current environment, enter rvm gemset list. Similarly, the arrows in it also indicate the currently used gemset.
$ rvm gemset list
gemsets for ruby-1.8.7-p352 (found in /Users/strike/.rvm/gems/ruby-1.8.7-p352)
brewster
canvas-src
global
=> miner
moms
tech
Among these gemsets, the gemset named global is special. As Wayne Seguin explained, "RVM provides a global gemset for each Ruby interpreter. The gem installed for a given Ruby to the global gemset can be used for all other gemsets related to that Ruby. This approach allows all projects to target a specific Ruby Interpreter installations share the same gem ". Let's see how to create, delete, extend and switch to different gemsets.
Back to top
Management gemset
RVM allows you to switch from one interpreter to another. You can now develop a new code based on the features and syntax of Ruby version 1.9.2 in one directory and maintain the older 1.8.7 code base in another directory, both of which are on the same computer . You can even run a single code base in any Ruby version you choose.
The next step is to use a separate gemset to manage the library dependencies of the project.
To demonstrate this, assume that the directory web-1.8.7 contains a Rails application based on Rails 3.1 and Ruby 1.8.7, and the directory daemon-1.9.2 is a system daemon written in Ruby, based on version 1.9.2 (You don't need to put the version number in the directory name; explicit naming is used here to minimize confusion). Provide a separate gemset for each code base and see how to populate each code base.
To switch to 1.8.7, enter rvm 1.8.7. Type rvm gemset create web to create a new gemset named Web, as shown in Listing 4.
Listing 4. Create and select a gemset
$ cd web-1.8.7
$ rvm 1.8.7
$ rvm gemset create web
$ rvm gemset list
brewster
canvas-src
global
=> miner
moms
tech
web
$ rvm gemset use web
$ rvm gemset list
brewster
canvas-src
global
miner
moms
tech
web
=> web
$ rvm info
ruby:
interpreter: "ruby"
version: "1.8.7"
...
patchlevel: "2011-06-30 patchlevel 352"
...
gemset: "web"
rvm gemset create will create a new gemset, but will not change it to the latest, active gemset. Use rvm gemset use to switch to a specific gemset. You can use the rvm info command to check or confirm your status at any time.
After the new gemset is activated, any command related to gem installation will only affect the gemset. For example, the most common gem install command will add a gem list. When gemset is activated, the gem will not be installed in the system gem cache, but in the gemset's own cache, as shown in Listing 5.
Listing 5. Install gem to gemset
$ rvm gemset use style --create
$ gem list
$ gem install rails
Fetching: multi_json-1.0.4.gem (100%)
Fetching: activesupport-3.1.3.gem (100%)
Fetching: builder-3.0.0.gem (100%)
Fetching: i18n-0.6.0.gem (100%)
Fetching: activemodel-3.1.3.gem (100%)
...
Successfully installed rails-3.1.3
29 gems installed
$ gem list
*** LOCAL GEMS ***
actionmailer (3.1.3)
actionpack (3.1.3)
activemodel (3.1.3)
activerecord (3.1.3)
activeresource (3.1.3)
activesupport (3.1.3)
arel (2.2.1)
builder (3.0.0)
bundler (1.0.21)
erubis (2.7.0)
hike (1.2.1)
i18n (0.6.0)
json (1.6.3)
mail (2.3.0)
mime-types (1.17.2)
multi_json (1.0.4)
polyglot (0.3.3)
rack (1.3.5)
rack-cache (1.1)
rack-mount (0.8.3)
rack-ssl (1.3.2)
rack-test (0.6.1)
rails (3.1.3)
railties (3.1.3)
rake (0.8.7)
rdoc (3.12)
sprockets (2.0.3)
thor (0.14.6)
tilt (1.3.3)
treetop (1.4.10)
tzinfo (0.3.31)
To verify that the gem is sealed in the gemset, switch to another gemset and rerun the gem list. Switch back and run the command again.
Similar to the gem command, the bundler command also installs gem, but it is installed from a manifest file (manifest) called Gemfile. If you run bundler in the context of a gemset, the gem will be added to the collection separately. Combining RVM and bundler makes it easy to switch between two Rails projects.
If you need to recreate the gemset from scratch, you can use rvm gemset empty someset to delete everything in one fell swoop. Because rvm empty is destructive and irreversible, RVM will prompt for confirmation. Enter yes to continue:
$ rvm gemset empty someset
WARN: Are you SURE you wish to remove the installed gems for
gemset 'ruby-1.8.7-p352@someset' (/Users/strike/.rvm/gems/ruby-1.8.7-p352@someset)?
(anything other than 'yes' will cancel)> yes
$ rvm gemset delete someset
WARN: Are you SURE you wish to remove the entire gemset
directory 'someset' (/Users/strike/.rvm/gems/ruby-1.8.7-p352@someset)?
(anything other than 'yes' will cancel)> yes
The line of code rvm gemset delete someset will completely delete the gemset someset. This also requires a prompt to be displayed.
You can find a complete list of RVM commands by entering rvm help, or refer to the documentation and tips on the RVM website.
Back to top
Other tips
RVM is very easy to master, but it has many broad and deep features that can make development work a breeze. This article only introduces some of the gems found in the utility.
Ideally, your system has all the utilities and packages listed here as a prerequisite before you try to build Ruby. However, if you are unable to install or build any of these packages (for example, because you do not have sufficient permissions to customize your own computer), RVM offers another option. You can install 5 libraries (Readline, IConv, zLib, OpenSSL, and Autoconf) one by one in your $ HOME / .rvm directory as private copies and use RVM as usual.
RVM itself often changes. You can automatically update the RVM software by using the rvm get latest command. The get latest command will refresh your local code with the latest and most stable utility version. If you want to try the advanced features of RVM, enter rvm get head to install the latest version.
If you often create gemsets, you may want a shortcut to create and use gemsets in one step. The rvm gemset use xyz --create command creates the gemset xyz (if it does not exist) and immediately switches to the gemset.
If you use a specific environment for a project, you can add a special dot file to the root of the project to automatically switch to the environment each time you enter the project directory. Just create a .rvmrc file and write the RVM commands commonly used in the command line to switch to the environment you want. For example, if you put rvm ree @ health in a file, RVM will automatically set the correct interpreter and gemset when you change the directory to any folder of the project.
If you choose .rvmrc and Gemfile as source control, the next developer who takes over your work can quickly regenerate all the requirements necessary for development.
Back to top
RVM is the best partner for Ruby developers
RVM is undoubtedly quickly becoming an essential tool. RVM may cancel a small portion of the virtual machine or maintain it as an alternative or additional Ruby development environment. Even better, as Ruby evolves, you can stay in sync with RVM.
Reference materials
Learn more about Yukihiro "Matz" Matsumoto, the Japanese computer scientist who created the Ruby language.
Learn more from the official Ruby website www.ruby-lang.org/en, and learn how to use Ruby coding through its official documentation.
AIX and UNIX zone: The developerWorks "AIX and UNIX zone" provides a lot of information related to all aspects of AIX system management, and you can use them to expand your UNIX skills.
New to AIX and UNIX: Visit the "New to AIX and UNIX" page to learn