Difference between revisions of "Ruby"

From ArchWiki
Jump to: navigation, search
m (Use AUR template.)
(Setup: rw)
(92 intermediate revisions by 34 users not shown)
Line 1: Line 1:
[[Category:Programming language]]
+
[[Category:Programming languages]]
{{i18n|Ruby}}
+
[[ja:Ruby]]
 +
[[zh-hans:Ruby]]
 
Ruby is a dynamic, interpreted, open source programming language with a focus on simplicity and productivity.
 
Ruby is a dynamic, interpreted, open source programming language with a focus on simplicity and productivity.
  
 
== Installing Ruby ==
 
== Installing Ruby ==
The version of Ruby you need to install depends on your requirements, as not all 3rd party code is compatible with all versions. Here is a summary of the versions below and how to get them:
 
  
=== Ruby 1.9 (Development) ===
+
For the latest version of Ruby, [[install]] the {{Pkg|ruby}} package. It includes [[#RubyGems|RubyGems]].
'''Summary:''' Use Ruby 1.9 for a new Rails project, or when you are sure all your required gems are supported by Ruby 1.9
 
  
Pros:
+
=== Multiple versions ===
* Vastly improved performance over 1.8
+
 
* New features for concurrency such as fibers.
+
If you want to run multiple versions on the same system (e.g. 2.0.0-p0 and 1.9.3-p392), the easiest way is to use [[RVM]], {{AUR|chruby}} or [[rbenv]].
* Various other language improvements, such as an improved CSV parser.
 
  
Cons:
+
=== Documentation ===
* Not compatible with many older gems (and Ruby On Rails versions prior to 2.3)
 
* Changes in the language might cause older Ruby code not to run, or exhibit unexpected bugs.
 
  
{{Note|Visit http://isitruby19.com/ to determine if the gems/modules you require are compatible with Ruby 1.9.}}
+
To make documentation available through the included {{ic|ri}} command-line tool, install {{Pkg|ruby-docs}}.
 +
You can then query the docs with: {{ic|ri Array}}, {{ic|ri Array.pop}} etc. (much like man-pages)
  
To install Ruby 1.9, simply install {{Pkg|ruby}}.
+
== RubyGems ==
  
Ruby 1.9 also includes RubyGems (detailed below), so you can easily update to the latest RubyGems using:
+
RubyGems is a package manager for Ruby modules (called ''gems''), somewhat comparable to what [[pacman]] is to Arch Linux. It is included in the {{pkg|ruby}} package.
# gem update --system
 
  
=== Ruby 1.8.7 (Stable) ===
+
=== Setup ===
'''Summary:''' Use Ruby 1.8.7 if you are unsure, or are not working on any legacy systems.
 
  
Pros:
+
[[Append]] {{ic|$(ruby -e 'print Gem.user_dir')/bin}} to the {{ic|PATH}} [[environment variable]] to allow RubyGems to be executed:
* Latest version of the 1.8 branch, capable of running most if not all recently updated gems and Ruby On Rails
 
  
Cons:
+
{{hc|1=~/.profile|2=
* Introduces certain breaking changes from 1.8.6, which causes older lesser-maintained gems and Rails prior to 2.0 not to work without "monkey patching".
+
PATH="$(ruby -e 'print Gem.user_dir')/bin:$PATH"
 +
}}
  
You can install {{AUR|ruby-1.8.7-svn}} or {{AUR|ruby1.8}} from the [[AUR]].
+
This is required for executable gems to work without typing out the full location, although libraries will work without having to modify your path.
  
RubyGems is not included with the {{AUR|ruby1.8}} package, so install {{AUR|rubygems1.8}} from the [[AUR]].
+
To allow installing RubyGems through the current [[user]], e.g. on ''Your user account isn't allowed to install to the system RubyGems.'', export {{ic|GEM_HOME}} to the local path:
 +
 +
$ export GEM_HOME=$HOME/.gem
  
=== Ruby 1.8.6 (Legacy) ===
+
You may want to append this variable to {{ic|.profile}} instead:
'''Summary:''' Use Ruby 1.8.6 if you are working on old projects, older version of Rails (especially < 2.0), or simply want the least possible issues.
+
{{hc|1=~/.profile|2=
 +
PATH="$(ruby -e 'print Gem.user_dir')/bin:$PATH"
 +
export GEM_HOME=$HOME/.gem
 +
}}
  
Pros:
+
Use {{ic|gem env}} to view the current RubyGems environment:
* Compatible with just about anything
+
 +
$ gem env
  
Cons:
+
=== Usage ===
* Missing some performance and language enhancements from 1.8.7
 
* Support might be dropped with Rails 3.0 (in Beta at the time of writing)
 
  
You can install {{AUR|ruby1.8.6-legacy}} from the [[AUR]].
+
To see what gems are installed:
 +
$ gem list
  
RubyGems is not included with the ruby1.8.6-legacy package, so install {{AUR|rubygems1.8}} from the [[AUR]].
+
To get information about a gem:
 +
$ gem spec ''gem_name''
  
{{Note|This package will replace the official {{Pkg|ruby}} package, as installing both Ruby 1.8 and 1.9, while possible, can cause erratic behavior of some 3rd party code.}}
+
By default, {{ic|gem list}} and {{ic|gem spec}} use the {{ic|--local}} option, which forces ''gem'' to search only the local system. This can be overridden with the {{ic|--remote}} flag. Thus, to search for the mysql gem:
 +
$ gem list --remote mysql
  
=== Multiple versions ===
+
To install a gem:
If you want to run multiple versions on the same system (e.g. '''1.9.1''' and '''1.8.7'''), the easiest way is to use [[RVM]].
+
$ gem install mysql
  
== RubyGems ==
+
The process can be sped up somewhat if you do not need local documentation:
''gem'' is the package manager of sorts for Ruby modules (called Gems), somewhat comparable to what pacman is to Arch Linux. The ''gem'' command will be installed if you followed the installation instructions above.
+
$ gem install mysql --no-document
  
=== Running as normal user ===
+
{{Note|This can be made the default option by configuring the following {{ic|~/.gemrc}} file:
When running ''gem'' as a user, the gems will be installed into {{ic|~/.gem}} and not affect anyone else, although it might be worth noting that not all gems are happy with be installed in this way, and might insist on being installed by root (especially if they have native extensions).  This is considered the best way to manage gems on Arch.
+
{{hc|~/.gemrc|<nowiki>
 +
gem: --no-document
 +
</nowiki>}}
 +
}}
  
You will need to add {{ic|~/.gem/ruby/1.9.1/bin}} to your $PATH.
+
To update all installed gems:
 +
$ gem update
  
=== Running as root ===
+
=== Installing gems per-user or system-wide ===
When running gem as root, gems will be installed for everyone on the machine. However, this tends to leave out-of-date executables littering the filesystem, and updating or installing gems without everyone's knowledge (like on a shared server) might cause Ruby applications to break.
 
  
[[Ruby#Bundler|Bundler]] solves these problems to some extent by packaging gems into your application. See the section below on using bundler.
+
By default in Arch Linux, when running {{ic|gem}}, gems are installed per-user (into {{ic|~/.gem/ruby/}}), instead of system-wide (into {{ic|/usr/lib/ruby/gems/}}). This is considered the best way to manage gems on Arch, because otherwise they might interfere with gems installed by Pacman.
  
=== Updating RubyGems ===
+
Gems can be installed system wide by running the {{ic|gem}} command as root, appended with the {{ic|--no-user-install}} flag. This flag can be set as default by replacing {{ic|--user-install}} by {{ic|--no-user-install}} in {{ic|/etc/gemrc}} (system-wide) or {{ic|~/.gemrc}} (per-user, overrides system-wide).
$ gem update
 
  
=== Installing a gem ===
+
[[#Bundler|Bundler]] solves these problems to some extent by packaging gems into your application. See the section below on using bundler.
This example installs the MySQL ruby gem:
 
$ gem install mysql
 
  
The process can be sped up somewhat if you do not need local documentation:
+
=== Bundler ===
$ gem install mysql --no-rdoc --no-ri
 
  
The gem will now be downloaded, compiled if necessary, and installed.
+
[http://bundler.io Bundler] allows you to specify which gems your application depends upon, and optionally which version those gems should be. Once this specification is in place, Bundler installs all required gems (including the full gem dependency tree) and logs the results for later inspection. By default, Bundler installs gems into a shared location, but they can also be installed directly into your application. When your application is run, Bundler provides the correct version of each gem, even if multiple versions of each gem have been installed. This requires a little bit of work: applications should be called with {{ic|bundle exec}}, and two lines of boilerplate code must be placed in your application's main executable.
  
=== Bundler ===
+
To install Bundler:
[http://github.com/carlhuda/bundler Bundler] installs gems (including those with native extensions) directly into your application, which works very well for shared hosting and easy deployment of [[Ruby on Rails]] applications for example. Bundler also resolves dependencies as a whole, rather than individually like RubyGems, making things a lot easier.  To install:
 
 
  $ gem install bundler
 
  $ gem install bundler
  
Bundler seems to want to install gems system-wide, contrary to the current default behaviour of ''gem'' itself on Arch. To correct this, add the following line to your {{ic|~/.bashrc}}:
+
By default, Bundler installs gems system-wide, which is contrary to the behaviour of ''gem'' itself on Arch. To correct this, add the following to your {{ic|~/.bashrc}}:
  export GEM_HOME="~/.gem/ruby/1.9.1"
+
  export GEM_HOME=$(ruby -e 'print Gem.user_dir')
  
 
To start a new bundle:
 
To start a new bundle:
 
  $ bundle init
 
  $ bundle init
  
Then add your required gems into "Gemfile" in the current directory (created by bundle init):
+
Then edit {{ic|Gemfile}} in the current directory (created by bundle init) and list your required gems:
 
 
 
{{hc|Gemfile|
 
{{hc|Gemfile|
gem "rails", "2.3.4"
+
gem "rails", "3.2.9"
 
gem "mysql"
 
gem "mysql"
 
}}
 
}}
  
Finally, run the following to install your gems:
+
Run the following to install gems into {{ic|GEM_HOME}}:
 
  $ bundle install
 
  $ bundle install
  
Or, alternatively, in order to install locally to {{ic|vendor/bundle}} under the working directory:
+
Alternatively, run the following to install gems to {{ic|.bundle}} in the working directory:
  $ bundle install --deployment
+
  $ bundle install --path .bundle
 +
 
 +
Don't forget to edit your main executable:
 +
{{bc|#!/usr/bin/env ruby
 +
 
 +
# "This will automatically discover your Gemfile, and make all of the gems in
 +
# your Gemfile available to Ruby." http://bundler.io/rationale.html
 +
require 'bundler/setup'
 +
 
 +
...
 +
}}
 +
 
 +
Finally, run your program:
 +
bundle exec ''main_executable_name.rb''
 +
 
 +
=== Managing RubyGems using pacman ===
 +
 
 +
Instead of managing gems with {{ic|gem}}, you can use [[pacman]], or an [[AUR]] helper. Ruby packages follow the naming convention ruby-''gemname''.
 +
 
 +
This option provides the following advantages:
 +
* Gems are updated along with the rest of your system.
 +
* Installed gems are available system-wide, instead of being available only to the user who installed them.
 +
 
 +
{{Note|There are also tools integrating ''gem'' with ''pacman'' by automatically generating PKGBUILDs for specified gems: see [[Creating packages#PKGBUILD generators]].}}
 +
 
 +
==== Quarry ====
 +
 
 +
Quarry is an opensource tool (GPL3 license) that allows to maintain [http://rubygems.org rubygems] binary repository for Arch Linux, as an easier alternative to building packages manually from the AUR. The source is hosted at [https://github.com/anatol/quarry github].
 +
 
 +
The repository is maintained by Arch developer anatolik at http://pkgbuild.com/~anatolik/quarry/, and is currently for the x86_64 architecture only. It contains many popular gems and new gems can be added upon request.
 +
 
 +
See [[Unofficial user repositories#quarry]] to enable it.
  
== Managing RubyGems using pacman ==
+
Then install required gem {{ic|# pacman -S ruby-$gemname}}.
  
Instead of using the gem command directly you can use pacman to manage the installed gems like normal packages. There are a lot of ruby packages available from [[AUR]]. Ruby packages follow the naming convention ruby-[gemname]. As an alternative you can use the tool {{AUR|pacgem}} which automatically creates arch packages from gems and installs them afterwards using pacman.
+
If you have general questions - send it at the project announcement https://bbs.archlinux.org/viewtopic.php?id=182729<br>
 +
If you have bugreports or code improvements - file at github https://github.com/anatol/quarry
  
 
== See also ==
 
== See also ==
* [[Ruby On Rails]]
 
  
== References ==
+
* [[Ruby on Rails]]
 
* Ruby - http://ruby-lang.org/
 
* Ruby - http://ruby-lang.org/
* Rubyforge - http://rubyforge.org
+
* Bundler - http://bundler.io/
* Bundler - http://github.com/carlhuda/bundler
+
* [[wikipedia:Why's_(poignant)_Guide_to_Ruby|why's (poignant) Guide to Ruby]]
 +
* [http://ruby.learncodethehardway.org/ Learn Ruby The Hard Way]
 +
* [http://blog.hyfather.com/blog/2011/10/18/bundler/ Comparison of Bundler and RVM workflows]

Revision as of 11:54, 28 November 2017

Ruby is a dynamic, interpreted, open source programming language with a focus on simplicity and productivity.

Installing Ruby

For the latest version of Ruby, install the ruby package. It includes RubyGems.

Multiple versions

If you want to run multiple versions on the same system (e.g. 2.0.0-p0 and 1.9.3-p392), the easiest way is to use RVM, chrubyAUR or rbenv.

Documentation

To make documentation available through the included ri command-line tool, install ruby-docs. You can then query the docs with: ri Array, ri Array.pop etc. (much like man-pages)

RubyGems

RubyGems is a package manager for Ruby modules (called gems), somewhat comparable to what pacman is to Arch Linux. It is included in the ruby package.

Setup

Append $(ruby -e 'print Gem.user_dir')/bin to the PATH environment variable to allow RubyGems to be executed:

~/.profile
PATH="$(ruby -e 'print Gem.user_dir')/bin:$PATH"

This is required for executable gems to work without typing out the full location, although libraries will work without having to modify your path.

To allow installing RubyGems through the current user, e.g. on Your user account isn't allowed to install to the system RubyGems., export GEM_HOME to the local path:

$ export GEM_HOME=$HOME/.gem

You may want to append this variable to .profile instead:

~/.profile
PATH="$(ruby -e 'print Gem.user_dir')/bin:$PATH"
export GEM_HOME=$HOME/.gem

Use gem env to view the current RubyGems environment:

$ gem env

Usage

To see what gems are installed:

$ gem list

To get information about a gem:

$ gem spec gem_name

By default, gem list and gem spec use the --local option, which forces gem to search only the local system. This can be overridden with the --remote flag. Thus, to search for the mysql gem:

$ gem list --remote mysql

To install a gem:

$ gem install mysql

The process can be sped up somewhat if you do not need local documentation:

$ gem install mysql --no-document
Note: This can be made the default option by configuring the following ~/.gemrc file:
~/.gemrc
gem: --no-document

To update all installed gems:

$ gem update

Installing gems per-user or system-wide

By default in Arch Linux, when running gem, gems are installed per-user (into ~/.gem/ruby/), instead of system-wide (into /usr/lib/ruby/gems/). This is considered the best way to manage gems on Arch, because otherwise they might interfere with gems installed by Pacman.

Gems can be installed system wide by running the gem command as root, appended with the --no-user-install flag. This flag can be set as default by replacing --user-install by --no-user-install in /etc/gemrc (system-wide) or ~/.gemrc (per-user, overrides system-wide).

Bundler solves these problems to some extent by packaging gems into your application. See the section below on using bundler.

Bundler

Bundler allows you to specify which gems your application depends upon, and optionally which version those gems should be. Once this specification is in place, Bundler installs all required gems (including the full gem dependency tree) and logs the results for later inspection. By default, Bundler installs gems into a shared location, but they can also be installed directly into your application. When your application is run, Bundler provides the correct version of each gem, even if multiple versions of each gem have been installed. This requires a little bit of work: applications should be called with bundle exec, and two lines of boilerplate code must be placed in your application's main executable.

To install Bundler:

$ gem install bundler

By default, Bundler installs gems system-wide, which is contrary to the behaviour of gem itself on Arch. To correct this, add the following to your ~/.bashrc:

export GEM_HOME=$(ruby -e 'print Gem.user_dir')

To start a new bundle:

$ bundle init

Then edit Gemfile in the current directory (created by bundle init) and list your required gems:

Gemfile
gem "rails", "3.2.9"
gem "mysql"

Run the following to install gems into GEM_HOME:

$ bundle install

Alternatively, run the following to install gems to .bundle in the working directory:

$ bundle install --path .bundle

Don't forget to edit your main executable:

#!/usr/bin/env ruby

# "This will automatically discover your Gemfile, and make all of the gems in
# your Gemfile available to Ruby." http://bundler.io/rationale.html
require 'bundler/setup'

...

Finally, run your program:

bundle exec main_executable_name.rb

Managing RubyGems using pacman

Instead of managing gems with gem, you can use pacman, or an AUR helper. Ruby packages follow the naming convention ruby-gemname.

This option provides the following advantages:

  • Gems are updated along with the rest of your system.
  • Installed gems are available system-wide, instead of being available only to the user who installed them.
Note: There are also tools integrating gem with pacman by automatically generating PKGBUILDs for specified gems: see Creating packages#PKGBUILD generators.

Quarry

Quarry is an opensource tool (GPL3 license) that allows to maintain rubygems binary repository for Arch Linux, as an easier alternative to building packages manually from the AUR. The source is hosted at github.

The repository is maintained by Arch developer anatolik at http://pkgbuild.com/~anatolik/quarry/, and is currently for the x86_64 architecture only. It contains many popular gems and new gems can be added upon request.

See Unofficial user repositories#quarry to enable it.

Then install required gem # pacman -S ruby-$gemname.

If you have general questions - send it at the project announcement https://bbs.archlinux.org/viewtopic.php?id=182729
If you have bugreports or code improvements - file at github https://github.com/anatol/quarry

See also