Where is the "Gemfile" in Ruby? - ruby

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?

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:
http://bundler.io/gemfile.html
http://bundler.io/man/gemfile.5.html

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

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

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

Related

How to install ruby gem without sudo?

Configuring a new machine (Mac OS Mojave - Version 10.14.2).
After installing ruby with rbenv. I'm trying to install some gem and running :
gem install rake bundler rspec rubocop pry pry-byebug hub colored octoki
But its give me the following error :
ERROR: While executing gem ... (TypeError)
incompatible marshal file format (can't be read)
format version 4.8 required; 60.33 given
Here is my Gem env :
RubyGems Environment:
- RUBYGEMS VERSION: 2.7.6
- RUBY VERSION: 2.5.3 (2018-10-18 patchlevel 105) [x86_64-darwin18]
- INSTALLATION DIRECTORY: /Users/elise/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0
- USER INSTALLATION DIRECTORY: /Users/elise/.gem/ruby/2.5.0
- RUBY EXECUTABLE: /Users/elise/.rbenv/versions/2.5.3/bin/ruby
- EXECUTABLE DIRECTORY: /Users/elise/.rbenv/versions/2.5.3/bin
- SPEC CACHE DIRECTORY: /Users/elise/.gem/specs
- SYSTEM CONFIGURATION DIRECTORY: /Users/elise/.rbenv/versions/2.5.3/etc
- RUBYGEMS PLATFORMS:
- ruby
- x86_64-darwin-18
- GEM PATHS:
- /Users/elise/.rbenv/versions/2.5.3/lib/ruby/gems/2.5.0
- /Users/elise/.gem/ruby/2.5.0
- GEM CONFIGURATION:
- :update_sources => true
- :verbose => true
- :backtrace => false
- :bulk_threshold => 1000
- :sources => ["http://gems.rubyforge.org/", "http://gems.github.com"]
- :benchmark => false
- "gem" => "--no-document"
- REMOTE SOURCES:
- http://gems.rubyforge.org/
- http://gems.github.com
- SHELL PATH:
- /Users/elise/.rbenv/versions/2.5.3/bin
- /usr/local/Cellar/rbenv/1.1.1/libexec
- ./bin
- ./node_modules/.bin
- /Users/elise/.rbenv/shims
- /Users/elise/.rbenv/bin
- /usr/local/bin
- /usr/bin
- /bin
- /usr/sbin
- /sbin
- /usr/local/sbin
Does anyone know where this bug comes from?
Thanks for the help.
You have only enable very old (and not maintained) remote gem sources in your gem configuration. This might be caused by some old migrated configuration or by following some very old and outdated advice.
To fix this, you first need to remove the outdated gem sources and then add the only one which should be currently used. For that, you can run the following command from your Terminal:
gem sources --remove http://gems.github.com/
gem sources --remove http://gems.rubyforge.org/
gem sources --add https://rubygems.org/
You have to remove all the gem sources you have and add https://rubygems.org/ instead. Note that http://gems.rubyforge.org/ and http://gems.github.com are permanently dead and should be removed. You can list your sources by running:
gem sources
You should get something like this:
*** CURRENT SOURCES ***
//gems.rubyforge.org/
//gems.github.com
1) Delete all sources:
gem sources -r http://gems.rubyforge.org/
gem sources -r http://gems.github.com
2) Add the right source:
gem sources -a https://rubygems.org/
Also, never sudo gem install
Hope this helps!

How to set a gem binary in path?

