Gem Syntax

From Bonus Bits
Jump to: navigation, search

Description

This article gives some of the most common gem commands for quick referencing.


List Installed Gems

gem list


Build Gem

  • Build gem from local
    gem build /path/to/gemspec/GEMNAME.gemspec
    


Install Gem

  • Standard install from rubygems.org
    gem install GEMNAME
    
  • Install local gem
    gem install -l /path/to/gem/GEMNAME.gem
    
  • Without documentation and faster install
    gem install GEMNAME --no-rdoc --no-ri
    

Examples

gem install chef --no-rdoc --no-ri
gem install -l /path/to/file/chef.gem


Install Gem from Git/Github Gem Source Repository

Install from a git URL source repository. No need to pull down the repo and build the gem!

Option 1 (Use specific_install gem)

  1. Install specific_install gem
    gem install specific_install --no-rdoc --no-ri
    
  2. Install gem from git source
    gem specific_install https://github.com/ORGNAME/REPONAME.git
    

Option 2 (Bundler/GemFile)

  1. Install bundler gem if missing
    gem install bundler --no-rdoc --no-ri
    
  2. Add the gem to your GemFile
    vim path/to/project/GemFile
    
    gem 'GEMNAME', :github => 'ORGNAME/REPONAME'
    
    OR
    gem 'GEMNAME', :git => 'git://github.com/ORGNAME/REPONAME.git'
    
  3. Run Bundler from the project folder where the GemFile resides.
    bundle install
    


Search for Gems

When searching you don't have to have the exact name. It regex filters your search. So if you want a list of all the gems with the name chef just enter chef as your search criteria.

Remote Repo Search

You used to have to specific -r for remote search, but now it seems to be a default. Although, it's backwards compatible so can use in any version.

gem search -r GEMNAME

Remote with Details Search

To get more details about the returned found gems you can add the -d argument. It takes longer to get a return. It includes things like Author, Homepage and description.

gem search -r -d GEMNAME

Examples

gem search chef -r -d


Uninstall Gem

gem uninstall GEMNAME

Examples

gem uninstall mongo -xq
gem uninstall mongo -v 1.8.5


Verify Gem Dependencies

Here is one way to quickly check that all the dependencies of the installed Rubygems are met in Bash.

gem list --no-version > ~/list.tmp
gem dependency --pipe > ~/depends.tmp
grep -v -f ~/list.tmp ~/depends.tmp > ~/missing_gem_dependencies.txt
rm -f ~/list.tmp
rm -f ~/depends.tmp


Update Gem

Update Single Gem

gem update GEMNAME

Example

gem update chef --no-rdoc --no-ri

Update all Gems

Beware of updating all your gems. You may run into compatibility issues between gems. This is where bundler or RVM Gemsets are very useful.

gem update

Show RubyGems Environment

gem environment

Example

RubyGems Environment:
  - RUBYGEMS VERSION: 1.8.25
  - RUBY VERSION: 1.9.3 (2013-05-15 patchlevel 429) [x86_64-linux]
  - INSTALLATION DIRECTORY: /home/username/.rvm/gems/ruby-1.9.3-p429
  - RUBY EXECUTABLE: /home/username/.rvm/rubies/ruby-1.9.3-p429/bin/ruby
  - EXECUTABLE DIRECTORY: /home/username/.rvm/gems/ruby-1.9.3-p429/bin
  - RUBYGEMS PLATFORMS:
    - ruby
    - x86_64-linux
  - GEM PATHS:
     - /home/username/.rvm/gems/ruby-1.9.3-p429
     - /home/username/.rvm/gems/ruby-1.9.3-p429@global
  - GEM CONFIGURATION:
     - :update_sources => true
     - :verbose => true
     - :benchmark => false
     - :backtrace => false
     - :bulk_threshold => 1000
  - REMOTE SOURCES:
     - http://rubygems.org/


