Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Elastic Beanstalk: can't find gem bundler (>= 0.a) with executable bundle (Gem::GemNotFoundException)

This error message is a well known error message. (see https://bundler.io/blog/2019/01/04/an-update-on-the-bundler-2-release.html for example.) Although I'm getting it with a new Elastic Beanstalk application with Ruby 2.6.1, and bundler 2.0.1. The error is:

  /opt/rubies/ruby-2.6.1/lib/ruby/site_ruby/2.6.0/rubygems.rb:289:in `find_spec_for_exe': can't find gem bundler (>= 0.a) with executable bundle (Gem::GemNotFoundException)
from /opt/rubies/ruby-2.6.1/lib/ruby/site_ruby/2.6.0/rubygems.rb:308:in `activate_bin_path'
from /opt/rubies/ruby-2.6.1/bin/bundle:23:in `<main>' (ElasticBeanstalk::ExternalInvocationError)

I've tried putting the following file: 01_install_bundler.config in the .ebextensions folder:

container_commands:
  01_install_bundler:
    command: "gem install bundler —-version 2.0.1"

Although this never gets run because if I look at the above error, I can see that it is happening during this point in the deploy process:

.../AppDeployStage0/AppDeployPreHook/10_bundle_install.sh] : Activity failed.

(i.e. during the bundle install command of an AppDeployPreHook script). See https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/custom-platform-hooks.html for reference of PlatformHooks.

I'm pretty sure that if I can ensure that the version of bundler being used is at least version 2.0.0, then there won't be a problem. Although I don't know how I can specify that easily. At the moment I'm ssh'ing to the server to /opt/elasticbeanstalk/hooks/appdeploy/pre/ to edit and fiddle with the scripts. Although I obviously need an automated, repeatable way of doing it.

It's frustrating that ruby 2.6.1 isn't choosing bundler version 2.0.0 by default. Any ideas?

==============================

Update:

If I edit the file /opt/elasticbeanstalk/hooks/appdeploy/pre/10_bundle_install.sh

if [ -f Gemfile ]; then
  echo "running 'bundle install' with Gemfile:"
  cat Gemfile

  +++ gem install bundler +++
  if [ -d $EB_APP_STAGING_DIR/vendor/cache ]; then
    bundle install --local
  else
    bundle install
  fi
else
  echo "no Gemfile found! Skipping bundle install stage!"
fi

and add the gem install bundler (without the pluses), then this fixes the problem because it installs the latest bundler, which is 2.0.1. For those who want to know the hack, the commands were:

eb ssh

sudo -i

cd /opt/elasticbeanstalk/hooks/appdeploy/pre

vim 10_bundle_install.sh

The problem with this solution is that it feels like a bit of a hack because it doesn't use .ebextensions. Is there a more proper way of fixing this?

like image 823
stwr667 Avatar asked Mar 26 '19 15:03

stwr667


1 Answers

So here's the programmatic solution to the above problem. Create the below file under .ebextensions/gem_install_bundler.config:

files:
  # Runs before `./10_bundle_install.sh`:
  "/opt/elasticbeanstalk/hooks/appdeploy/pre/09_gem_install_bundler.sh" :
    mode: "000775"
    owner: root
    group: users
    content: |
      #!/usr/bin/env bash

      EB_APP_STAGING_DIR=$(/opt/elasticbeanstalk/bin/get-config container -k app_staging_dir)
      EB_SCRIPT_DIR=$(/opt/elasticbeanstalk/bin/get-config container -k script_dir)
      # Source the application's ruby, i.e. 2.6. Otherwise it will be 2.3, which will give this error: `bundler requires Ruby version >= 2.3.0` 
      . $EB_SCRIPT_DIR/use-app-ruby.sh

      cd $EB_APP_STAGING_DIR
      echo "Installing compatible bundler"
      gem install bundler -v 2.0.1

Then when you next eb deploy, the bundler will have been updated to version 2.0.1, and you won't get the above error again.

More information in the docs here:

https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/custom-platform-hooks.html

and here:

https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/customize-containers-ec2.html#linux-files

Last note: Ensure that you either commit these changes before running eb deploy, or stage them and run eb deploy --staged. See: https://docs.aws.amazon.com/elasticbeanstalk/latest/dg/eb3-cli-git.html. I learned this the hard way!

like image 149
stwr667 Avatar answered Oct 02 '22 14:10

stwr667