I have a gem I have built with an executable. The executable is under the bin directory of my repo and it is defined in the gemspec file:
spec.executables << 'my_gem'
After installing the gem the executable is also installed correctly:
$ ls /Users/myuser/.gems/gems/my_gem-1.0.0/
bin/ lib/
$ ls /Users/myuser/.gems/gems/my_gem-1.0.0/bin/
my_gem*
However I want to add this executable in the path so I can run something along the lines of my_gem or bundle exec my_gem to run the executable from the cli.
What do I need to add to my gemspec file to do this?
Here is my ruby version and ruby gems versions:
$ ruby -v
ruby 2.1.1p76
$ gem -v
2.2.2
Here is my gem env:
$ gem env
RubyGems Environment:
- RUBYGEMS VERSION: 2.2.2
- RUBY VERSION: 2.1.1 (2014-02-24 patchlevel 76) [x86_64-darwin15.0]
- INSTALLATION DIRECTORY: /Users/myuser/.gems
- RUBY EXECUTABLE: /Users/myuser/.rbenv/versions/2.1.1/bin/ruby
- EXECUTABLE DIRECTORY: /Users/myuser/.gems/bin
- SPEC CACHE DIRECTORY: /Users/myuser/.gem/specs
- RUBYGEMS PLATFORMS:
- ruby
- x86_64-darwin-15
- GEM PATHS:
- /Users/myuser/.gems
- /Users/myuser/.gem/ruby/2.1.0
- /Users/myuser/.rbenv/versions/2.1.1/lib/ruby/gems/2.1.0
- GEM CONFIGURATION:
- :update_sources => true
- :verbose => true
- :backtrace => false
- :bulk_threshold => 1000
- :sources => ["https://rubygems.org/", "https://my.org.url/artifactory/api/gems/gem-public/"]
- REMOTE SOURCES:
- https://rubygems.org/
- https://my.org.url/artifactory/api/gems/gem-public/
- SHELL PATH:
- /Users/myuser/.rbenv/versions/2.1.1/bin
- /usr/local/Cellar/rbenv/1.0.0/libexec
- /Users/myuser/.rbenv/shims
- /Users/myuser/.rbenv/bin
- /usr/local/bin
- /usr/bin
- /bin
- /usr/sbin
- /sbin
The weird thing is that I already attempted this with a different gem I created using the gem tutorial and that works just fine.
Update: When debugging this for some reason it's running the lib/my_gem.rb file instead of the bin/my_gem file.
The gem has been installed properly. The issue is actually with the executable.
The executable I created ended with this common if statement:
if $PROGRAM_NAME == __FILE__
main
end
To fix it I just had to change it to this:
main

bundle install using old rubygems version

I'm having what appears to be the exact same problem seen in a 1-month old question that no one has touched. I installed rbenv using homebrew, installed ruby 2.3.1 using rbenv install 2.3.1, installed jekyll and bundler using gem install jekyll and gem install bundler, then within a jekyll project, I typed bundle install. I got the error Rubygems 2.0.14.1 is not threadsafe, so your gems will be installed one at a time. Upgrade to Rubygems 2.1.0 or higher to enable parallel gem installation. When I run gem env, I see this:
RubyGems Environment:
- RUBYGEMS VERSION: 2.6.6
- RUBY VERSION: 2.3.1 (2016-04-26 patchlevel 112) [x86_64-darwin15]
- INSTALLATION DIRECTORY: /Users/lindsb/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0
- USER INSTALLATION DIRECTORY: /Users/lindsb/.gem/ruby/2.3.0
- RUBY EXECUTABLE: /Users/lindsb/.rbenv/versions/2.3.1/bin/ruby
- EXECUTABLE DIRECTORY: /Users/lindsb/.rbenv/versions/2.3.1/bin
- SPEC CACHE DIRECTORY: /Users/lindsb/.gem/specs
- SYSTEM CONFIGURATION DIRECTORY: /Users/lindsb/.rbenv/versions/2.3.1/etc
- RUBYGEMS PLATFORMS:
- ruby
- x86_64-darwin-15
- GEM PATHS:
- /Users/lindsb/.rbenv/versions/2.3.1/lib/ruby/gems/2.3.0
- /Users/lindsb/.gem/ruby/2.3.0
- GEM CONFIGURATION:
- :update_sources => true
- :verbose => true
- :backtrace => false
- :bulk_threshold => 1000
- REMOTE SOURCES:
- https://rubygems.org/
- SHELL PATH:
- /Users/lindsb/.rbenv/versions/2.3.1/bin
- /usr/local/Cellar/rbenv/1.0.0/libexec
- /Users/lindsb/.local/bin
- /usr/local/sbin
- /usr/local/bin
- /Users/lindsb/.rbenv/shims
- /Users/lindsb/.local/bin
- /usr/local/sbin
- /usr/local/bin
- /usr/local/bin
- /usr/bin
- /bin
- /usr/sbin
- /sbin
- /opt/X11/bin
- /Library/TeX/texbin
I don't understand why bundler complains about Rubygems being below 2.1.0 when my Rubygems version is 2.6.6.
There is a Gemfile and a Gemfile.lock in the project directory. Gemfile looks like this:
source 'https://rubygems.org'
# jekyll
gem "jekyll", "3.1.1"
gem "redcarpet"
# compiling less
gem 'therubyracer'
gem 'less'
# minifying
gem 'jekyll-press'
# octokit
gem 'octokit'
gem 'netrc'
Hi I am learing Rails and encountered the same issue and I don't know what the root cause it is. But when I try reinstall bundler again
gem install bundler
and then run
bundle install
everything goes fine now.
I don't know exactly what the issue was, but when I removed Gemfile.lock and reran bundle install everything worked fine. There were probably just some bad constraints or something in Gemfile.lock.