Add Ruby Gem Internal Source

  1. Add Internal Gem Repo Source
    gem source add http://internal-server/
    
    gem source
    
  2. View Source List
    *** CURRENT SOURCES ***
     http://rubygems.org/
     http://internal-server/
  3. Remove Default Source
    gem source -r http://rubygems.org/
    
  4. View Source List
    gem source
    
    *** CURRENT SOURCES ***
     http://internal-server/
  5. Add Default Source back to end of list
    gem source -a http://rubygems.org/
    
    gem source
    
    *** CURRENT SOURCES ***
     http://internal-server/
     http://rubygems.org/


Update RubyGem Program

gem update --system


Cleanup Older Versions

All

gem cleanup -q

Specific Gem (All Older Versions)

gem cleanup GEMNAME -q

Specific Gem (Specific Version)

gem uninstall GEMNAME --version 1.0.0

Specific Gem (Specific Version Range)

gem uninstall GEMNAME --version '<1.3.4'


List Dependencies of a Gem

gem dep GEMNAME

Example

gem dep chef
Gem chef-11.4.4
  erubis (>= 0)
  highline (>= 1.6.9)
  json (<= 1.7.7, >= 1.4.4)
  mixlib-authentication (>= 1.3.0)
  mixlib-cli (~> 1.3.0)
  mixlib-config (>= 1.1.2)
  mixlib-log (>= 1.3.0)
  mixlib-shellout (>= 0)
  net-ssh (~> 2.6)
  net-ssh-multi (~> 1.1.0)
  ohai (>= 0.6.0)
  rack (>= 0, development)
  rake (>= 0, development)
  rdoc (>= 0, development)
  rest-client (< 1.7.0, >= 1.0.4)
  rspec-core (~> 2.12.0, development)
  rspec-expectations (~> 2.12.0, development)
  rspec-mocks (~> 2.12.0, development)
  rspec_junit_formatter (>= 0, development)
  sdoc (>= 0, development)
  yajl-ruby (~> 1.1)


Install Manual

