Rickrc northwest
Rickrc northwest

Reputation: 1

LoadError when requiring Sinatra Gem in app

I'm following the tutorial http://learnrubythehardway.org/book/ex50.html, which provides:

create the most basic Sinatra application possible. Put the following code into bin/app.rb:

require 'sinatra'

set :port, 8080
set :static, true
set :public_folder, "static"
set :views, "views"

get '/' do
    return 'Hello world'
end

I installed gem and Sinatra without a problem. I also copied the app.rb from the tutorial into my local bin/app.rb file.

I ran it with: ruby bin/app.rb

The terminal outputs:

/home/t/.rbenv/versions/2.2.0/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in 'require': cannot load such file -- sinatra (LoadError)
    from /home/t/.rbenv/versions/2.2.0/lib/ruby/2.2.0/rubygems/core_ext/kernel_require.rb:54:in 'require'
    from bin/app.rb:1:in `<main>

When I run rake test it passes the test. What's the problem?

Upvotes: 0

Views: 2738

Answers (1)

the Tin Man
the Tin Man

Reputation: 160611

If you're following their directions you ran sudo gem install sinatra. Don't use sudo in your environment because you're using rbenv.

When you used sudo, you had the System Ruby install Sinatra, not your currently selected Ruby as set by rbenv.

Read rbenv's "Installing Ruby gems" documentation.

To fix the problem simply run gem install sinatra, which will install Sinatra and its dependencies in your rbenv-configured Ruby.

That won't remove Sinatra from the System Ruby, but its existence there should be safe and benign.

"Using rbenv doesn't work with sudo?" will help too.

Upvotes: 5

Related Questions