Search code examples
ruby-on-railsrubycapistranocapistrano3

Capistrano 3 rbenv issue: no such file to load -- rubygems (LoadError)


I'm having issues with running ruby gem commands while using Capistrano 3 with capistrano-rbenv (I'm assuming it can't find the ruby interpreter), specifically delayed_job. I've posted the necessary files associated with the issue:

`require': no such file to load -- rubygems (LoadError)

Any assistance would be greatly appreciated.

Gemfile

gem 'capistrano', "~> 3.6"
gem 'capistrano-rbenv', '~> 2.0'

Capfile

# Load DSL and set up stages
require "capistrano/setup"

# Include default deployment tasks
require "capistrano/deploy"
require "capistrano/rbenv"

# Load custom tasks from `lib/capistrano/tasks` if you have any defined
Dir.glob("lib/capistrano/tasks/*.rake").each { |r| import r }

deploy.rb

# config valid only for current version of Capistrano
lock '3.6.1'

set :application, 'youtube_rank_tracker'
set :repo_url, './.git'

# Default branch is :master
# ask :branch, `git rev-parse --abbrev-ref HEAD`.chomp

# Default deploy_to directory is /var/www/my_app_name
set :deploy_to, '/dogecoin'

# Default value for :scm is :git
set :scm, :git

# Default value for :format is :airbrussh.
set :format, :airbrussh

# You can configure the Airbrussh format using :format_options.
# These are the defaults.
set :format_options, command_output: true, log_file: 'log/capistrano.log', color: :auto, truncate: :auto

# Default value for :pty is false
set :pty, true

# Default value for :linked_files is []
# append :linked_files, 'config/database.yml', 'config/secrets.yml'

# Default value for linked_dirs is []
# append :linked_dirs, 'log', 'tmp/pids', 'tmp/cache', 'tmp/sockets', 'public/system'

# Default value for default_env is {}
# set :default_env, { path:  '$HOME/.rbenv/shims:$HOME/.rbenv/bin:$PATH' }

# Default value for keep_releases is 5
set :keep_releases, 5

# set :default_env, { path: "$HOME/.rbenv/bin:$PATH" }
set :rbenv_type, :user # or :system, depends on your rbenv setup
set :rbenv_ruby, '2.1.2'
set :rbenv_prefix, "RBENV_ROOT=#{fetch(:rbenv_path)} RBENV_VERSION=#{fetch(:rbenv_ruby)} #{fetch(:rbenv_path)}/bin/rbenv exec"
set :rbenv_map_bins, %w{rake gem bundle ruby rails}
set :rbenv_roles, :all # default value

namespace :deploy do
    desc 'Restart application'
  task :restart do
    on roles(:all) do |host|
        execute :mkdir, "-p #{release_path}/tmp"
      execute :touch, release_path.join('tmp/restart.txt')
      execute "cd #{release_path} && RAILS_ENV=production bin/delayed_job restart;"
      # execute "cd #{release_path} && bundle exec rake -T"
    end
    end
end

after 'deploy:publishing', 'deploy:restart'

Error

he deploy has failed with an error: Exception while executing as root@.....: cd /.... && RAILS_ENV=production bin/delayed_job restart; exit status: 1
cd /.... && RAILS_ENV=production bin/delayed_job restart; stdout: /usr/lib/ruby/vendor_ruby/bundler/shared_helpers.rb:2:in `require': no such file to load -- rubygems (LoadError)
    from /usr/lib/ruby/vendor_ruby/bundler/shared_helpers.rb:2
    from /usr/lib/ruby/vendor_ruby/bundler/setup.rb:1:in `require'
    from /usr/lib/ruby/vendor_ruby/bundler/setup.rb:1

Solution

  • Short answer

    Instead of writing it yourself, use the semi-official Capistrano 3 Delayed Job integration Gem: https://github.com/AgileConsultingLLC/capistrano3-delayed-job

    Long answer

    If you were to actually write this yourself, capistrano-rbenv only affects a small default list of commands, as seen here: https://github.com/capistrano/rbenv/blob/master/lib/capistrano/tasks/rbenv.rake#L47

    In this case, you'd probably want something like the following:

    namespace :deploy do
      desc 'Restart application'
      task :restart do
        on roles(:all) do |host|
          within release_path do
            execute :ruby, "bin/delayed_job", 'restart'
          end
        end
      end
    end
    

    Instead of creating the tmp/ directory, add it to :linked_dirs in your config.

    Also, it looks like you are touching tmp/restart.txt to restart Passenger. Alternatively, use the capistrano-passenger gem to do that. It can run in a newer mode which uses the passenger commands to restart your app, or you can tell it to use the touch file mechanism.

    Finally, instead of using cd ${release_path}, use the within DSL method. That will accomplish the same thing and not break the command map.