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.
Related
I tried to build a local gem and was surprised to find that even the dependency gems were not installed I can still run gem build .gemspec successfully.
For example, my .gemspec has declared the following dependency and I am sure they are not installed yet. But gem build succeeded and only after I run gem install to install my local gem will those gems be installed as well.
spec.add_runtime_dependency "terminal-notifier-guard"
spec.add_dependency "activesupport", "~> 4.2.0"
From c/c++ background, I find that is a bit confusing. I know ruby is interpreted/dynamic (whatever that means) language, the script will be interpreted when I actually run it. But what does gem build .gemspec build then? What criteria will it use to determine the build is successful (e.g. except for syntax error in codes)?
Dependent gems are installed as remote dependency and are installed when the gem is being installed or during bundle in case of rails. You cannot make it local dependent since it cannot be accessed by others.So your mention of
spec.add_dependency "activesupport", "~> 4.2.0"
in .gemspec file is correct.
.gemspec: Gem build information is stored here. It is a standard format for describing all of the information that gets packed with gems then deployed to rubygems.org.
gem build hola.gemspec
It's just used to build and you need to run gem install to test it.
I'm trying to install a gem that i'm developing from local.
gem install mygem.gemspec
It produces an error:
ERROR: Could not find a valid gem 'mygem.gemspec' (>= 0) in any repository
I can't find any error why it doesn't work, what might be a problem here?
You don’t install the .gemspec file, you use that to build the gem, and then install the resulting gem.
Something like:
$ gem build mygem.gemspec
Successfully built RubyGem
Name: mygem
Version: 1.0.0
File: mygem-1.0.0.gem
and then:
$ gem install mygem-1.0.0.gem
Successfully installed mygem-1.0.0
1 gem installed
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!
How can I make a typical gem setup (as generated by bundle gem) run a customize version of bundler?
I've added:
#group :development do
gem "bundler", github: 'pjump/bundler'
#end
to my Gemfile (with or without the the hash symbols), and bundle install works, but bundle exec keeps telling me that the bundler repo is not yet checked out. The only way I can make it work for now is by installing the customized version with gem istall and not specifying a bundler dependency in the package at all.
Bundler isn't able to bootstrap itself from a Gemfile, so adding a customized version to your Gemfile will not do what you want. Installing it with gem install is the correct solution (or running rake install from the forked Bundler repo directory, which builds and installs the gem in one step).
I am currently working on sample ruby project (not rails).
In the rails project we can use bundle package command for storing installed ruby gems.
It will lock and cache gems from RubyGems into ./vendor/cache. folder.
Now I have to use same functionality. So how can I store gems on local machine and when we do bundle install it will fetch the required ruby gems from that source.
bundler is not part of rails, but it is an independent ruby gem itself. so given you have the bundle command available, you can just set up you Gemfile and use bundle install as you are used to.
This is also described on the bundler homepage
Use Bundler
install bundler from your server prompt (regardless of project folder) to ensure the bundle command is accessible.
gem install bundler
now that your server has bundler installed make a file under the root of your project called Gemfile and add the source and required gems something similar to:
source 'https://rubygems.org'
gem 'example_gem'
gem 'example_gem_with_version', ">=0.9.2"
...
Now your Gemfile is ready. From the root of your project run the bundle install command and specify your vendor directory
bundle install --path vendor
After it retrieves the source it will cache it under the vendor directory. To install locally without fetching from rubygems.org simply use
bundle install --local