Rackup: cannot load such file 'sinatra' - ruby

I already installed sinatra gem and in irb, if I type,
require 'sinatra'
It returns true.
But when I do
rackup -d config.ru
It tells me
nil
Exception `LoadError' at /usr/local/lib/ruby/1.9.1/rubygems/custom_require.rb:36 - cannot load such file -- sinatra
Here is my config.ru
require './app'
set :environment, ENV['RACK_ENV'].to_sym
set :app_file, 'app.rb'
disable :run
run Sinatra::Application
app.rb
require 'rubygems'
require 'sinatra'
get '' do
'Hello World'
end
I don't know what is going wrong.
$ which ruby
/usr/local/bin/ruby
$ which rackup
/usr/local/bin/rackup
$ ruby -v
ruby 1.9.3p374 (2013-01-15 revision 38858) [x86_64-linux]
$ rackup -v
Rack 1.2 (Release: 1.5)

I think this is just the verbose output from setting the -d option. Does the server actually start (after producing a load of output)?
What’s happening is this. Using -d sets Ruby’s $DEBUG flag to true. Rack then tries to load the app through the config.ru, which in turn loads your app.rb. The first thing in app.rb is require 'sinatra'.
Rubygems replaces the original require method with its own. When you call require it tries to load the file normally using the existing load path and the original require method. If the gem hasn’t been loaded this will raise a LoadError, which Rubygems catches before loading the gem.
With the $DEBUG flag set, Ruby will produce a message when an exception is raised even though it is rescued and dealt with, and this is what you’re seeing.
To avoid this simply omit the -d flag to your call to rackup (perhaps enabling warnings with -w would give you a sufficiently verbose output without swamping you in too much detail).
If the server isn’t starting then it will be a different issue rather than not finding Sinatra. If that is the case you’ll need to check the rest of the output for clues.
(Note that I originally thought something else was happening, and that’s what my questions int he comments were about.)

my guess is that your rackup script is a binstub of a 'rack' gem
installed in a diff ruby1.9x vm
maybe earlier version of ruby1.9.2
so it can't see the sinatra installed
I'd try 'which rackup' on the command line

This is definitely issue of load path. Anyway try to setup required ruby and gems via RVM and Bundler. It makes sure that Ruby interpreter and load paths are consistent.

Related

Bundler issue when running a ruby script

I am trying to run some older ruby script (with old ruby version) from within a ruby script. Here is a program:
old_ruby187.rb
#!/usr/ruby/1.8.7/bin/ruby
puts "Hello"
new_ruby230.rb
#!/usr/ruby/2.3.0/bin/ruby
require "rubygems"
require "bundler"
Bundler.setup # Code works if I comment this line
puts `old_ruby187.rb`
I get bundler load error. If I execute ./new_ruby230.rb, it gives an error for the last puts command line.:
'require': no such file to load -- rubygems (LoadError)
If I comment just Bundler.setup and run it, it works fine. Not sure if Bundler.setup tries to load something for system call. I need bundler for other gems used in new_ruby230.rb script.
Any help is appreciated.
Update (02/22/2018):
I ended up using ssh when calling old ruby script. Something like:
new_ruby230.rb
#!/usr/ruby/2.3.0/bin/ruby
require "rubygems"
require "bundler"
require "socket"
Bundler.setup
puts `ssh #{Socket.gethostname} old_ruby187.rb` # this worked!
Pretty sure you're not supposed to require rubygems like that. Bundler is there to manage your gems. Remove that require statement (require "rubygems") and you should be good to go.

Require files from gem in Ruby script without bundle exec

I have a script that needs to require specific files out of gems defined in the project Gemfile.
#!/usr/bin/env ruby
require 'some_gem/helpers/some_helper'
... rest of script
When I run the script, I get an error about not being able to load some_helper.rb. If I run with bundle exec command... then everything works.
I understand that bundle exec exposes the Gems to the $LOAD_PATH which lets require work. Is there a way to move that capability into the script so users don't have to type bundle exec?
Do I just need to add require "bundler/setup" to the script before I require the gem files?
http://bundler.io/v1.12/#getting-started
:)
#!/usr/bin/env ruby
require 'rubygems' # because reasons.. most probably it is not needed unless you are using really old ruby where it is not loaded by default
# also at the moment rubygems and bundler are being merged :)
require 'bundler/setup' # for things installed with bundler
require 'some_gem/helpers/some_helper'
You can also check e.g. http://mislav.net/2013/01/understanding-binstubs/