Bundler can't find gem bundler/bower

Environment:
Mac OS X 10.11.3 (El Capitan)
Homebrew 0.9.5
rvm 1.26.11 (same problem occurs with rbenv)
Bundler 1.11.2
Gemfile (excerpt):
source 'https://rubygems.org'
require 'bundler/bower'
asset "jquery", "~2.1.4"`
I get this error:
bundle install
[!] There was an error parsing `Gemfile`:
cannot load such file -- bundler/bower. Bundler cannot continue.
# from /Users/nobby/becompany/website/src/website-static/Gemfile:5
# -------------------------------------------
#
> require 'bundler/bower'
#
# -------------------------------------------
My RubyGems environment is:
- RUBYGEMS VERSION: 2.5.1
- RUBY VERSION: 2.3.0 (2015-12-25 patchlevel 0) [x86_64-darwin15]
- INSTALLATION DIRECTORY: /Users/nobby/.rvm/gems/ruby-2.3.0
- USER INSTALLATION DIRECTORY: /Users/nobby/.gem/ruby/2.3.0
- RUBY EXECUTABLE: /Users/nobby/.rvm/rubies/ruby-2.3.0/bin/ruby
- EXECUTABLE DIRECTORY: /Users/nobby/.rvm/gems/ruby-2.3.0/bin
- SPEC CACHE DIRECTORY: /Users/nobby/.gem/specs
- SYSTEM CONFIGURATION DIRECTORY: /Users/nobby/.rvm/rubies/ruby-2.3.0/etc
- RUBYGEMS PLATFORMS:
- ruby
- x86_64-darwin-15
- GEM PATHS:
- /Users/nobby/.rvm/gems/ruby-2.3.0
- /Users/nobby/.rvm/gems/ruby-2.3.0#global
- GEM CONFIGURATION:
- :update_sources => true
- :verbose => true
- :backtrace => false
- :bulk_threshold => 1000
- REMOTE SOURCES:
- https://rubygems.org/
- SHELL PATH:
- /Users/nobby/.rvm/gems/ruby-2.3.0/bin
- /Users/nobby/.rvm/gems/ruby-2.3.0#global/bin
- /Users/nobby/.rvm/rubies/ruby-2.3.0/bin
- /Users/nobby/.rvm/bin
- /Users/nobby/src/apache/ant/apache-ant-1.9.6/bin
- /usr/local/bin
- /usr/bin
- /bin
- /usr/sbin
- /sbin
Maybe bundler is looking in the wrong gem paths? Is there a way to see which paths it is using?
It works when I manually add the dependencies to LOAD_PATH in Gemfile; maybe this helps to point me to the cause of the problem:
[ 'bundler-bower-0.0.3', 'bower-rails-0.10.0'].each do |dep|
$LOAD_PATH.unshift "/Users/nobby/.rvm/gems/ruby-2.3.0/gems/#{dep}/lib"
end
https://github.com/LTe/bundler-bower
You need to
gem install 'bundler-bower'
before you can require modules from it. Potentially (hopefully) bundler is clever enough to resolve dependency order if you add
gem 'bundler-bower'
to your gemfile and then install it with
bundle install
instead

Rails 4.2 and gem config broken

My OS X ruby dev environment is broken, I don't know why.
I undesrtood that the gem path is wrong, but I don't know how to fix it.
/Users/muqaddar/.rvm/rubies/ruby-2.0.0-p598/lib/ruby/site_ruby/2.0.0/rubygems/dependency.rb:315:in `to_specs': Could not find 'rails' (>= 0) among 14 total gem(s) (Gem::LoadError)
Checked in 'GEM_PATH=/Users/muqaddar/.rvm/gems/ruby-2.0.0-p598:/Users/muqaddar/.rvm/gems/ruby-2.0.0-p598#global', execute `gem env` for more information
from /Users/muqaddar/.rvm/rubies/ruby-2.0.0-p598/lib/ruby/site_ruby/2.0.0/rubygems/dependency.rb:324:in `to_spec'
from /Users/muqaddar/.rvm/rubies/ruby-2.0.0-p598/lib/ruby/site_ruby/2.0.0/rubygems/core_ext/kernel_gem.rb:64:in `gem'
from /usr/bin/rails:22:in `<main>'
Here is my config with gem env:
RubyGems Environment:
- RUBYGEMS VERSION: 2.4.6
- RUBY VERSION: 2.0.0 (2014-11-13 patchlevel 598) [x86_64-darwin14.1.0]
- INSTALLATION DIRECTORY: /Users/muqaddar/.rvm/gems/ruby-2.0.0-p598
- RUBY EXECUTABLE: /Users/muqaddar/.rvm/rubies/ruby-2.0.0-p598/bin/ruby
- EXECUTABLE DIRECTORY: /Users/muqaddar/.rvm/gems/ruby-2.0.0-p598/bin
- SPEC CACHE DIRECTORY: /Users/muqaddar/.gem/specs
- SYSTEM CONFIGURATION DIRECTORY: /Users/muqaddar/.rvm/rubies/ruby-2.0.0-p598/etc
- RUBYGEMS PLATFORMS:
- ruby
- x86_64-darwin-14
- GEM PATHS:
- /Users/muqaddar/.rvm/gems/ruby-2.0.0-p598
- /Users/muqaddar/.rvm/gems/ruby-2.0.0-p598#global
- GEM CONFIGURATION:
- :update_sources => true
- :verbose => true
- :backtrace => false
- :bulk_threshold => 1000
- REMOTE SOURCES:
- https://rubygems.org/
- SHELL PATH:
- /Users/muqaddar/.rvm/gems/ruby-2.0.0-p598/bin
- /Users/muqaddar/.rvm/gems/ruby-2.0.0-p598#global/bin
- /Users/muqaddar/.rvm/rubies/ruby-2.0.0-p598/bin
- /usr/local/mysql/bin
- /usr/local/bin
- /usr/bin
- /bin
- /usr/sbin
- /sbin
- /opt/X11/bin
- /Users/muqaddar/.rvm/bin
I tried many things, reading lots of threads.
- reinstall rvm
- remove .vendor/bundle
- bundle install
...etc
I think the gems are not installed in the right directory. How to change that depending my rvm config ?
You should try and follow these steps if you are using a mac: http://www.installrails.com/
Note that when installing a new gem, you first have to add it to the Gemfile inside your editor. For example: gem 'bootstrap-sass', '~> 3.3.3'
After doing this navigate to your folder using terminal and type: "bundle install". This will install your added gems.

Resources