Difference between revisions of "Ruby"

From ArchWiki
Jump to: navigation, search
(Bundler)
m (Bundler: It doesn't expand with quotes)
(28 intermediate revisions by 19 users not shown)
Line 1: Line 1:
[[Category:Development (English)]]
+
[[Category:Programming language]]
 
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's a summary of the versions below and how to get them:
+
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) ===
+
=== Ruby 1.9.3 (Stable) ===
'''Summary:''' Use Ruby 1.9 for a new new Rails project, or when you're sure all your required gems are supported by Ruby 1.9
+
'''Summary:''' Ruby 1.9 is recommended usage for new projects.
  
 
Pros:
 
Pros:
Line 17: Line 17:
 
* Changes in the language might cause older Ruby code not to run, or exhibit unexpected bugs.
 
* Changes in the language might cause older Ruby code not to run, or exhibit unexpected bugs.
  
{{Box Note | Visit http://isitruby19.com/ to determine if the gems/modules you require are compatible with Ruby 1.9 }}
+
{{Note|Visit http://isitruby19.com/ to determine if the gems/modules you require are compatible with Ruby 1.9.}}
  
To install Ruby 1.9, simply run:
+
To install Ruby 1.9, simply install {{Pkg|ruby}}.
# pacman -S ruby
+
  
 
Ruby 1.9 also includes RubyGems (detailed below), so you can easily update to the latest RubyGems using:
 
Ruby 1.9 also includes RubyGems (detailed below), so you can easily update to the latest RubyGems using:
 
  # gem update --system
 
  # gem update --system
  
=== Ruby 1.8.7 (Stable) ===
+
=== Ruby 1.8.7 (Deprecated) ===
'''Summary:''' Use Ruby 1.8.7 if you're unsure, or are not working on any legacy systems.
+
'''Summary:''' Use Ruby 1.8.7 with any incompatible or out of date code as necessary.
  
Pros:
+
Last stable version of 1.8, which is incompatible with 1.9. However, there is still code that is based on it.
* Latest version of the 1.8 branch, capable of running most if not all recently updated gems and Ruby On Rails
+
  
Cons:
+
You can install {{AUR|ruby-1.8.7-svn}} or {{AUR|ruby1.8}} from the [[AUR]].
* 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".
+
  
You can install [http://aur.archlinux.org/packages.php?ID=30221 ruby1.8] from the [[AUR]].
+
RubyGems is not included with the {{AUR|ruby1.8}} package, so install {{AUR|rubygems1.8}} from the [[AUR]].
  
RubyGems is not included with the ruby1.8 package, so install [http://aur.archlinux.org/packages.php?ID=30224 rubygems1.8] from the [[AUR]].
+
=== Multiple versions ===
 +
If you want to run multiple versions on the same system (e.g. '''1.9.3''' and '''1.8.7'''), the easiest way is to use [[RVM]] or [[rbenv]].
  
=== Ruby 1.8.6 (Legacy) ===
+
== RubyGems ==
'''Summary:''' Use Ruby 1.8.6 if you are working on old project, older version of Rails (especially < 2.0), or simply want the least possible issues.
+
''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.
  
Pros:
+
=== Running as normal user ===
* Compatible with just about anything
+
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 being 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.
  
Cons:
+
To use gems which install binaries, you need to add {{ic|~/.gem/ruby/1.9.3/bin}} to your {{ic|$PATH}}.
* 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 [http://aur.archlinux.org/packages.php?ID=36898 ruby1.8.6-legacy] from the [[AUR]].
+
This per-user behavior is enabled via {{ic|/etc/gemrc}} and can be overridden by a {{ic|~/.gemrc}} file.
 
+
RubyGems is not included with the ruby1.8.6-legacy package, so install [http://aur.archlinux.org/packages.php?ID=33312 rubygems] from the [[AUR]].
+
 
+
{{Box Note | This package will replace the official "ruby" package, as installing both Ruby 1.8 and 1.9 while possible, can cause erratic behaviour of some 3rd party code }}
+
 
+
=== Multiple versions ===
+
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]].
+
 
+
== RubyGems ==
+
''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.
+
  
 
=== Running as root ===
 
=== Running as root ===
When running gem as root, gems will be installed for everyone on the machine. This has the advantage of simplicity and is the most reliable method, but updating or installing gems without everyone's knowledge (like on a shared server) might cause Ruby applications to break.
+
When running as root, the gems will be installed into {{ic|/root/.gems}} and will '''not''' be installed to {{ic|/usr/lib/ruby/gems/}}.
  
When not running gem as root, the gems will be installed into {{Filename|~/.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).
+
{{Note|See bug #[https://bugs.archlinux.org/task/33327 33327] for more information.}}
  
 
[[Ruby#Bundler|Bundler]] solves these problems to some extent by packaging gems into your application. See the section below on using bundler.
 
[[Ruby#Bundler|Bundler]] solves these problems to some extent by packaging gems into your application. See the section below on using bundler.
  
 
=== Updating RubyGems ===
 
=== Updating RubyGems ===
  # gem update --system
+
  $ gem update
  
 
=== Installing a gem ===
 
=== Installing a gem ===
 
This example installs the MySQL ruby gem:
 
This example installs the MySQL ruby gem:
  # gem install mysql
+
  $ gem install mysql
  
The process can be speeded up somewhat if you don't need local documentation:
+
The process can be sped up somewhat if you do not need local documentation:
  # gem install mysql --no-rdoc --no-ri
+
  $ gem install mysql --no-rdoc --no-ri
  
 
The gem will now be downloaded, compiled if necessary, and installed.
 
The gem will now be downloaded, compiled if necessary, and installed.
  
 
=== Bundler ===
 
=== 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.
+
[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
  
It might be a good idea to install bundler as a system-wide gem so everyone can use it:
+
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}}:
  # gem install bundler
+
  export GEM_HOME=~/.gem/ruby/1.9.3
  
 
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 add your required gems into "Gemfile" in the current directory (created by bundle init):
  gem "rails", "2.3.4"
 
  gem "mysql"
 
  
Finally, run the following to install your gems. They will all be installed locally into {{Filename|~/.bundle}}, eliminating the need for root access:
+
{{hc|Gemfile|
  # bundle install
+
gem "rails", "3.2.9"
 +
gem "mysql"
 +
}}
 +
 
 +
Finally, run the following to install your gems:
 +
$ bundle install
 +
 
 +
Or, alternatively, in order to install locally to {{ic|.bundle}} under the working directory:
 +
  $ bundle install --path .bundle
 +
 
 +
== Managing RubyGems using pacman ==
 +
 
 +
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.
 +
 
 +
{{Warning|Many ruby gem packages in the AUR explicitly use the {{ic|--no-user-install}} or {{ic|--user-install}} command line switches, bypassing the global setting found in {{ic|/etc/gemrc}} or the users own {{ic|~/.gemrc}}. You're editing the PKGBUILD file before you install, right?}}
  
 
== See also ==
 
== See also ==
Line 99: Line 98:
  
 
== References ==
 
== References ==
* Ruby - ruby-lang.org/
+
* Ruby - http://ruby-lang.org/
 
* Rubyforge - http://rubyforge.org
 
* Rubyforge - http://rubyforge.org
 
* Bundler - http://github.com/carlhuda/bundler
 
* Bundler - http://github.com/carlhuda/bundler

Revision as of 01:31, 23 January 2013

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

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.3 (Stable)

Summary: Ruby 1.9 is recommended usage for new projects.

Pros:

  • Vastly improved performance over 1.8
  • New features for concurrency such as fibers.
  • Various other language improvements, such as an improved CSV parser.

Cons:

  • 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 install Ruby 1.9, simply install ruby.

Ruby 1.9 also includes RubyGems (detailed below), so you can easily update to the latest RubyGems using:

# gem update --system

Ruby 1.8.7 (Deprecated)

Summary: Use Ruby 1.8.7 with any incompatible or out of date code as necessary.

Last stable version of 1.8, which is incompatible with 1.9. However, there is still code that is based on it.

You can install ruby-1.8.7-svnAUR or ruby1.8AUR from the AUR.

RubyGems is not included with the ruby1.8AUR package, so install rubygems1.8AUR from the AUR.

Multiple versions

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

RubyGems

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.

Running as normal user

When running gem as a user, the gems will be installed into ~/.gem and not affect anyone else, although it might be worth noting that not all gems are happy with being 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.

To use gems which install binaries, you need to add ~/.gem/ruby/1.9.3/bin to your $PATH.

This per-user behavior is enabled via /etc/gemrc and can be overridden by a ~/.gemrc file.

Running as root

When running as root, the gems will be installed into /root/.gems and will not be installed to /usr/lib/ruby/gems/.

Note: See bug #33327 for more information.

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

Updating RubyGems

$ gem update

Installing a gem

This example installs the MySQL ruby gem:

$ gem install mysql

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

$ gem install mysql --no-rdoc --no-ri

The gem will now be downloaded, compiled if necessary, and installed.

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

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 ~/.bashrc:

export GEM_HOME=~/.gem/ruby/1.9.3

To start a new bundle:

$ bundle init

Then add your required gems into "Gemfile" in the current directory (created by bundle init):

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

Finally, run the following to install your gems:

$ bundle install

Or, alternatively, in order to install locally to .bundle under the working directory:

$ bundle install --path .bundle

Managing RubyGems using pacman

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 pacgemAUR which automatically creates arch packages from gems and installs them afterwards using pacman.

Warning: Many ruby gem packages in the AUR explicitly use the --no-user-install or --user-install command line switches, bypassing the global setting found in /etc/gemrc or the users own ~/.gemrc. You're editing the PKGBUILD file before you install, right?

See also

References