How can I debug require in Ruby 1.9

According to the Tin Man's opinion, i open a new question.
Original questions is here: How does Rubygem require all gems?
Original code i used to debug:
require 'debugger'
debugger
require 'thor'
Here's the dilemma:
debug with the default -rdebug, i cant go to Rubygem's source code
debug with debugger gem, i have to require before i debug (the way i tried in the previous question, which is unacceptable because something important had happened before i get there)
Hope to find a way to debug IT.
When rubygems is required it replaces Ruby’s Kernel.require method with its own that searches for required files in the installed gems. The integration with Ruby 1.9 and above is basically a call to require 'rubygems' during start up. This can be disabled with the (poorly documented) --disable-gems option to the ruby executable. You could make use of this to set up your debugging before explicitly calling require 'rubygems'.
# start with ruby --disable-gems
require 'debug' #standard library debug - doesn't load rubygems
require 'rubygems' #now you can debug this
If you want to use the debugger gem for your debugging it’s still possible but a little trickier as you have to load debugger without loading Rubygems. In order to do this you’ll need to manually set up your load path to include Debugger’s lib dir, as well as the lib dirs of any gems Debugger depends on. This is basically what Rubygems does for you when you call require 'debugger' with Rubygems loaded.
To determine what libs Debugger needs, you can use this command:
ruby -e "lp = $:.dup; gem 'debugger'; puts $: - lp"
This is little Ruby script that first takes a copy of the load path ($: is the load path, you can also use $LOAD_PATH), then activates the Debugger gem, then prints out the difference between the new load path and the original. This will give you the dirs that activating debugger adds to the load path.
On my machine this looks like this:
$ ruby -e "lp = $:.dup; gem 'debugger'; puts $: - lp"
/Users/matt/.rvm/gems/ruby-1.9.3-p385/gems/columnize-0.3.6/lib
/Users/matt/.rvm/gems/ruby-1.9.3-p385/gems/debugger-ruby_core_source-1.2.0/lib
/Users/matt/.rvm/gems/ruby-1.9.3-p385/gems/debugger-linecache-1.2.0/lib
/Users/matt/.rvm/gems/ruby-1.9.3-p385/gems/debugger-1.5.0/lib
You can now use this to create a script to use Debugger to debug require 'rubygems':
# start with ruby --disable-gems
# set up the load path without loading rubygems
$:.unshift '/Users/matt/.rvm/gems/ruby-1.9.3-p385/gems/columnize-0.3.6/lib'
$:.unshift '/Users/matt/.rvm/gems/ruby-1.9.3-p385/gems/debugger-ruby_core_source-1.2.0/lib'
$:.unshift '/Users/matt/.rvm/gems/ruby-1.9.3-p385/gems/debugger-linecache-1.2.0/lib'
$:.unshift '/Users/matt/.rvm/gems/ruby-1.9.3-p385/gems/debugger-1.5.0/lib'
# require debugger and start it
require 'debugger'
debugger
require "rubygems" #now you can debug this with debugger

I see gem in "gem list" but have "no such file to load"

