0
votes

I have used bundler "bundle install --path vendor/bundle" to install my gem mysql2 in my project.

My Gemfile was like this

source 'https://rubygems.org'
gem 'mysql2', '~> 0.3.20'

Now i want to require this gem in my ruby script but it is not working. Always shows "no such file mysql2".

My code is like this

require 'json'
    load_paths = Dir.pwd + "/vendor/bundle/ruby/2.5.0/gems/"
    $LOAD_PATH.unshift(*load_paths)
    require 'mysql2'

def lambda_handler(event:, context:)
    # # TODO implement
    @db_host  = "host"
    @db_user  = "user"
    @db_pass  = "pass"
    @db_name = "db"

    client = Mysql2::Client.new(:host => @db_host, :username => @db_user, :password => @db_pass, :database => @db_name)
    @cdr_result = client.query("SELECT count(*) from table_name")
    puts @cdr_result
    { statusCode: 200, body: JSON.generate('Hello from Lambda!') }

 end

error is comming like this

Traceback (most recent call last):
        2: from replaceFile.rb:4:in `<main>'
        1: from /home/vagrant/.rvm/rubies/ruby-2.5.0/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require'
/home/vagrant/.rvm/rubies/ruby-2.5.0/lib/ruby/2.5.0/rubygems/core_ext/kernel_require.rb:59:in `require': cannot load such file -- mysql2 (LoadError)

I am using ruby version = ruby 2.5.0p0 (2017-12-25 revision 61468) [x86_64-linux]

1

1 Answers

0
votes

Using options is not enough : you need to configure bundler so it can remember it. Here is what the documentation says :

Flags passed to bundle install or the Bundler runtime, such as --path foo or 
--without production, are not remembered between commands. If these options 
must be remembered, they must be set using bundle config (e.g., bundle config 
path foo).

Until you do that, bundler is going to ignore the local installation that you did and still try to fetch gems like it does usually i.e. not in the vendor/bundle path. Hence the failure