Ruby not recognising gems from local source, included in Gemfile - ruby

I've had some trouble with gems from a local source before, so set up a simple test project. I haven't had simple-rss installed before. This is my Gemfile:
source 'https://rubygems.org'
gem 'simple-rss', :path => '~/code/simple-rss'
I then run bundle install, it tells me:
Using simple-rss 1.2.3 from source at `~/code/simple-rss`
Using bundler 1.12.4
Bundle complete! 1 Gemfile dependency, 2 gems now installed.
So then I make test.rb in the same folder as the Gemfile. The test.rb file only has the require right now:
require 'simple-rss'
I run ruby test.rb, and get this error:
/System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- simple-rss (LoadError)
from /System/Library/Frameworks/Ruby.framework/Versions/2.0/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:55:in `require'
from test.rb:1:in `<main>'
So that's the first evidence of a problem. I then do:
sudo gem install simple-rss
That successfully installs it. I do ruby test.rb again, and get no issues. So it's able to run the gem from the original source once it's been installed with gem install, but not from the local source after running with Bundler.

If you want to use the gems dictated in your Gemfile, I suggest prefixing your commands with bundle exec, eg. bundle exec ruby test.rb. Otherwise your Ruby will not know about them.

Related

`require': cannot load such file -- rack/handler (LoadError)

I'm not a Ruby person, so this may be a 101 question. I'm just trying to use a utility that happens to be written in Ruby.
I'm using tilemaker, a utility in the openstreetmap ecosystem. It creates tiles in the mbtiles format. The repository comes with a simple utility to serve the tiles on a browser to test the files you create. This utility is written in Ruby, and is what I'm having trouble with.
The repo's README has instructions for the server utility. The installation instructions read:
(If you don't already have them, you'll need to install Ruby and the required gems to run the demonstration server. On Ubuntu, for example, sudo apt install sqlite3 libsqlite3-dev ruby ruby-dev and then sudo gem install sqlite3 cgi glug rack.)
I'm on Debian 11 (on Qubes, so I don't mind running sudo gem install as they recommend). I hope this is close enough to Ubuntu but maybe this is related to the problem.
This is what I get:
$ ruby server.rb ~/countries-raster.mbtiles
Starting local server
Traceback (most recent call last):
3: from server.rb:22:in `<main>'
2: from server.rb:118:in `<class:MapServer>'
1: from /usr/lib/ruby/vendor_ruby/rubygems/core_ext/kernel_require.rb:85:in `require'
/usr/lib/ruby/vendor_ruby/rubygems/core_ext/kernel_require.rb:85:in `require': cannot load such file -- rack/handler (LoadError)
What am I missing here? Thanks.
It appears that the issue is that you are using a 3.X release of rack.
According to the CHANGELOG as of version 3.0 Rack::Handler was removed from rack and pulled out into its own gem (rackup).
To resolve the issue you will need to either use an older version of rack
gem 'rack', '~> 2.2'
Or you will need to add the rackup gem as a dependency:
gem 'rack'
gem 'rackup'
Either option will provide access to rack/handler:
rack (2.2.5) - Source
rackup (0.2.3) - Source
for a better setup under your user, this could be done:
ensure to have ruby running under your user by:
ruby -v # 2.7 or higher is better
then create a file besides your script server.rb called Gemfile:
# Gemfile
source "https://rubygems.org"
gem "rack"
gem "sqlite3"
gem "cgi"
gem "glug"
After that, ensure to have bundler installed (this is a gem to manage gem versioning), by:
gem install bundler
If you face a permission error, is because your ruby program is under root user. So you can use via sudo bundle install, but anything else must be run under sudo, or you can install and setup rvm which will install and configure ruby under your user.
After installing you can call:
bundle install
# will install all the gems needed and will lock the latest versions for you inside Gemfile.lock
run your server by:
bundle exec ruby server.rb
by running via bundle exec your telling to ruby to use the gems installed and versioned by the Gemfile.lock. This guarantees to your software to require the specific versions and avoid collisions or anything else.

Bundle install doesn't install gem from private git repo

Something strange is happening, everything used to work fine a few hours ago.
I have a private gem as dependency to a project. I've added the private gem git repo as follows:
gem 'my-awesome-gem', '>=1.2.3', git: 'https://john:pass123#bitbucket.org/johndoe/my-awesome-gem'
When doing bundle install it shows:
...
Using my-awesome-gem 1.2.3 from https://john:pass123#bitbucket.org/johndoe/my-awesome-gem (at master#bc19e27)
Bundle complete! 5 Gemfile dependencies, 21 gems now installed.
Use `bundle info [gemname]` to see where a bundled gem is installed.
But this gem doesn't show when I do gem list. Also, when I do pry -r my-awesome-gem or require it in the project it returns:
! Unable to load application: LoadError: cannot load such file -- my-awesome-gem
/Users/johndoe/.rbenv/versions/2.4.1/lib/ruby/2.4.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- my-awesome-gem (LoadError)
...
I've been playing around for a few hours but can't find the problem. I've tried reinstalling ruby via rbenv. I'm using Ruby 2.4.1 via rbenv.
What am I doing wrong?
When using Bundler you must engage Bundler in any code you use. That means either:
require 'bundler/setup'
require 'my-awesome-gem'
Or:
pry -r bundler/setup -r my-awesome-gem
The gem is installed, it's just not in your $LOAD_PATH until you get Bundler to load in the Gemfile and find all the dependencies.

CanĀ“t require gems Sinatra

I'm trying to build a test site using the Sinatra gem but it seems like I can't find the gem when running the server, even though I can find the version through the terminal.
`require': cannot load such file -- sinatra (LoadError)
I've tried reinstalling all gems, etc but with no success.
Advice?
Running: OS X 10.11
Looks like you need to add gem 'sinatra', '~> 1.4.6' to your Gemfile and run bundle install.

