Ruby script cannot locate required gem

15,751

Solution 1

Gems must first be installed before you can require them in a script. To install a gem; first open a terminal window and then type:

gem install GEM-NAME or in your case: gem install mechanize

Solution 2

If you use , add into your link to gem:

gem 'mechanize'

Then issue bundle installation for your or other project folder:

$ bundle install

Make sure that gem is present in the after the bundle installation.

Then even in case if you don't use bundler, make sure that bundle founds the gem:

$ bundle show mechanize
/path/to/mechanize/gem

Then if your app isn't the app, just run your app with:

$ bundle exec ./your_app
Share:
15,751
CodeBiker
Author by

CodeBiker

Full-stack developer with practical experience in Javascript (ES6 and previous), SCSS, Node.js, Express, Python/Django, Ruby on Rails, and testing frameworks including Cypress and Pytest. Passionate about accessibility, responsive and performant technologies, and making things that humans love to use.

Updated on June 04, 2022

Comments

  • CodeBiker
    CodeBiker almost 2 years

    I'm trying to run a Ruby script that has worked fine in the past. I need to use the Mechanize gem, so at the top of the script, it says require 'mechanize'. However, when I try to run the script now, the Terminal output reads:

    /Users/codebiker/.rvm/rubies/ruby-2.1.0/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require': cannot load such file -- mechanize (LoadError)
    from /Users/codebiker/.rvm/rubies/ruby-2.1.0/lib/ruby/site_ruby/2.1.0/rubygems/core_ext/kernel_require.rb:55:in `require'
    from myscript.rb:2:in `<main>'
    

    When I run gem env, as suggested here, I get:

    RubyGems Environment:
      - RUBYGEMS VERSION: 2.2.1
      - RUBY VERSION: 2.1.0 (2013-12-25 patchlevel 0) [x86_64-darwin13.0]
      - INSTALLATION DIRECTORY: /Users/codebiker/.rvm/gems/ruby-2.1.0
      - RUBY EXECUTABLE: /Users/codebiker/.rvm/rubies/ruby-2.1.0/bin/ruby
      - EXECUTABLE DIRECTORY: /Users/codebiker/.rvm/gems/ruby-2.1.0/bin
      - SPEC CACHE DIRECTORY: /Users/codebiker/.gem/specs
      - RUBYGEMS PLATFORMS:
        - ruby
        - x86_64-darwin-13
      - GEM PATHS:
         - /Users/codebiker/.rvm/gems/ruby-2.1.0
         - /Users/codebiker/.rvm/gems/ruby-2.1.0@global
      - GEM CONFIGURATION:
         - :update_sources => true
         - :verbose => true
         - :backtrace => false
         - :bulk_threshold => 1000
         - :benchmark => false
         - :sources => ["http://rubygems.org/", "http://gemcutter.org"]
         - "install" => "--no-rdoc --no-ri"
         - "update" => "--no-rdoc --no-ri"
      - REMOTE SOURCES:
         - http://rubygems.org/
         - http://gemcutter.org
      - SHELL PATH:
         - /Users/codebiker/.rvm/gems/ruby-2.1.0/bin
         - /Users/codebiker/.rvm/gems/ruby-2.1.0@global/bin
         - /Users/codebiker/.rvm/rubies/ruby-2.1.0/bin
         - /usr/local/bin
         - /usr/bin
         - /bin
         - /usr/sbin
         - /sbin
         - /usr/local/bin
         - /opt/X11/bin
         - /usr/texbin
         - /Users/codebiker/.rvm/bin
    

    When I run rvm get head, I get:

    Upgrading the RVM installation in /Users/codebiker/.rvm/
    RVM PATH line found in /Users/codebiker/.bashrc /Users/codebiker/.zshrc.
    RVM sourcing line found in /Users/codebiker/.bash_profile /Users/codebiker/.zprofile.
    Upgrade of RVM in /Users/codebiker/.rvm/ is complete.
    

    When I run which ruby, I get:

    /Users/codebiker/.rvm/rubies/ruby-2.1.0/bin/ruby

    And ruby -v gives me:

    ruby 2.1.0p0 (2013-12-25 revision 44422) [x86_64-darwin13.0]

    Any advice, thoughts, or tips would be appreciated! I have no idea what's wrong here.

  • CodeBiker
    CodeBiker over 10 years
    This completely fixed it -- thanks! However, I'm not sure why Mechanize wasn't there anymore. I've used the Mechanize gem in my scripts on my computer plenty of times before so I just assumed there was no need to install it again. Why would it suddenly disappear? Could this have to do with my recent Ruby 2.1 installation?
  • CodeBiker
    CodeBiker over 10 years
    Great response, thanks! I voted feed_me_code's answer as correct because this is just a script, not a full app, but I appreciate the advice. I am curious if you happen to have an idea of why the gem disappeared. (I would never uninstall it as I use it a lot.)
  • Малъ Скрылевъ
    Малъ Скрылевъ over 10 years
    @CodeBiker If you use rvm, you can install it into other gemset. or if you've used the root installation, it could be treated as an other gemset also.
  • MrPizzaFace
    MrPizzaFace over 10 years
    Yes that is exactly right. Each ruby installation will have an independent directory for gems. Glad I could help. Happy coding!