What Is the Use of Gemfile in Rails

What is the use of Gemfile in rails?

During your development in Rails, there will be times where you will want to provide some functionality which is required by you, but either you don't know how to do or you don't want to implement it on your own since a lot of work has been put into its development by talented developers.

These developments which you might need (user authentication, message system, asset handlers, geolocation, pagination system, linking to exterior services such as Amazon AWS, and last but not least Rails itself) are called Ruby Gems. These are ruby software packages, not necessarily relating to Rails, but since Rails is based on Ruby, 98% of the gems can be made availble to your Rails webapp code.

Lots of gems can be found in github, but its funner to search for gems via ruby-gems or ruby-toolbox

Your gemfile is a list of all gems that you want to include in the project.
It is used with bundler (also a gem) to install, update, remove and otherwise manage your used gems.

The gemfile has another purpose - you can group gems in :development, :test, :assets, :production, etc groups and Rails will know when to include the gems. For example:

group :development, :test do
gem "rspec-rails"
gem "factory_girl_rails"
gem "guard-rspec"
end

Note that on Rails 4, the assets group has been deprecated

These gems belong to development environment and the test environment since they are for testing the application. You don't need them available in the production environment (you could, but that will bloat the memory unnecessarily).

So - To use the gemfile, simply write the gem you wish to install such as

gem 'devise'

make sure to install bundler beforehand (in your console/cmd/ssh) with

$ gem install bundler

and then write in the console

bundle install

you will notice another gemfile appears! Gemfile.lock
This file, as you will see if you open it with a text reader, lists all your gems with their version and their dependencies. This will come useful when you need to know which versions of the gems you installed.

For more reading on the Gemfile - read on the bundler page

for information regarding picking a gem you could start with this

Good luck and have fun!


Ok, so whats this Gemfile.lock that got created?

Gemfile.lock, as the name suggests is a locking on all the versions of all the gems that got installed. So if Gemfile is what required to be installed, the lock file is what got installed and what version are actually required to get the app up and running.

If you don't have the gems in that specific version (as specified in Gemfile.lock) rails will complain and you will have to either install the missing gems (via bundle install) or fix any conflicts manually (I believe bundler will give you some clues on that)

Some things to know about Gemfile.lock

  • if you accidently delete it, it will get regenerated when you run bundle install. If you accidently delete Gemfile, you are out of luck.. You should use git :)
  • Heroku doesn't care about Gemfile.lock since it will reinstall all gems. So for Heroku, you must set the gem version you want, or Heroku will always install the latest version of gem, which may cause issues
  • Keep the Gemfile.lock in your project so you will always know what version of gems make your app work properly.

What is the difference between Gemfile and Gemfile.lock in Ruby on Rails

The Gemfile is where you specify which gems you want to use, and lets you specify which versions.

The Gemfile.lock file is where Bundler records the exact versions that were installed. This way, when the same library/project is loaded on another machine, running bundle install will look at the Gemfile.lock and install the exact same versions, rather than just using the Gemfile and installing the most recent versions. (Running different versions on different machines could lead to broken tests, etc.) You shouldn't ever have to directly edit the lock file.

Check out Bundler's Purpose and Rationale, specifically the Checking Your Code into Version Control section.

Difference between Gemfile and Gemfile.lock in Ruby on Rails

Gemfile contains the gems that will be included in your project once you run bundle install. You can group them, as well as specify their origin (where they will be fetched from), and version/branch.

Gemfile.lock is generated by bundler and contains a list of all the gems which are actually installed and their version, and including all their dependencies.

How to create a Gemfile?

The Gemfile is just a text file within your project which contains a list of gems for use in your application.

If you do not have one in your application, you can create the file using an editor of your choice, saving it as Gemfile (with no extension), and in your example, containing:

source 'https://rubygems.org'

gem 'rspec'

When creating a new Rails application, why is there a Gemfile.lock file without running bundle install?

Gemfile.lock is a snapshot of the gems and their versions created when you run bundle install. As explained in the Checking Your Code into Version Control section of the Bundler rationale:

Gemfile.lock makes your application a single package of both your own
code and the third-party code it ran the last time so you know for sure
that everything worked. Specifying exact versions of the third-party
code you depend on in your Gemfile would not provide the same
guarantee, because gems usually declare a range of versions for their
dependencies.

Gems can be installed outside of bundler by RubyGems (e.g. gem install gem_name) but it's best to use RVM which allows you to install separate versions of Ruby and manage individual gemsets for each application as explained in the RVM best practices.

Gemfile.lock Use in Rails?

You should read all the documentation from the bundler gem: http://gembundler.com/

THE GEMFILE.LOCK

When you run bundle install, Bundler will persist the full names and
versions of all gems that you used (including dependencies of the gems
specified in the Gemfile(5)) into a file called Gemfile.lock.

Bundler uses this file in all subsequent calls to bundle install,
which guarantees that you always use the same exact code, even as your
application moves across machines.

Because of the way dependency resolution works, even a seemingly small
change (for instance, an update to a point-release of a dependency of
a gem in your Gemfile(5)) can result in radically different gems being
needed to satisfy all dependencies.

As a result, you SHOULD check your Gemfile.lock into version control.
If you do not, every machine that checks out your repository
(including your production server) will resolve all dependencies
again, which will result in different versions of third-party code
being used if any of the gems in the Gemfile(5) or any of their
dependencies have been updated.

How to install gems from Gemfile?

run the command bundle install in your shell, once you have your Gemfile created.

This command will look your Gemfile and install the relevant Gems on the indicated versions.

The Gemfiles are installed because in your Gemfile you are pointing out the source where the gems can be downloaded from.

Your can create a Gemfile just by typing bundle init in your shell

I add a Gemfile example for your reference:

source "https://rubygems.org"  # where gems will be downloaded from
ruby "2.2.3" # ruby version, change for the one you use

gem "sinatra"
gem "sinatra-flash"
gem "sinatra-partial"
gem "bcrypt"
gem "dm-validations"
gem "dm-transactions"
gem "data_mapper"
gem "dm-postgres-adapter"
gem "pg"
gem "database_cleaner"

group :test do # you can make groups for test, development, production..
gem "rspec"
gem "capybara"
gem "rspec-sinatra"
gem "cucumber"
gem "coveralls", require: false
end


Related Topics



Leave a reply



Submit