Why does Nokogiri require only work in super-user mode?

I keep getting an error when I run a file called wc.rb. I type:
ruby wc.rb
but I keep getting this error:
/usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in `require': cannot load such file -- nokogiri (LoadError)
from /usr/lib/ruby/2.0.0/rubygems/core_ext/kernel_require.rb:45:in `require'
from wc.rb:3:in `<main>'
However, it works when I run the same script with:
sudo ruby wc.rb
Is there anything I can do so that I don't have to run this script as a super user?
When you run with sudo are you running the same version of Ruby? If sudo ruby -v returns something different than ruby -v, you may have only installed the Nokogiri gem in the Ruby 2.0 gem location, and not for the gem location associated with your system's default version of Ruby.
Update: If you originally installed the Nokogiri gem using sudo, you also may need to uninstall it first using sudo (sudo gem uninstall nokogiri) and re-install it without using sudo (gem install nokogiri) for the permissions to be correct.

Error referencing installed Ruby Gem

Can anyone help with an odd Ruby Gem problem I'm having - note I've installed many gems in the past and never seen this.
I've downloaded the net-ssh and net-sftp gems using the following commands:
sudo gem install net-ssh
sudo gem install net-sftp
both installed successfully. However when I reference the files within my code I get the following error:
./workers/ffmpeg_worker.rb:6:in `require': no such file to load -- net/ssh (LoadError)
from ./workers/ffmpeg_worker.rb:6
from ./workers/tests/test_ffmpeg_worker.rb:3:in `require'
from ./workers/tests/test_ffmpeg_worker.rb:3
from /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake/rake_test_loader.rb:5:in `load'
from /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake/rake_test_loader.rb:5
from /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake/rake_test_loader.rb:5:in `each'
from /Library/Ruby/Gems/1.8/gems/rake-0.8.7/lib/rake/rake_test_loader.rb:5
rake aborted!
Command failed with status (1): [/System/Library/Frameworks/Ruby.framework/...]
Note when I do:
gem list --local
The gems are listed as such:
net-scp (1.0.2)
net-sftp (2.0.2, 1.1.0)
net-ssh (2.0.15, 1.1.2)
net-ssh-gateway (1.0.1)
Any clues?
When I see this sort of thing, it usually comes down to having more than one Ruby installed on the machine (which means more than one gem location, etc.), and different apps or environments getting confused about which one to load.
Check for that first. If that isn't the answer, do a gem which net/ssh to see where RubyGems thinks the gem is installed -- make sure it isn't the local user's .gems directory or something.
If neither one solves the mystery, stick some debugging code into your worker script and have it output its load path ($:) so you can see where it's looking.

Resources