Can´t require gems Sinatra - ruby

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.

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.

Ruby twitter-bootstrap-rails

I am just started learning Ruby rails and immediately facing problem.
I am using Windows 8.1, Ruby 2.2.3, Rails 4.2.5.
Problem is I installed twitter-bootstrap-rails gem, like this:
gem install twitter-bootstrap-rails
, and it said me everything have gone well, no problems. But when I type
bundle show twitter-bootstrap-rails
it says me there is no such gem. And for that reason I think I cant use rails generator, bootstrap is not in generators list:
rails g --help
I am completely new in Ruby. Any help will be appreciated. Thank you in advance!
It sounds like what you need to do is add the twitter-bootstrap-rails gem to your Gemfile, and run bundle install.
Although you installed the gem using gem install, bundler doesn't know that because it's not present in your Gemfile.
Once you add the gem to your Gemfile and run bundle install, bundle show should display the path of the installed gem. Your generators should work as well!

Error on require 'motion-cocoapods' with RubyMotion

I just got Ruby motion, and I wanted to try out Cocoapods. I installed it just as it asks on the website:
http://www.rubymotion.com/developer-center/articles/cocoapods/
I add
require 'motion-cocoapods' to my simple 'Hello' project. And I get this error when trying to rake it:
rake aborted!
Unable to activate cocoapods-0.16.1, because rake-10.0.3 conflicts with rake (~> 0.9.4)
I guess this has something to do with my version of rake, but I have no idea what I need to do to fix this problem. Please help!
This is caused by having a version of rake newer than 0.9.x installed. When you just run rake, it loads up the newest version (10.0.3 in your case). Then, when the cocoapod gem tries to load, it tries to activate rake 0.9.x and fails (the ~> 0.9.4 means that it will accept any version starting with 0.9.).
One solution would be to completely remove the rake gem and install the 0.9.4 version explicitly:
gem uninstall rake
gem install rake --version '0.9.6'
However, this could become an issue if you have any other projects that require a newer version of rake. A better solution would be to use Bundler:
gem install bundler
Create a Gemfile in your project folder containing:
source :rubygems
gem 'rake'
gem 'motion-cocoapods'
Add the following to Rakefile, immediately under the require 'motion/project' line:
require 'bundler'
Bundler.require
Then run bundle install from the console. This will lock this specific project on rake 0.9.6. The only catch is that you'll probably need to prefix all of your rake commands with bundle exec.
I was able to solve this issue by following the steps on this japanese blog:
http://blog.amacou.net/post/37702092871/rubymotion-cocoapods-rake
First uninstall:
gem uninstall motion-cocoapods
gem uninstall cocoapods
download cocoapods :
git clone git://github.com/CocoaPods/CocoaPods.git
find the gemspec file
and change this:
s.add_runtime_dependency 'rake', '~> 0.9.4'
to this:
s.add_runtime_dependency 'rake', '> 0.9.4'
then install it as a gem
rake gem:install
then reinstall motion-cocoapods:
gem install motion-cocoapods
My feeling is this is a hack though, and I'm worried it could cause problems else where. If anyone has a better answer, please post it.

Missing gem when running rake

I'm using rbenv with Ruby 1.9.2-p290, Rails 3.1, and the database is MySQL.
When I try
rake db:create
I get the following error:
Could not find multi_json-1.0.3 in any of the sources
I've also tried bundle exec rake db:create.
My GemFile looks like this:
source 'http://rubygems.org'
gem 'rails', '3.1.0'
gem 'mysql2'
gem 'json'
group :assets do
gem 'sass-rails', " ~> 3.1.0"
gem 'coffee-rails', "~> 3.1.0"
gem 'uglifier'
end
gem 'jquery-rails'
Quite old, but in case anyone else comes across this and is looking for the real answer: upgrade your bundler gem.
For me didn't work neither smathy and kalleth answers.
Always getting:
Could not find multi_json-1.3.0 in any of the sources
What it worked for me was deleting Gemfile.lock and running again bundle install
It installed a new version of multi_json:
Installing multi_json (1.3.2)
I ran into this problem too with a Rails 3.1 application + rails engines .
bundle exec rake -T reported the error you're reporting.
What solved it for me was running the following command to tell bundler to install the gems to the local 'vendor/bundle' path within the application with the following command:
bundle install --path=vendor/bundle
After I did that, bundle exec rake -T worked correctly.
I had the same problem, and used Jorge's answer to get to my solution:
bundle update multi_json
This just updates multi_json, and not all the other gems, which would potentially happen when you delete the Gemfile.lock and run "bundle install".
Always run your commands through bundle exec. That way you ensuer that you load the correct environment which is expected by your app. Use it like:
bundle exec rake db:migrate
Also make sure you have actually run bundle install to install all required gems and their dependencies.

Resources