How can I specify a local gem in my Gemfile? - ruby-on-rails

I'd like Bundler to load a local gem. Is there an option for that? Or do I have to move the gem folder into the .bundle directory?

I believe you can do this:
gem "foo", path: "/path/to/foo"

In addition to specifying the path (as Jimmy mentioned) you can also force Bundler to use a local gem for your environment only by using the following configuration option:
$ bundle config set local.GEM_NAME /path/to/local/git/repository
This is extremely helpful if you're developing two gems or a gem and a rails app side-by-side.
Note though, that this only works when you're already using git for your dependency, for example:
# In Gemfile
gem 'rack', :github => 'rack/rack', :branch => 'master'
# In your terminal
$ bundle config set local.rack ~/Work/git/rack
As seen on the docs.

You can also reference a local gem with git if you happen to be working on it.
gem 'foo',
:git => '/Path/to/local/git/repo',
:branch => 'my-feature-branch'
Then, if it changes I run
bundle exec gem uninstall foo
bundle update foo
But I am not sure everyone needs to run these two steps.

In order to use local gem repository in a Rails project, follow the steps below:
Check if your gem folder is a git repository (the command is executed in the gem folder)
git rev-parse --is-inside-work-tree
Getting repository path (the command is executed in the gem folder)
git rev-parse --show-toplevel
Setting up a local override for the rails application
bundle config local.GEM_NAME /path/to/local/git/repository
where GEM_NAME is the name of your gem and /path/to/local/git/repository is the output of the command in point 2
In your application Gemfile add the following line:
gem 'GEM_NAME', :github => 'GEM_NAME/GEM_NAME', :branch => 'master'
Running bundle install should give something like this:
Using GEM_NAME (0.0.1) from git://github.com/GEM_NAME/GEM_NAME.git (at /path/to/local/git/repository)
where GEM_NAME is the name of your gem and /path/to/local/git/repository from point 2
Finally, run bundle list, not gem list and you should see something like this:
GEM_NAME (0.0.1 5a68b88)
where GEM_NAME is the name of your gem
A few important cases I am observing using:
Rails 4.0.2
ruby 2.0.0p247 (2013-06-27 revision 41674) [x86_64-linux]
Ubuntu 13.10
RubyMine 6.0.3
It seems RubyMine is not showing local gems as an external library. More information about the bug can be found here and here
When I am changing something in the local gem, in order to be loaded in the rails application I should stop/start the rails server
If I am changing the version of the gem, stopping/starting the Rails server gives me an error. In order to fix it, I am specifying the gem version in the rails application Gemfile like this:
gem 'GEM_NAME', '0.0.2', :github => 'GEM_NAME/GEM_NAME', :branch => 'master'