I am on Ubuntu10
sudo apt-get install ruby1.9.1-full
then download sources of rubygem 1.3.7 and install it
sudo ruby setup.rb
then, for example, install sinatra
sudo gem install sinatra
Finally open irb and type
require "rubygems"
require "sinatra"
and get error
LoadError: no such file to load -- sinatra
from (irb):2:in `require'
from (irb):2
from /usr/bin/irb:12:in `<main>'
I had exactly this problem. The problem is that gem and ruby disagree about where the gems live. Compare these:
ruby -e "puts Gem.path"
gem env
gem which sinatra
If you're like my setup, you'll notice that there's an entry in gem env's paths that isn't in Gem.path, and that's exactly where sinatra will claim to be. In my case, I had to add
export GEM_HOME=/usr/lib/ruby/gems/1.9.1
to my .profile. Then everyone was happy.
Execute
sudo gem install sinatra --verbose
and note the path where the gem is getting installed.
Then try this in irb
puts $LOAD_PATH
and make sure that gem is installed in one of the directories in $LOAD_PATH
And ideally just start using http://rvm.beginrescueend.com/
I usually hit this error when I forget:
require 'rubygems'
It'd be helpful if you provided the actual code sample, though, what gem you want to require, and what Ruby version you're using if this doesn't solve the problem.
This was before here on SO quite a few times. Problem is that you probably have two versions of ruby. The one is installing the gem and the other one is trying to use it. Do this in terminal:
$ which -a ruby
Or this:
$ which -a gem
to see if you have more than one version of ruby/gem installed. If so - remove one version (via $ rm or package manager of your system).
I use ruby gems 1.8.7 for a project. I was getting the same error. Use the line require 'rubygems'. It must always be the first require statement, otherwise you can get an error. In my code, I had
require 'watir'
require 'rubygems'
# more code
I got the error - in `require': no such file to load -- watir (LoadError).
When I put rubygems first, the error went away and everything worked. I don't know
why this happens.
Btw, I tried user24359 answer and it did not help me.
C:\code>ruby -e "puts Gem.path"
-e:1: uninitialized constant Gem (NameError)

How to get Sinatra to auto-reload the file after each change?

I am using
# my_app.rb
load 'index.rb'
and start the sever like this
ruby my_app.rb
but it never reload any changes I made in index page.
Did I miss anything here?
See the Sinatra FAQ,
"How do I make my Sinatra app reload on changes?"
First off, in-process code reloading in Ruby is hard and having a
solution that works for every scenario is technically impossible.
Which is why we recommend you to do out-of-process reloading.
First you need to install rerun if you haven’t already:
$ gem install rerun
Now if you start your Sinatra app like this:
$ ruby app.rb
All you have to do for reloading is instead do this:
$ rerun 'ruby app.rb'
If you are for instance using rackup, instead do
the following:
$ rerun 'rackup'
You get the idea.
If you still want in-process reloading, check out Sinatra::Reloader.
gem install sinatra-reloader
require 'sinatra'
require 'sinatra/reloader'
Note: it will reload only sinatra handlers (and, maybe some sinatra server configuration commands), but not custom files, which you have to reload manually.
UPD after 9 years: seems like it is already possible to reload other files using also_reload, dont_reload and after_reload -- https://github.com/sinatra/sinatra/pull/1150
You can use the rerun gem.
gem install rerun
rerun 'ruby app.rb'
OR if you are using rackup
rerun 'rackup'
gem install sinatra-reloader
require 'sinatra/base'
require "sinatra/reloader"
class MyApp < Sinatra::Base
register Sinatra::Reloader
get '/' do
"Hello Testing1!"
end
end
You may want to set environment variable to development and conditionally load the gem.
When you run the application with Passenger Standalone, just create a tmp/always_restart file:
$ touch tmp/always_restart.txt
See Passenger documentation for more info.
I like the Shotgun gem. If you're using a modular Sinatra app and have a config.ru file it's easy to run.
shotgun config.ru
Check the gem out here. It's fairly straight forward and no configuration needed.
On Windows, I am using my restart gem for this:
restart ruby my_app.rb
or, with rackup:
restart rackup
See here for more info, hope you find it useful.
You could use guard-rack. Lifted from an article at dblock.org:
Add this to your Gemfile:
group :development do
gem "guard"
gem "guard-bundler"
gem "guard-rack"
end
Then, create a Guardfile at the root of your project with this content:
guard 'bundler' do
watch('Gemfile')
end
guard 'rack' do
watch('Gemfile.lock')
watch(%r{^(config|app|api)/.*})
end
Lastly, run Guard, like so: bundle exec guard, and rackup will reload every time.
If you only change your templates sinatra will always rerender them if you set your environment to development:
ruby app.rb -e development

Resources