33

I often see in documentation on the Internet, "put this in the Gemfile". I don't know where and what this "Gemfile" is. If I install a gem then I have installed it. Who need than a "Gemfile"? Where or what is the Gemfile, and why is it used?

Michael Gaskill
  • 7,913
  • 10
  • 38
  • 43
GluecklichesHuhn
  • 331
  • 1
  • 3
  • 3

4 Answers4

17

The Gemfile is wherever you want it to be - usually in the main directory of your project and the name of the file is Gemfile.

It's convenient to have one because it allows you to use Bundler to manage which gems and which versions of each your project needs to run.

If you are not using Bundler (which you should!), then you can just install any gems you come across with gem install X and ignore instructions about adding a line to your Gemfile.

Read more about it here:

Weston
  • 461
  • 3
  • 8
  • okay, okay thanks. Because I have a problem: if I install the gem "battery" then it dont functional, if I require it and make it by the instructions. Because under the instruczions stand "Add this to your Gemfile: gem 'battery'", "then run: bundle". What should I do ? – GluecklichesHuhn May 30 '16 at 15:35
  • @GluecklichesHuhn Please take a look at the "Getting Started" section in the following link. I could not explain it better! http://bundler.io/ – Weston May 31 '16 at 09:35
  • thank you. really help me understand ruby. i'am a newbie – arced Jul 28 '17 at 08:04
5

Gemfile is in Rails project, for Ruby run gem environment to find out about your gem environment:

RubyGems Environment:
  - RUBYGEMS VERSION: 2.4.8
  - RUBY VERSION: 2.2.1 (2015-02-26 patchlevel 85) [i686-linux]
  - INSTALLATION DIRECTORY: /home/gagan/.rvm/gems/ruby-2.2.1
  - RUBY EXECUTABLE: /home/gagan/.rvm/rubies/ruby-2.2.1/bin/ruby
  - EXECUTABLE DIRECTORY: /home/gagan/.rvm/gems/ruby-2.2.1/bin
  - SPEC CACHE DIRECTORY: /home/gagan/.gem/specs
  - SYSTEM CONFIGURATION DIRECTORY: /home/gagan/.rvm/rubies/ruby-2.2.1/etc
  - RUBYGEMS PLATFORMS:
    - ruby
    - x86-linux
  - GEM PATHS:
     - /home/gagan/.rvm/gems/ruby-2.2.1
     - /home/gagan/.rvm/gems/ruby-2.2.1@global
  - GEM CONFIGURATION:
     - :update_sources => true
     - :verbose => true
     - :backtrace => false
     - :bulk_threshold => 1000
  - REMOTE SOURCES:
     - https://rubygems.org/
  - SHELL PATH:
     - /home/gagan/.rvm/gems/ruby-2.2.1/bin
     - /home/gagan/.rvm/gems/ruby-2.2.1@global/bin
     - /home/gagan/.rvm/rubies/ruby-2.2.1/bin
     - /usr/local/heroku/bin
     - /usr/lib/lightdm/lightdm
     - /usr/local/sbin
     - /usr/local/bin
     - /usr/sbin
     - /usr/bin
     - /sbin
     - /bin
     - /usr/games
     - /home/gagan/.rvm/bin
     - /home/gagan/.rvm/bin

Notice the two sections for:

  • INSTALLATION DIRECTORY
  • GEM PATHS
Gagan Gami
  • 10,121
  • 1
  • 29
  • 55
3

Gemfile is a file which must be located in root of your rails project. It is used for describing gem dependencies for Ruby programs.

The first thing in your gemfile is a source in which you tell the Gemfile where to look for gems.

Source can be called as a block and you can have multiple sources in your gemfile.

source "https://my_awesome_source.com" do
  gem "my_gem"
  gem "my_other_gem"
end

Here is some documentation where you can read more about gemfile http://bundler.io/gemfile.html

RubyDigger19
  • 835
  • 13
  • 38
0

Just run gem update --system and you're good to go. It's that easy!!!

msoler
  • 2,930
  • 2
  • 18
  • 30