On rhel8, I built and installed a custom gem test1.
$ gem build test1.gemspec
Successfully built RubyGem
Name: test1
Version: 0.0.1
File: test1-0.0.1.gem
$ gem install test1-0.0.1.gem
Successfully installed test1-0.0.1
1 gem installed
# check if gem is installed. All gems are installed in this path
$ gem which test1
/usr/local/share/gems/gems/test1-0.0.1/lib/test1.rb
Now in another custom gem I build, the gemspec file looks like this:
# in test2.gemspec file
Gem::Specification.new do |spec|
spec.name = "test2"
...
spec.add_runtime_dependency "test1", "0.0.1"
...
end
When I try to go install test2 gem it fails and says it could not find the test1 gem which is already installed. This problem started happening on rhel8. I tested on debian and alpine and it had no issues finding and installing the gem.
$ gem build test2.gemspec
Successfully built RubyGem
Name: test2
Version: 0.0.1
File: test2-0.0.1.gem
$ gem install test2-0.0.1.gem
ERROR: Could not find a valid gem 'test1' (= 0.0.1) in any repository
If I change to add_development_dependency, it builds and installs the gem. The image works as expected and has no issues. Does anyone know why its not able to find the gem when I add add_runtime_dependency?
If you are building a private gem that requires another private gem, you will need to define the dependency gem in both the gemspec and the Gemfile, while providing the actual source in the Gemfile.
For example, we are using GemFury to host private gems, and then in any other gem that needs this private gem, we do this:
# Gemfile
source 'https://gem.fury.io/your-user/' do
gem 'your-gem'
end
gemspec
# your-gem.gemspec
s.add_runtime_dependency 'your-gem', '0.0.1'
Related
Description
I'm trying to setup my personal website using GitHub Pages with Jekyll 3.8.5 as described in https://help.github.com/en/github/working-with-github-pages/creating-a-github-pages-site-with-jekyll, but having an issue with the bundler.
Details
Bundle can't find the installed Jekyll 3.8.5
$ bundle exec jekyll 3.8.5 new .
fatal: 'jekyll 3.8.5' could not be found. You may need to install the jekyll-3.8.5 gem or a related gem to be able to use this subcommand.`
Verifying that I actually have jekyll-3.8.5
$ bundle info jekyll
* jekyll (3.8.5)
Summary: A simple, blog aware, static site generator.
Homepage: https://github.com/jekyll/jekyll
Path: /Users/macikportali/.rbenv/versions/2.7.1/lib/ruby/gems/2.7.0/gems/jekyll-3.8.5
Gemfile (installed with bundle install)
source "https://rubygems.org"
# Hello! This is where you manage which Jekyll version is used to run.
# When you want to use a different version, change it below, save the
# file and run `bundle install`. Run Jekyll with `bundle exec`, like so:
#
# bundle exec jekyll serve
#
# This will help ensure the proper Jekyll version is running.
# Happy Jekylling!
# gem "jekyll", "~> 4.0.0"
# gem "jekyll", "~> 3.8.5"
# This is the default theme for new Jekyll sites. You may change this to anything you like.
gem "minima", "~> 2.5"
gem "jekyll-athena"
# Seems this is needed to install a lot of subcommands, see: https://github.com/jekyll/jekyll-compose
gem 'jekyll-compose', group: [:jekyll_plugins]
# If you want to use GitHub Pages, remove the "gem "jekyll"" above and
# uncomment the line below. To upgrade, run `bundle update github-pages`.
# gem "github-pages", group: :jekyll_plugins
# for jekyll 3.8.5
gem "github-pages", "204", group: :jekyll_plugins
# If you have any plugins, put them here!
group :jekyll_plugins do
gem "jekyll-feed", "~> 0.11"
end
# Windows and JRuby does not include zoneinfo files, so bundle the tzinfo-data gem
# and associated library.
install_if -> { RUBY_PLATFORM =~ %r!mingw|mswin|java! } do
gem "tzinfo", "~> 1.2"
gem "tzinfo-data"
end
# Performance-booster for watching directories on Windows
gem "wdm", "~> 0.1.1", :install_if => Gem.win_platform?
Jekyll is used with shims
$ which jekyll
/Users/macikportali/.rbenv/shims/jekyll
My current rbenv version
$ rbenv version
2.7.1 (set by /Users/macikportali/.rbenv/version)
Question
Why bundler cannot see the installed jekyll-3.8.5 gem?
Okey, I think I figured it out. I have two different versions for Jekyll.
$ gem list jekyll
*** LOCAL GEMS ***
jekyll (4.0.1, 3.8.5)
What I did was to add underscores(_) as prefix and postfix to the version, thus, executed
bundle exec jekyll _3.8.5_ new docs
That fixed the problem because I believe this is the convention that gem follows when you have different versions.
Now, I have a different problem which is having 404 page but that's another issue to deal with.
I've written a simple PasswordGenerator gem that I have at ~/workspace/gems/password_generator and have an app at ~/workspace/rubysamples/app where I want to use it. I have a Gemfile, the content of it is this:
gem 'password_generator', path: '~/workspace/gems/password_generator'
I installed it locally, like this:
bundle install --local
Resolving dependencies...
Using bundler 1.16.5
Using password_generator 0.1.0 from source at `~/workspace/gems/password_generator`
Bundle complete! 1 Gemfile dependency, 2 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.
It looks like it's installed locally:
bundle info password_generator
* password_generator (0.1.0)
Summary: Simple password generator
Homepage: https://github.com/jedrekdomanski/password_generator
Path: /home/jedrek/workspace/gems/password_generator
When I try to use it
~/workspace/rubysamples/app/password_reset.rb
PasswordGenerator.generate
I get an error
uninitialized constant PasswordGenerator (NameError)
What am I doing wrong? Am I missing anything?
Here's my gem repo: https://github.com/jedrekdomanski/password_generator
I also tried pointing to my repo and branch in the Gemfile
gem 'password_generator', git: 'git#github.com:jedrekdomanski/password_generator.git', branch: 'master'
but I get the same error message uninitialized constant PasswordGenerator (NameError)
There are potentially two issues. The first is how you are starting Ruby and the second is how you are requiring your module.
First, if you are starting Ruby by running ruby password_reset.rb then you are ignoring the Gemfile. The Gemfile is only used when you're using bundler, so you want to make sure you are starting Ruby by running bundle exec ruby password_reset.rb. This causes bundler to read your Gemfile and execute Ruby in that context.
Second, you're not properly including your module in your Ruby file. Just because you've added the gem to your Gemfile and started Ruby using bundler doesn't mean that the Ruby process knows you intend to use that gem's module; it just makes the module available for use. You might wonder, "Why don't I have to do that in Rails?" Because Rails does that for you automatically via config/application.rb.
Given these two issues, the correct way to accomplish your goal is to configure your app as follows:
First, create your Gemfile:
# Gemfile
gem 'password_generator', path: '~/workspace/gems/password_generator'
Second, create your password_reset.rb file:
# password_reset.rb
# Manually require any libraries that this app will use, even if defined in Gemfile
require 'password_generator'
# Call `puts` so something is printed to the console when this app runs
puts PasswordGenerator.generate
Third, run bundle install to ensure your Gemfile is properly formatted and to generate your Gemfile.lock:
⇒ bundle install
Using bundler 1.16.5
Using password_generator 0.1.0 from source at `../../gems/password_generator`
Bundle complete! 1 Gemfile dependency, 2 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.
Fourth, run bundle exec ruby password_reset.rb and observe the output:
⇒ bundle exec ruby password_reset.rb
kpiDfyTxtdAsKmYuZqmK
Everything works because:
Ruby is started with Bundler
Bundler reads your Gemfile and makes the gems available to Ruby
Your app requires the module from the gem before attempting to use the module
I have a Gem project that has the following in the mygem.gemspec file:
Gem::Specification.new do |s|
s.add_dependency 'some_other_gem'
end
Everything builds and publishes just fine for the Gem.
In my Rails projects that use it, I have the following in my Gemfile:
source 'http://abc123#gem.fury.io/my_user/' do
gem 'mygem', '1.12'
end
When I run my projects, I need to explicitly add the following in the Gemfile or else I wind up with dependency errors:
'some_other_gem', '3.14'
source 'http://abc123#gem.fury.io/my_user/' do
gem 'mygem', '1.12'
end
Project's Rails version is 5.0.6 running on Ruby 2.3.1.
Is there more necessary than just s.add_dependency in my mygem.gemspec file?
It seems that the workflow that I use to build and install gems locally does not translate when the gemspec has platform = "java".
I am using Jruby 1.7.18, installed with rvm
$ which gem
/Users/USERNAME/.rvm/rubies/jruby-1.7.18/bin/gem
When I build a gem with the default platform, everything goes according to plan:
$ grep platform batching_core.gemspec
# spec.platform = "java"
$ gem build batching_core.gemspec
Successfully built RubyGem
Name: batching_core
Version: 0.1.0
File: batching_core-0.1.0.gem
$ gem install batching_core-0.1.0.gem
jar dependencies found on non-java platform gem - do not install jars
Successfully installed batching_core-0.1.0
1 gem installed
Note the success message of "Successfully installed batching_core-0.1.0"
But when my project requires me to use platform=java so that I can require some jar files. I can build the gem, but I cannot install it. The installation ends without the success message, and I cannot require the gem in irb:
$ grep platform batching_core.gemspec
spec.platform = "java"
$ gem build batching_core.gemspec
Successfully built RubyGem
Name: batching_core
Version: 0.1.0
File: batching_core-0.1.0-java.gem
$ gem install batching_core-0.1.0-java.gem
$
^^ Note the lack of output. No success message, no output at all.
$ irb
jruby-1.7.18 :001 > require 'batching_core'
LoadError: no such file to load -- batching_core
from org/jruby/RubyKernel.java:1071:in `require'
from /Users/USERNAME/.rvm/rubies/jruby-1.7.18/lib/ruby/shared/rubygems/core_ext/kernel_require.rb:55:in `require'
I tried the solution suggested here, but got the same result.
Creating a gem using Jruby and not Ruby
EDIT: Here's the project that causes the issue:
https://github.com/ValerieAnne563/so34754484
It looks to me like the gem does not contain the file you're trying to require. Can you check the contents to see if "batching_core.rb" is in the gem's lib dir?
As for the warning...I'm confused about your environment. This is a gem with a jar in it being installed on JRuby? Maybe it would clarify some things if you link to the project or post the gemspec.
I'm building a gem from a currently working ruby program. It's using jruby 1.7.12 and, among other things, does a "require 'yaml". For the gem, my Gemfile contains:
source 'https://rubygems.org'
gemspec
When I run
gem build program.gemspec
that works just fine, but when I run
gem install program-0.15.01.gem
it fails with
ERROR: Could not find a valid gem 'yaml' (>= 0) in any repository
ERROR: Possible alternatives: aml, cyaml, haml, maml, raml
Doesn't make any sense since the yaml module is part of the ruby 1.9.3 standard library.
I've upgraded to the latest rubygems (2.4.5).
What the heck am I missing?
yaml is part of Ruby. There is no yaml gem (see https://rubygems.org/search?query=yaml).
Therefore remove
s.add_runtime_dependency 'yaml'
from your gemspec and just add require 'yaml' to the file in which you want to use YAML.