You can reference gems with source:
source: 'https://source.com', git repository (:github => 'git/url')
and with local path
:path => '.../path/gem_name'.
You can learn more about [Gemfiles and how to use them]
(https://kolosek.com/rails-bundle-install-and-gemfile) in this article.

If you want the branch too:
gem 'foo', path: "point/to/your/path", branch: "branch-name"

Related

Install a gem locally for development

I'm new to ruby (many years of Python experience) and want to install this gem locally to replace the existing one which I installed using gem install swagger-docs. I have tried everything but cannot get rails to pick up my local version.
If you want your Gemfile to use a local gem, use path like this:
gem "swagger-docs", :path => "/Users/name/my_swagger_fork"
If you want to use a git remote, you can do this:
gem "swagger-docs", :git => "git://github.com/user/swagger.git", :branch => "my-awesome-branch"

Gemfile gem installation and gemspec dependencies

I have an app whose Gemfile requires a gem that is also dependent on another gem that is currently found on github.
So
app/Gemfile reads
gem "my-gem", :git => "git://github.com/MyCompany/my-gem.git"
my-gem/Gemfile reads
gem "my-gem-2", :git => "git#github.com:MyCompany/my-gem-2.git"
my-gem/my-gem.gemspec reads
spec.add_dependency "my-gem-2"
When I run bundle inside of app I get an error that it can't find gem my-gem-2 which is required by my-gem; however, if I put the following line
gem "my-gem-2", :git => "git#github.com:MyCompany/my-gem-2.git"
inside of app/Gemfile then it works fine.
This practice seems redundant as I wouldn't think I'd have to add gem dependencies of another gem into my parent app. Is there something I'm doing wrong here that myapp can't find my-gem-2?
This is just the way it goes - Gemfile dependencies within gems are just for when you're developing that gem. Only the gemspec gets evaluated when the gem is used elsewhere (and gemspecs have no concept of git dependencies), hence only dependencies in the gemspec apply.
So: you will need to have both git references within your app's Gemfile.
As stated in the gem specification, the list of gems that you provide through add_dependency will be use to make sure those are already installed in the system during the installation process (i.e gem install). So this line:
my-gem/my-gem.gemspec reads spec.add_dependency "my-gem-2"
Will trigger the verification of whether or not the gem is installed in the system, but it will not trigger any automatic installation of such gem, as Bundler would do.
This other line (inside of your gem):
gem "my-gem-2", :git => "git#github.com:MyCompany/my-gem-2.git"
Specify that a gem should come from a git repository with a .gemspec at its root.
For more details: Gems from git repositories

Bundler throws No Such file or directory for gem install

In Gemfile,
gem "backup", :git => "git://github.com/tenmiles/backup.git", :ref => "develop"
n local and in staging, bundle install did finish successfully. In production, when bundle install --deployment happens, bundler throws this error
Using backup (3.0.19) from git://github.com/tenmiles/backup.git (at develop)
/usr/local/lib/ruby/site_ruby/1.9.1/rubygems/installer.rb:365:in `initialize': No such file or directory - /home/anand/public_html/myapp/releases/20111113170352/vendor/bundle/ruby/1.9.1/bundler/gems/gems/backup-3.0.19/bin/backup (Errno::ENOENT)
I checked in /home/anand/public_html/myapp/releases/20111113170352/vendor/bundle/ruby/1.9.1/bundler/gems/gems/ and backup-150fb5168ebe is there! Its a gem installed via git. why is bundler looking for backup-3.0.19. How can I refresh backup gem and ask bundler to re install the gem from scratch.
Please help
Try this http://raflabs.com/blogs/silence-is-foo/2010/07/19/installing-a-gem-fork-from-github-source/ U can install the gem into your gem set by the method mentioned in there
I had this issue with 1.0.10, but when I updated the servers to bundler 1.0.21 the problem went away.
i have the some problem with current spork. If you specify a version it should work
gem "backup", '1.0', :git => "git://github.com/tenmiles/backup.git"
it fixed it for me

'bundle install' error for gems using git path Rails 3

Every time I deploy to my production server I get the following error for gems that use a git path:
git://github.com/odorcicd/example.git (at rails3) is not checked out. Please run `bundle install` (Bundler::PathError)
I've found that if I run "bundle install --deployment" it solves this problem. But it installs all my gems again and I have to do it after each deployment. Has anyone found a better solution than this?
This is an example of the using a git path in my Gemfile:
gem 'efax', :git => 'https://github.com/TTDaVeTT/efax.git'
Same issue here: I've got this gem that requires a git origin.
gem "machinist_mongo", :git => "https://github.com/nmerouze/machinist_mongo.git", :require => "machinist/mongoid", :branch => "machinist2"
From what I've found; bundle installs the gem, but not in your gemset - so you have to get rails to include the gems specifically somehow... need help on that part.

How to install Rails 3 master from GitHub

I'm using rvm (Ruby Version Manager) and running Rails 3 RC. I need to test an app to see if a bug has been resolved with a recent commit to Rails 3 master on GitHub.
How do I install Rails 3 master from GitHub and then generate a new app?
I can't use
gem install rails --pre
because I want the edge version, not the release candidate.
Can you suggest a helpful gist or blog post?
All I could find was this:
http://weblog.rubyonrails.org/2010/1/1/getting-a-new-app-running-on-edge
and it is out-of-date.
Thanks!
You can create an empty folder, then put a Gemfile inside, with this:
source 'http://rubygems.org'
gem 'rails', :git => 'git://github.com/rails/rails.git'
Then inside the folder, run:
bundle install
Inside the folder again, run:
bundle exec rails new /path/to_my_new_application/appname
And that's it.
You can do this with your rails 3 app Gemfile. Bundler is able to install directly from github and if you dont specify a branch or tag then it will use master. Add this to your Gemfile after you generate your rails 3 app, and then run bundle install and start up your app. After you bundle install it will show you the commit number in Gemfile.lock.. it should be the latest commit number from the master rails repo. Here is what I do in my Gemfile:
gem 'rails', :git => 'git://github.com/rails/rails.git'
#gem 'rails', '3.0.0.rc'
I just uncomment and comment these 2 lines to switch b/w RC and master... and bundle install.
Alternately, you can clone the repo and then use your local source in the Gemfile:
I think it should look something like this (untested):
gem 'rails', :require => 'rails', :path => "/path_to/rails"
Why not take a look through the commit log here: http://github.com/rails/rails/commits/master before cloning the repository? I don't know what bug you are looking for but remember, you can also look at the "diffs" from each commit to see what has changed.

Resources