Usage: gem install GEMNAME [GEMNAME ...] [options] -- --build-flags [options]

  Options:
        --platform PLATFORM          Specify the platform of gem to install
    -v, --version VERSION            Specify version of gem to install
        --[no-]prerelease            Allow prerelease versions of a gem
                                     to be installed. (Only for listed gems)


  Deprecated Options:
    -u, --[no-]update-sources        Update local source cache


  Install/Update Options:
    -i, --install-dir DIR            Gem repository directory to get installed
                                     gems
    -n, --bindir DIR                 Directory where binary files are
                                     located
    -d, --[no-]rdoc                  Generate RDoc documentation for the gem on
                                     install
        --[no-]ri                    Generate RI documentation for the gem on
                                     install
    -E, --[no-]env-shebang           Rewrite the shebang line on installed
                                     scripts to use /usr/bin/env
    -f, --[no-]force                 Force gem to install, bypassing dependency
                                     checks
    -w, --[no-]wrappers              Use bin wrappers for executables
                                     Not available on dosish platforms
    -P, --trust-policy POLICY        Specify gem trust policy
        --ignore-dependencies        Do not install any required dependent gems
    -y, --include-dependencies       Unconditionally install the required
                                     dependent gems
        --[no-]format-executable     Make installed executable names match ruby.
                                     If ruby is ruby18, foo_exec will be
                                     foo_exec18
        --[no-]user-install          Install in user's home directory instead
                                     of GEM_HOME.
        --development                Install any additional development
                                     dependencies
        --conservative               Don't attempt to upgrade gems already
                                     meeting version requirement


  Local/Remote Options:
    -l, --local                      Restrict operations to the LOCAL domain
    -r, --remote                     Restrict operations to the REMOTE domain
    -b, --both                       Allow LOCAL and REMOTE operations
    -B, --bulk-threshold COUNT       Threshold for switching to bulk
                                     synchronization (default 1000)
        --clear-sources              Clear the gem sources
        --source URL                 Add URL as a remote source for gems
    -p, --[no-]http-proxy [URL]      Use HTTP proxy for remote operations


  Common Options:
    -h, --help                       Get help on this command
    -V, --[no-]verbose               Set the verbose level of output
    -q, --quiet                      Silence commands
        --config-file FILE           Use this config file instead of default
        --backtrace                  Show stack backtrace on errors
        --debug                      Turn on Ruby debugging


  Arguments:
    GEMNAME       name of gem to install

  Summary:
    Install a gem into the local repository

  Description:
    The install command installs local or remote gem into a gem repository.

    For gems with executables ruby installs a wrapper file into the executable
    directory by default.  This can be overridden with the --no-wrappers option.
    The wrapper allows you to choose among alternate gem versions using
    _version_.

    For example `rake _0.7.3_ --version` will run rake version 0.7.3 if a newer
    version is also installed.

    If an extension fails to compile during gem installation the gem
    specification is not written out, but the gem remains unpacked in the
    repository.  You may need to specify the path to the library's headers and
    libraries to continue.  You can do this by adding a -- between RubyGems'
    options and the extension's build options:

      $ gem install some_extension_gem
      [build fails]
      Gem files will remain installed in \
      /path/to/gems/some_extension_gem-1.0 for inspection.
      Results logged to /path/to/gems/some_extension_gem-1.0/gem_make.out
      $ gem install some_extension_gem -- --with-extension-lib=/path/to/lib
      [build succeeds]
      $ gem list some_extension_gem

      *** LOCAL GEMS ***

      some_extension_gem (1.0)
      $

    If you correct the compilation errors by editing the gem files you will need
    to write the specification by hand.  For example:

      $ gem install some_extension_gem
      [build fails]
      Gem files will remain installed in \
      /path/to/gems/some_extension_gem-1.0 for inspection.
      Results logged to /path/to/gems/some_extension_gem-1.0/gem_make.out
      $ [cd /path/to/gems/some_extension_gem-1.0]
      $ [edit files or what-have-you and run make]
      $ gem spec ../../cache/some_extension_gem-1.0.gem --ruby > \
                 ../../specifications/some_extension_gem-1.0.gemspec
      $ gem list some_extension_gem

      *** LOCAL GEMS ***

      some_extension_gem (1.0)
      $

  Defaults:
    --both --version '>= 0' --rdoc --ri --no-force
    --install-dir C:/Ruby/lib/ruby/gems/1.9.1


Uninstall Manual

Usage: gem uninstall GEMNAME [GEMNAME ...] [options]

  Options:
    -a, --[no-]all                   Uninstall all matching versions
    -I, --[no-]ignore-dependencies   Ignore dependency requirements while
                                     uninstalling
    -x, --[no-]executables           Uninstall applicable executables without
                                     confirmation
    -i, --install-dir DIR            Directory to uninstall gem from
    -n, --bindir DIR                 Directory to remove binaries from
        --[no-]user-install          Uninstall from user's home directory
                                     in addition to GEM_HOME.
        --[no-]format-executable     Assume executable names match Ruby's prefix and suffix.
    -v, --version VERSION            Specify version of gem to uninstall
        --platform PLATFORM          Specify the platform of gem to uninstall


  Common Options:
    -h, --help                       Get help on this command
    -V, --[no-]verbose               Set the verbose level of output
    -q, --quiet                      Silence commands
        --config-file FILE           Use this config file instead of default
        --backtrace                  Show stack backtrace on errors
        --debug                      Turn on Ruby debugging


  Arguments:
    GEMNAME       name of gem to uninstall

  Summary:
    Uninstall gems from the local repository

  Defaults:
    --version '>= 0' --no-force --install-dir C:/Ruby/lib/ruby/gems/1.9.1
    --user-install


Search Manual

