Gemspec resolve dependencies - ruby

I wanted to create a gem with some dependencies. I followed a tutorial, here is the full code.
I have
s.add_dependency "sinatra"
In the gemspec. I build the gem. When I tried to install it with
gem install --local gemname.gem
I got
ERROR: Could not find a valid gem 'sinatra' (>= 0) in any repository
I instead expected that gem install will first install sinatra and then proceed with my gem.
How can I make it install any dependencies prior to my gem? I tried to:
add gem 'sinatra' to the gemfile
use add_runtime_dependency instead of add_dependency
require 'rubygems' on top of my gemspec file

TL;DR:
gem install gemname.gem # NO --local switch
When given, --local restricts all the actions to local domain, as clearly written in gem help install output:
Local/Remote Options:
-l, --local Restrict operations to the LOCAL domain
That said, the above will succeed if and only sinatra is already available in local.

Related

Can bundler use locally installed gems when a custom source is specified

I have a project where we are building a custom gem which is hosted on a private gemserver (gemfury). This gem is a dependency in multiple Gemfiles where I need to run tests.
So I have a Gemfile that looks like this:
source "https://rubygems.org"
source 'https://gem.fury.io/custom/' do
gem 'my-custom-gem', '0.0.42'
end
gem 'aws-sdk-iotdataplane', '~>1.15.0'
gem 'bson', '~>4.4.2'
gem 'mongoid', '~>6.1.1'
.
.
.
I build and install the gem locally with rake install (which works fine) and then attempt to run bundle install on the above Gemfile.
I get the following error:
$ bundle install
Fetching gem metadata from https://gem.fury.io/custom/..
Could not find gem 'my-custom-gem (= 0.0.42)' in rubygems repository https://gem.fury.io/custom/ or installed locally.
The source contains the following versions of 'my-custom-gem': 0.0.1, 0.0.4
It is my understanding that if the gem is installed locally on the system, bundler should not attempt to fetch it from the source.
Is this correct?
If I remove the custom source block from around the gem line, it will use the local gem and the fact that the error message says "... or installed locally" really seems to suggest this should work.

How to install gems from Gemfile?

I want to add code coverage to my project and sign up coveralls.io and create Gemfile with:
gem 'coveralls', require: false
but how can I install the gem 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
First install bundler if you don't have it
gem install bundler or sudo gem install bundler if you don't have the required permissions. Bundler is a gem that manages gem dependencies.
then you can follow the above instruction for creating the gemfile, after which you can issue the command
bundle install

Unable to require gem from git with RVM

I cannot require a custom gem I developed to a ruby project. I use RVM. Here's what I've done:
I added gem locally via Gemfile:
gem 'my-gem', git: 'https://github.com/username/my-gem.git'
I installed the gem:
bundle
Fetching https://github.com/username/my-gem.git
Fetching gem metadata from https://rubygems.org/............
Fetching version metadata from https://rubygems.org/..
Resolving dependencies...
Using my-gem 0.1.0 https://github.com/username/my-gem.git (at master#dcdac02)
Using bundler 1.11.2
Bundle complete! 1 Gemfile dependency, 2 gems now installed.
I confirmed it was installed:
bundle show my-gem
/Users/myuser/.rvm/gems/ruby-2.2.2/bundler/gems/my-gem-dcdac02a8b69
I confirmed my gem paths:
GEM PATHS:
- /Users/myuser/.rvm/gems/ruby-2.2.2
- /Users/myuser/.rvm/gems/ruby-2.2.2#global
When I run gem list, my gem is missing. When I require 'my-gem', the gem cannot be found.
When I run gem which my-gem, I get:
ERROR: Can't find ruby library file or shared library my-gem
I'm not really sure what else to try. Any ideas?
Rubygems have no concept of git installed gems, so Bundler includes a specific mechanism for loading these paths into the GEMPATH, you need to do the following before you can require them:
require 'bundler'
Bundler.setup
See the Bundler git gems docs for more info.
I would make sure the installed version of the gem has all the files you expect, especially lib and its contents. This past discussion might help you:
gem which cannot find gem despite it being installed
I found a similar issue
bundle show kubernetes_metadata_filter
/fluentd/vendor/bundle/ruby/2.3.0/bundler/gems/fluent-plugin-kubernetes_metadata_filter-0cd7e29eacec
while the rest of my gems were install here:
/fluentd/vendor/bundle/ruby/2.3.0/gems/
notice the subtle difference between 2.3.0/bundler/gems/... and 2.3.0/gems/......
HACKY SOLUTION:
after bundle install, i did the following:
gem install specific_install
gem specific_install -l <url to a github gem>
That did the trick and installed the gem to the gems directory, and not just bundler. I beleive the correct fix is the project needs to require bundler on startup, then it will get the bundler installed gems as well, but not all projects are well suited for that solution. good luck!

Bundler: install local gem with system gems

I am using rvm, ruby 2.0.0 and bundler.
My Gemfile looks like this:
source 'https://rubygems.org'
gem 'logger'
gem 'mygem', :path => '.'
bundle installs both of them gems. bundle show shows logger is installed in ~/.rvm/gems/ruby-2.0.0-p247/gems, but mygem is installed in the path where the gem is located.
Is there any way to get bundle to install the local gem into rvm's gems directory?
No, Bundler treats path gems differently and does not install them to your GEM_PATH. This is so that you don't need to reinstall as you make changes.
It is not normal or necessary for a gem to point to itself or its runtime dependencies in its Gemfile. You might want to add gemspec to do this automatically. See http://bundler.io/v1.3/rubygems.html

How do I install a Ruby gem from GitHub and have it work in IRB?

Seems like every Ruby tutorial I find centers around Rails.
Anyway, I simply want to install a gem from a GitHub repo and have that gem work in irb.
I want to install the exifr gem. When I do a gem install exifr it doesn't get the newest version.
So I created a Gemfile and put:
gem 'exifr', :git => 'git://github.com/remvee/exifr.git'
Then bundle install. Installs OK but now gem list doesn't find the gem. So I can't require it in irb.
Any help for NON Rails applications?
Thanks
You need to kick off the bundler setup if you want to use the gems from a gemfile:
require 'bundler/setup'
require 'exifr'
or:
irb -rbundler/setup
> require 'exifr'
This is equivalent to running bundle exec irb, except it doesn't depend on a specific invocation to work, and instead presumes that a Gemfile is available and the gems were installed with Bundler.
Try...
bundle exec gem list
And..
bundle exec irb -rexifr
And see if that works. That should help gem/irb find the installed gem.

Resources