Ruby Language Gem Usage Using a Gemfile and Bundler

Help us to keep this website almost Ad Free! It takes only 10 seconds of your time:
> Step 1: Go view our video on YouTube: EF Core Bulk Extensions
> Step 2: And Like the video. BONUS: You can also share it!

Example

A Gemfile is the standard way to organize dependencies in your application. A basic Gemfile will look like this:

source 'https://rubygems.org'

gem 'rack'
gem 'sinatra'
gem 'uglifier'

You can specify the versions of the gem you want as follows:

# Match except on point release. Use only 1.5.X
gem 'rack', '~>1.5.2'
# Use a specific version.
gem 'sinatra', '1.4.7'
# Use at least a version or anything greater.
gem 'uglifier', '>= 1.3.0'

You can also pull gems straight from a git repo:

# pull a gem from github
gem 'sinatra', git: 'https://github.com/sinatra/sinatra.git'
# you can specify a sha
gem 'sinatra', git: 'https://github.com/sinatra/sinatra.git', sha: '30d4fb468fd1d6373f82127d845b153f17b54c51'
# you can also specify a branch, though this is often unsafe
gem 'sinatra', git: 'https://github.com/sinatra/sinatra.git', branch: 'master'

You can also group gems depending on what they are used for. For example:

group :development, :test do
    # This gem is only available in dev and test, not production.
    gem 'byebug'
end

You can specify which platform certain gems should run on if you application needs to be able to run on multiple platforms. For example:

platform :jruby do
  gem 'activerecord-jdbc-adapter'
  gem 'jdbc-postgres'
end

platform :ruby do
  gem 'pg'
end

To install all the gems from a Gemfile do:

gem install bundler
bundle install


Got any Ruby Language Question?