Usage: gem search [STRING] [options]

  Options:
    -i, --[no-]installed             Check for installed gem
    -v, --version VERSION            Specify version of gem to search
                                     for use with --installed
    -d, --[no-]details               Display detailed information of gem(s)
        --[no-]versions              Display only gem names
    -a, --all                        Display all gem versions
        --[no-]prerelease            Display prerelease versions


  Deprecated Options:
    -u, --[no-]update-sources        Update local source cache


  Local/Remote Options:
    -l, --local                      Restrict operations to the LOCAL domain
    -r, --remote                     Restrict operations to the REMOTE domain
    -b, --both                       Allow LOCAL and REMOTE operations
    -B, --bulk-threshold COUNT       Threshold for switching to bulk
                                     synchronization (default 1000)
        --clear-sources              Clear the gem sources
        --source URL                 Add URL as a remote source for gems
    -p, --[no-]http-proxy [URL]      Use HTTP proxy for remote operations


  Common Options:
    -h, --help                       Get help on this command
    -V, --[no-]verbose               Set the verbose level of output
    -q, --quiet                      Silence commands
        --config-file FILE           Use this config file instead of default
        --backtrace                  Show stack backtrace on errors
        --debug                      Turn on Ruby debugging


  Arguments:
    STRING        fragment of gem name to search for

  Summary:
    Display all gems whose name contains STRING

  Defaults:
    --local --no-details


Update Manual

Usage: gem update GEMNAME [GEMNAME ...] [options]

  Options:
        --system [VERSION]           Update the RubyGems system software
        --platform PLATFORM          Specify the platform of gem to update
        --[no-]prerelease            Allow prerelease versions of a gem
                                     as update targets


  Deprecated Options:
    -u, --[no-]update-sources        Update local source cache


  Install/Update Options:
    -i, --install-dir DIR            Gem repository directory to get installed
                                     gems
    -n, --bindir DIR                 Directory where binary files are
                                     located
    -d, --[no-]rdoc                  Generate RDoc documentation for the gem on
                                     install
        --[no-]ri                    Generate RI documentation for the gem on
                                     install
    -E, --[no-]env-shebang           Rewrite the shebang line on installed
                                     scripts to use /usr/bin/env
    -f, --[no-]force                 Force gem to install, bypassing dependency
                                     checks
    -w, --[no-]wrappers              Use bin wrappers for executables
                                     Not available on dosish platforms
    -P, --trust-policy POLICY        Specify gem trust policy
        --ignore-dependencies        Do not install any required dependent gems
    -y, --include-dependencies       Unconditionally install the required
                                     dependent gems
        --[no-]format-executable     Make installed executable names match ruby.
                                     If ruby is ruby18, foo_exec will be
                                     foo_exec18
        --[no-]user-install          Install in user's home directory instead
                                     of GEM_HOME.
        --development                Install any additional development
                                     dependencies
        --conservative               Don't attempt to upgrade gems already
                                     meeting version requirement


  Local/Remote Options:
    -l, --local                      Restrict operations to the LOCAL domain
    -r, --remote                     Restrict operations to the REMOTE domain
    -b, --both                       Allow LOCAL and REMOTE operations
    -B, --bulk-threshold COUNT       Threshold for switching to bulk
                                     synchronization (default 1000)
        --clear-sources              Clear the gem sources
        --source URL                 Add URL as a remote source for gems
    -p, --[no-]http-proxy [URL]      Use HTTP proxy for remote operations


  Common Options:
    -h, --help                       Get help on this command
    -V, --[no-]verbose               Set the verbose level of output
    -q, --quiet                      Silence commands
        --config-file FILE           Use this config file instead of default
        --backtrace                  Show stack backtrace on errors
        --debug                      Turn on Ruby debugging


  Arguments:
    GEMNAME       name of gem to update

  Summary:
    Update the named gems (or all installed gems) in the local repository

  Defaults:
    --rdoc --ri --no-force --install-dir C:/Ruby/lib/ruby/gems/1.9.1


Sources

http://stackoverflow.com/questions/5902488/uninstall-old-ruby-gems-versions
http://stackoverflow.com/questions/4133052/how-do-i-check-that-all-the-dependencies-of-my-installed-ruby-gems-are-satisfied