Why can't Ruby's bundle install find the gem I created? - ruby

I'm trying to use a Ruby gem that I created (Called "mygem"). But my rails application cannot find it. Can someone please advise why and how to fix it?
Step #1: Verify that mygem exists in my current directory:
$ pwd
/myDir
$ ls mygem-1.7.3.gem
-rw-r--r-- 1 myuser wheel 27136 Aug 6 11:41 mygem-1.7.3.gem
Step #2: Make sure the Gemfile says to pickup my gem from the right place:
$ grep mygem Gemfile
gem "mygem", :path => "/myDir"
Step #3: Do bundle install.
But when I do that, it says it can't find it:
$ bundle install
Fetching gem metadata from http://rubygems.org/........
Fetching gem metadata from http://gems.myserver.com/...
Fetching additional metadata from http://rubygems.org/....
Fetching additional metadata from http://gems.myserver.com/..
Resolving dependencies...
Could not find gem 'mygem (>= 0) ruby' in source at /myDir.
Source does not contain any versions of 'mygem (>= 0) ruby'

Try specifying the version number
gem "mygem", '1.7.3', :path => "/myDir"
Credit goes to this answer

Related

Ruby - Cannot use locally installed gem

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

How to update ruby gem dependency? Must I rebuild it? How?

I have a gem already installed locally, and I wanna add a new dependency into it. I worked with below steps:
Open the gem installation folder as below:
/usr/local/rvm/gems/ruby-2.1.0/gems/nesta-0.10.0
Add new dependency below into gemspec file (nesta.gemspec)
s.add_dependency('stacktracer', '>= 0.0.1')
Append gem 'stacktracer' into Gemfile
Add require "stacktracer" into Rakefile
Run command bundle update nesta
Run command bundle install
Check Gemfile.lock, I already found the new dependency like below:
I have next dependencies:
DEPENDENCIES
debugger
mr-sparkle (>= 0.0.2)
nesta!
rack-test (= 0.6.1)
rspec (~> 2.14.0)
**stacktracer**
test-unit (= 1.2.3)
webrat (~> 0.7.3)
But when I met two problems here:
run command gem dependency nesta, I did not find stacktracer dependency list.
I added require "stacktracer" into app.rb, then run the application, it failed due to below error:
/usr/local/rvm/gems/ruby-2.1.0/gems/nesta-0.10.0/lib/nesta/app.rb:4:in `require': cannot load such file -- stacktracer (LoadError)
Could any one help me to tackle the problems? Is there any approach that could manage to update gem dependency successfully?
You are not supposed to edit the unpacked gems under .../rvm/gems/ruby-2.1.0/gems. If you need to update the alien gem, you are to follow this approach:
Fork the gem source at github
Clone the repo to your local drive
Update whatever you want + update version
Set the dependency in your Gemfile to either use unpacked version or your git:
gem 'nesta', :path => '.../nesta.git'
gem 'nesta', :git => 'git://...'
Don’t forget to eliminate the original gem presence over your system. Hope it helps.

Can't find rubygem when it is clearly there?

I've just made a little gem and pushed it to Rubygems.org:
https://rubygems.org/gems/fragrant_wind
However, I can't seem to download it again, any ideas?
$ gem install fragrant_wind
ERROR: Could not find a valid gem 'fragrant_wind' (>= 0) in any repositor
Try fixing your gem name. Choose either underscores or hyphens, but don't mix them.
For example you have:
# .gemspec
s.name = 'fragrant-wind'
Better is:
# fragrant_wind.gemspec
s.name = 'fragrant_wind'
Edit 1: it works for me to download then install, but it doesn't work via RubyGems.
$ gem install fragrant_wind-0.0.2.gem
Successfully installed fragrant_wind-0.0.2
1 gem installed
Installing ri documentation for fragrant_wind-0.0.2...
Building YARD (yri) index for fragrant_wind-0.0.2...
Installing RDoc documentation for fragrant_wind-0.0.2...
$ irb
> require 'fragrant_wind'
=> true
> FragrantWind.new.generate
=> "billowing waterfall"
Edit 2: it works via bundler too.(Thanks to #Bitterzoet for this)
# Gemfile
source 'https://rubygems.org'
gem "fragrant_wind"
$ bundle install
Fetching gem metadata from https://rubygems.org/..
Installing fragrant_wind (0.0.2)
Using bundler (1.2.3)
Your bundle is complete! ...
Edit 3: it works now as expected (Monday 10 a.m. PST). My guess is Rubygems was having a delay or glitch due to its own update last night.

Installing local Ruby gem, using bundler

Our Gemfile is as follows:
source "http://rubygems.org"
gem 'langrove', :path => "/home/user/gems/"
The .gem file is in place in "/home/user/gems/ItIsHere.gem"
But when we run bundle install, the following error is received:
Could not find gem 'ItIsHere (>= 0) ruby' in source at /home/user/gems.
Source does not contain any versions of 'ItIsHere (>= 0) ruby'
Thank You
One solution is to run gem unpack on the specific .gem you want to use
Then reference the unpacked version.
I normally keep things relative to my project
eg:
gem 'awesomelib', :path => '../awesomelib'
You didn't specify the gem version.For this reason you got error.so you will change the code like this.
gem 'remotipart', '1.0.5', :path => "/home/user/gems/"
And check your folder :path also.
first
rails new app_name --skip-bundle
then
use bundle install --local
This will install gems from local.

Bundler and Heroku: conditional gems / two different versions of a gem

I have a gem that is not public and not on a publicly accessible repo (it is on the local filesystem) that I wish to use in a Heroku hosted app.
Bundler does not even allow this, for example:
group :production do
gem 'mygem', :git => #giturl
end
group :development do
gem "mygem", :require => "mygem", :path => "/gem_dev/mygem"
end
$ bundle install
...
You cannot specify the same gem twice coming from different sources. You specified that mygem (>= 0) should come from source at vendor/cache and source at ...
I've used bundle install --path vendor and bundle package to try and get it to use the cache, but since the gem is a local path Bundler tells me (helpfully) that it won't cache it. To get around this I copied the .gem to vendor/cache and had the line in Gemfile:
gem 'mygem', :path => 'vendor/cache'
but I get this error from Bundler:
Could not find gem 'mygem (>= 0) ruby' in source at vendor/cache.
Source does not contain any versions of 'mygem (>= 0) ruby'
Heroku needs a valid path. Any ideas how I can get this to work for me?
Any help is much appreciated.
This can't be done with the current version (1.0.x). From http://gembundler.com/man/bundle-package.1.html
GIT AND PATH GEMS In Bundler 1.0, the bundle package command only
packages .gem files, not gems specified using the :git or :path
options. This will likely change in the future.
What follows is my opinion:
Why not? That surely wasn't a technical decision so I'm... aggrieved... Bundler is supposed to solve problems, and since it's written in Ruby by a couple of well known rubyists you'd expect (or I would) that they'd have taken the route that Ruby core has - we're adults, let us choose what we really want to do, regardless of whether the computer believes otherwise.
If I'm using Bundler and want to install a thousand different versions of a gem then that should be my business. A warning would've done. Let's hope the next version doesn't have this strange decision included in the code.

Resources