Problems starting application deployment in RAILS 7 with capistrano - ruby-on-rails

I was following a tutorial to test how to deploy an application in Rails but when I tried to run it, it generated several errors that I was solving, first I clarify that I am working locally from windows, and the server I am using as a target for testing is an ubuntu 18 instance of AWS. However I put the unicorn gem with this code in the gemfile.
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
ruby "3.1.2"
# Bundle edge Rails instead: gem "rails", github: "rails/rails", branch: "main"
gem "rails", "~> 7.0.4"
# The original asset pipeline for Rails [https://github.com/rails/sprockets-rails]
gem "sprockets-rails"
# Use mysql as the database for Active Record
gem "mysql2", "~> 0.5"
# Use the Puma web server [https://github.com/puma/puma]
gem "puma", "~> 5.0"
# Use JavaScript with ESM import maps [https://github.com/rails/importmap-rails]
gem "importmap-rails"
# Hotwire's SPA-like page accelerator [https://turbo.hotwired.dev]
gem "turbo-rails"
# Hotwire's modest JavaScript framework [https://stimulus.hotwired.dev]
gem "stimulus-rails"
# Build JSON APIs with ease [https://github.com/rails/jbuilder]
gem "jbuilder"
# Use Redis adapter to run Action Cable in production
# gem "redis", "~> 4.0"
platforms :ruby do # linux
gem 'unicorn'
end
# Use Kredis to get higher-level data types in Redis [https://github.com/rails/kredis]
# gem "kredis"
# Use Active Model has_secure_password [https://guides.rubyonrails.org/active_model_basics.html#securepassword]
# gem "bcrypt", "~> 3.1.7"
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem "tzinfo-data", platforms: %i[ mingw mswin x64_mingw jruby ]
# Reduces boot times through caching; required in config/boot.rb
gem "bootsnap", require: false
gem "rack-cors"
# Use Sass to process CSS
# gem "sassc-rails"
# Use Active Storage variants [https://guides.rubyonrails.org/active_storage_overview.html#transforming-images]
# gem "image_processing", "~> 1.2"
group :development, :test do
# See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem
gem "debug", platforms: %i[ mri mingw x64_mingw ]
end
group :development do
# Use console on exceptions pages [https://github.com/rails/web-console]
gem "web-console"
gem 'capistrano'
gem 'capistrano-bundler'
gem 'capistrano-rails', github: 'capistrano/rails'
gem 'capistrano-rails-console'
gem 'capistrano-sidekiq'
gem 'rvm1-capistrano3', require: false
gem 'airbrussh', require: false
gem 'net-ssh', '>= 6.0.2'
gem 'ed25519', '>= 1.2', '< 2.0'
gem 'bcrypt_pbkdf', '>= 1.0', '< 2.0'
# Add speed badges [https://github.com/MiniProfiler/rack-mini-profiler]
# gem "rack-mini-profiler"
# Speed up commands on slow machines / big apps [https://github.com/rails/spring]
# gem "spring"
end
group :test do
# Use system testing [https://guides.rubyonrails.org/testing.html#system-testing]
gem "capybara"
gem "selenium-webdriver"
gem "webdrivers"
end
The error that generates me in the deployment is this:
Execute deploy:restart
00:35 deploy:restart
01 sudo /etc/init.d/unicorn_pruebadespliegue_production restart
01 /etc/init.d/unicorn_pruebadespliegue_production: 26: kill:
01 No such process
01
01 Couldn't reload, starting 'cd /home/web/apps/pruebadespliegue/current; bundle exec unicorn -D -c /home/web/apps/pruebadespliegue/shared/config…
01 master failed to start, check stderr log for details
01
cap aborted!
SSHKit::Runner::ExecuteError: Exception while executing as web#ec2-44-210-112-191.compute-1.amazonaws.com: sudo exit status: 1
sudo stdout: /etc/init.d/unicorn_pruebadespliegue_production: 26: kill: No such process
Couldn't reload, starting 'cd /home/web/apps/pruebadespliegue/current; bundle exec unicorn -D -c /home/web/apps/pruebadespliegue/shared/config/unicorn.rb -E production' instead
master failed to start, check stderr log for details
sudo stderr: Nothing written
The truth is that I have tried to look for several solutions but I can't find the way to start the deployment and I am stuck in this error, in which I can't find any information.
the code for my deploy.rb is this:
# config valid only for current version of Capistrano
lock '3.17.1'
def deploysecret(key)
#deploy_secrets_yml ||= YAML.load_file('config/deploy-secrets.yml')[fetch(:stage).to_s]
#deploy_secrets_yml[key.to_s]
end
set :rails_env, fetch(:stage)
set :rvm_ruby_version, deploysecret(:rvm_ruby_version)
set :rvm_type, :user
set :application, deploysecret(:application)
set :full_app_name, "#{fetch(:application)}_#{fetch(:stage)}"
# If ssh access is restricted, probably you need to use https access
set :repo_url, deploysecret(:repo_url)
#set :scm, :git
#set :revision, `git rev-parse --short #{fetch(:branch)}`.strip
set :log_level, :info
set :pty, true
set :use_sudo, false
set :linked_files, %w{config/database.yml config/secrets.yml}
set :linked_dirs, %w{log tmp public/system public/assets}
set :keep_releases, 10
set :local_user, ENV['USER']
set :deploy_user, deploysecret(:user)
# Run test before deploy
set :tests, []
# Config files should be copied by deploy:setup_config
set(:config_files, %w(
log_rotation
database.yml
secrets.yml
unicorn.rb
sidekiq.yml
nginx.conf
unicorn_init.sh
))
set(:symlinks, [
{
source: 'nginx.conf',
link: "/etc/nginx/sites-enabled/#{fetch(:full_app_name)}"
},
{
source: 'unicorn_init.sh',
link: "/etc/init.d/unicorn_#{fetch(:full_app_name)}"
},
{
source: 'log_rotation',
link: "/etc/logrotate.d/#{fetch(:full_app_name)}"
}
])
namespace :deploy do
# deploy:setup_config
# remove the default nginx configuration as it will tend
# to conflict with our configs.
before 'deploy:setup_config', 'nginx:remove_default_vhost'
before 'deploy:setup_config', 'nginx:enable_virtual_host'
after 'deploy:setup_config', 'nginx:reload'
after 'deploy:setup_config', 'nginx:executable_init'
# Check right version of deploy branch
before :deploy, 'deploy:check_revision'
# Run test aund continue only if passed
before :deploy, 'deploy:run_tests'
# Compile assets locally and then rsync
# after 'deploy:symlink:shared', 'deploy:compile_assets_locally'
after :finishing, 'deploy:cleanup'
# Restart unicorn
after 'deploy:publishing', 'deploy:restart'
end

Related

e/azhar/.rvm/gems/ruby-3.0.0/gems/railties-6.0.4.4/lib/rails/application/configuration.rb:160:in `load_defaults': Unknown version "7.0" (RuntimeError)

I'm trying to create new app using ruby 3 and rails 7, getting this error while starting the server.
Here is my Gemfile:
source "https://rubygems.org"
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
ruby "3.0.0"
# Bundle edge Rails instead: gem "rails", github: "rails/rails", branch: "main"
gem "rails", "~> 6.0.4.4"
# Use sqlite3 as the database for Active Record
gem "pg"
# Use the Puma web server [https://github.com/puma/puma]
gem "puma", "~> 5.0"
# Build JSON APIs with ease [https://github.com/rails/jbuilder]
# gem "jbuilder"
# Use Redis adapter to run Action Cable in production
# gem "redis", "~> 4.0"
# Use Kredis to get higher-level data types in Redis [https://github.com/rails/kredis]
# gem "kredis"
# Use Active Model has_secure_password [https://guides.rubyonrails.org/active_model_basics.html#securepassword]
# gem "bcrypt", "~> 3.1.7"
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem "tzinfo-data", platforms: %i[ mingw mswin x64_mingw jruby ]
# Reduces boot times through caching; required in config/boot.rb
gem "bootsnap", require: false
# Use Active Storage variants [https://guides.rubyonrails.org/active_storage_overview.html#transforming-images]
# gem "image_processing", "~> 1.2"
# Use Rack CORS for handling Cross-Origin Resource Sharing (CORS), making cross-origin AJAX possible
# gem "rack-cors"
group :development, :test do
# See https://guides.rubyonrails.org/debugging_rails_applications.html#debugging-with-the-debug-gem
gem "debug", platforms: %i[ mri mingw x64_mingw ]
end
group :development do
# Speed up commands on slow machines / big apps [https://github.com/rails/spring]
# gem "spring"
end
gem 'devise'
gem 'cssbundling-rails'
gem 'mini_magick', '>= 4.9.5'
gem 'carrierwave', '~> 0.9'
gem 'bootstrap-sass','~> 2.3.2'
# gem 'activeadmin'
gem 'sass-rails'
I am trying to copy my old project which is built on ruby 2 and rails 7.
Maybe if you change your gemfile version by deleting your gemfile.lock and then uninstalling this version 6.0.4.4 as the error path shows us running trying to use this old version
gem 'rails', '~> 7.0', '>= 7.0.2.3' # Gemfile
gem uninstall rails
gem uninstall railties
# for example
gem install rails -v 7.0.2.3
# or only
bundle
and make sure...
config.load_defaults 7.0 # application.rb
class ... < ActiveRecord::Migration[7.0] # db/migrate/*.rb
Please make sure the correct rails version is mentioned in file \config\application.rb
config.load_defaults rails_version you want to use

capistrano deployment with rbenv: /home/deploy/.rbenv/bin/rbenv: No such file or directory

I recently switched from rvm to rbenv on my ubuntu server. I have followed all instructions in the official documentation https://github.com/rbenv/rbenv. When I try to deploy with capistrano, I run into an error message saying it can't find the /bin/rbenv directory.
00:24 bundler:config
01 RBENV_ROOT=$HOME/.rbenv RBENV_VERSION=2.5.8 $HOME/.rbenv/bin/rbenv exec bundle config --local deployment true
01 bash: /home/deploy/.rbenv/bin/rbenv: No such file or directory
#<Thread:0x00007ff4640d25e0#/Users/kevinsun/.rvm/gems/ruby-2.5.8#wager/gems/sshkit-1.21.2/lib/sshkit/runners/parallel.rb:10 run> terminated with exception (report_on_exception is true):
Traceback (most recent call last):
1: from /Users/kevinsun/.rvm/gems/ruby-2.5.8#wager/gems/sshkit-1.21.2/lib/sshkit/runners/parallel.rb:11:in `block (2 levels) in execute'
/Users/kevinsun/.rvm/gems/ruby-2.5.8#wager/gems/sshkit-1.21.2/lib/sshkit/runners/parallel.rb:15:in `rescue in block (2 levels) in execute': Exception while executing as deploy#3.133.135.38: bundle exit status: 127 (SSHKit::Runner::ExecuteError)
bundle stdout: Nothing written
bundle stderr: bash: /home/deploy/.rbenv/bin/rbenv: No such file or directory
(Backtrace restricted to imported tasks)
cap aborted!
SSHKit::Runner::ExecuteError: Exception while executing as deploy#3.133.135.38: bundle exit status: 127
bundle stdout: Nothing written
bundle stderr: bash: /home/deploy/.rbenv/bin/rbenv: No such file or directory
Caused by:
SSHKit::Command::Failed: bundle exit status: 127
bundle stdout: Nothing written
bundle stderr: bash: /home/deploy/.rbenv/bin/rbenv: No such file or directory
Tasks: TOP => deploy:updated => bundler:install => bundler:config
(See full trace by running task with --trace)
The deploy has failed with an error: Exception while executing as deploy#3.133.135.38: bundle exit status: 127
bundle stdout: Nothing written
bundle stderr: bash: /home/deploy/.rbenv/bin/rbenv: No such file or directory
On my instance, drilling down into that directory I can confirm there is no /bin/rbenv under /home/deploy/.rbenv/
ubuntu#ip-10-0-0-163:/home/deploy/.rbenv$ ls -a
. .. plugins shims versions
I have tried fiddling around with my deploy file according to this answer but to no avail.
Here is my Gemfile, Capfile, and deploy.rb:
Gemfile
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
ruby '2.5.8'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.2.3'
# Use postgresql as the database for Active Record
gem 'pg', '>= 0.18', '< 2.0'
# Use Puma as the app server
gem 'puma', '~> 3.11'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
gem "font-awesome-rails"
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# See https://github.com/rails/execjs#readme for more supported runtimes
gem 'mini_racer', platforms: :ruby
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.2'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.5'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 4.0'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'
# Use ActiveStorage variant
# gem 'mini_magick', '~> 4.8'
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.1.0', require: false
gem 'momentjs-rails'
gem 'bootstrap-toggle-rails'
#Admin user
gem 'activeadmin'
gem 'activeadmin_settings_cached'
gem 'formadmin'
gem 'active_admin_role'
gem "kaminari"
# charts
gem "chartkick"
gem 'groupdate'
#background
gem "redis"
gem 'sidekiq', '5.2.7'
gem 'sidekiq-status'
# copy clipboard
gem 'clipboard-rails'
# ransack
gem 'ransack'
# geocoder
gem 'geocoder'
# Plus integrations with:
gem 'devise'
gem 'cancancan'
gem 'draper'
gem 'pundit'
#design
gem "bootstrap", '~> 4.3.1'
gem 'ckeditor', github: 'galetahub/ckeditor'
gem 'paperclip'
gem 'aws-sdk', '~> 3.0', '>= 3.0.1'
gem 'aws-sdk-s3', '~> 1.83', '>= 1.83.1'
gem 'color'
#payments
gem 'money-rails'
gem 'stripe'
gem 'paypal-sdk-rest'
#nested form
gem 'nested_form_fields'
# slick
gem "jquery-slick-rails"
gem 'jquery-rails'
gem 'jquery-ui-rails'
#gem 'font-awesome-sass'
gem 'remotipart'
# paginate
gem 'will_paginate'
gem 'will_paginate_infinite'
gem 'validates_email_format_of'
# simple form and validations
gem 'simple_form'
gem 'client_side_validations'
gem 'client_side_validations-simple_form'
gem 'jquery-validation-rails'
gem 'alertifyjs-rails'
gem 'bootstrap3-datetimepicker-rails', '~> 4.17.47'
#social login
gem 'omniauth'
gem 'omniauth-facebook'
gem 'omniauth-google-oauth2'
#API
gem 'rack-cors', '0.4.0'
gem 'rack-attack', '5.0.1'
gem 'active_model_serializers'
gem 'jwt'
gem 'simple_command'
gem 'bcrypt'
# sitemap
gem 'sitemap_generator'
# notifications
gem 'notifications', '~> 0.6.0'
# enum
gem 'simple_enum'
# recaptcha
gem "recaptcha", :require => "recaptcha/rails"
# cron
gem 'whenever', require: false
# autolink
gem "rails_autolink"
#for read unread
gem 'unread'
#list gem
gem 'acts_as_list'
#cropper and dropzone
gem 'cropper_rails'
gem 'dropzonejs-rails'
group :development do
# Access an interactive console on exception pages or by calling 'console' anywhere in the code.
gem 'byebug'
gem 'web-console', '>= 3.3.0'
gem 'listen', '>= 3.0.5', '< 3.2'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
gem 'spring-watcher-listen', '~> 2.0.0'
gem 'annotate'
gem 'bullet'
gem "brakeman"
gem "rb-readline"
#deploy
gem 'capistrano', require: false
# gem 'capistrano-rvm', require: false
gem 'capistrano-rbenv', '~> 2.2'
gem 'capistrano-rails', require: false
gem 'capistrano-bundler', require: false
gem 'capistrano-sidekiq', require: false
gem 'capistrano3-puma', require: false
gem 'sshkit-sudo', require: false
gem 'capistrano-nvm', require: false
gem 'capistrano3-monit', github: 'naps62/capistrano3-monit'
end
group :test do
# Adds support for Capybara system testing and selenium driver
gem 'capybara', '>= 2.15'
gem 'selenium-webdriver'
# Easy installation and use of chromedriver to run system tests with Chrome
gem 'chromedriver-helper'
end
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
gem 'language_filter'
gem 'nokogiri', '~> 1.10', '>= 1.10.9'
gem 'figaro'
#gem 'rvm-capistrano'
Capfile
# Load DSL and set up stages
require "capistrano/setup"
# Include default deployment tasks
require "capistrano/deploy"
# Load the SCM plugin appropriate to your project:
#
# require "capistrano/scm/hg"
# install_plugin Capistrano::SCM::Hg
# or
# require "capistrano/scm/svn"
# install_plugin Capistrano::SCM::Svn
# or
require "capistrano/scm/git"
install_plugin Capistrano::SCM::Git
# Include tasks from other gems included in your Gemfile
require 'capistrano/rails'
require 'capistrano/bundler'
# require 'capistrano/rvm'
require 'capistrano/rbenv'
require 'capistrano/puma'
require 'capistrano/sidekiq'
require "whenever/capistrano"
require 'capistrano/sidekiq/monit'
install_plugin Capistrano::Puma # Default puma tasks
install_plugin Capistrano::Puma::Workers # if you want to control the workers (in cluster mode)
install_plugin Capistrano::Puma::Monit
# 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.8.1'
#require "rvm/capistrano"
set :application, 'ewagers-noah-rails'
set :repo_url, 'git#gitlab.com:ankush_verma/ewagers.git'
set :branch, 'autoscale-test'
set :user, 'deploy'
set :puma_threads, [4, 16]
set :puma_workers, 4
# Don't change these unless you know what you're doing
set :pty, false
set :use_sudo, false
set :sidekiq_user, fetch(:user)
set :stage, :production
set :deploy_via, :remote_cache
set :deploy_to, "/home/#{fetch(:user)}/apps/#{fetch(:application)}"
set :puma_bind, "unix://#{shared_path}/tmp/sockets/#{fetch(:application)}-puma.sock"
set :puma_state, "#{shared_path}/tmp/pids/puma.state"
set :puma_pid, "#{shared_path}/tmp/pids/puma.pid"
set :puma_access_log, "#{release_path}/log/puma.error.log"
set :puma_error_log, "#{release_path}/log/puma.access.log"
set :ssh_options, { forward_agent: true, user: fetch(:user), keys: %w(~/.ssh/id_rsa.pub) }
set :puma_preload_app, true
set :puma_worker_timeout, nil
set :puma_init_active_record, true # Change to false when not using ActiveRecord
set :rvm_ruby_version, 'ruby-2.5.8#wager'
set :linked_files, fetch(:linked_files, []).push('config/database.yml', 'config/secrets.yml', 'puma.rb', 'config/app.yml', "config/sidekiq.yml", "config/env.yml")
set :sidekiq_config, 'config/sidekiq.yml'
# Default value for linked_dirs is []
# append :linked_dirs, 'log', 'tmp/pids', 'tmp/cache', 'tmp/sockets', 'public/system'
set :linked_dirs, fetch(:linked_dirs, []).push('log', 'tmp/pids', 'tmp/cache', 'tmp/sockets', 'vendor/bundle', 'public/system', 'public/uploads', 'public/watir')
## Defaults:
# set :scm, :git
# set :branch, :master
# set :format, :pretty
# set :log_level, :debug
set :keep_releases, 2
set :execute_monit_without_sudo, false
## Linked Files & Directories (Default None):
# set :linked_files, %w{config/database.yml}
# set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system}
namespace :puma do
desc 'Create Directories for Puma Pids and Socket'
task :make_dirs do
on roles(:app) do
execute "mkdir #{shared_path}/tmp/sockets -p"
execute "mkdir #{shared_path}/tmp/pids -p"
end
end
before :start, :make_dirs
end
namespace :deploy do
desc "Make sure local git is in sync with remote."
task :check_revision do
on roles(:app) do
unless `git rev-parse HEAD` == `git rev-parse origin/#{fetch(:branch)}`
puts "WARNING: HEAD is not the same as origin/#{fetch(:branch)}"
puts "Run `git push` to sync changes."
exit
end
end
end
desc 'Initial Deploy'
task :initial do
on roles(:app) do
before 'deploy:restart', 'puma:start'
invoke 'deploy'
end
end
desc 'Restart application'
task :restart do
on roles(:app), in: :sequence, wait: 5 do
invoke 'puma:restart'
end
end
before :starting, :check_revision
after :finishing, :compile_assets
after :finishing, :cleanup
after :finishing, :restart
end
namespace :deploy do
desc "Update crontab with whenever"
task :update_cron do
on roles(:app) do
within current_path do
execute :bundle, :exec, "whenever --update-crontab #{fetch(:application)}"
end
end
end
after :finishing, 'deploy:update_cron'
end
namespace :deploy do
desc 'run "CREATE EXTENSION IF NOT EXISTS "pgcrypto"" in sudo mode'
task :sudo_pgcrypto do
on roles(:all) do |host|
execute :sudo, "CREATE EXTENSION IF NOT EXISTS", "pgcrypto"
end
end
end
namespace :logs do
desc "tail rails logs"
task :puma do
on roles(:app) do
execute "tail -f -n 500 #{shared_path}/log/#{fetch(:rails_env)}.log "
end
end
end
# ps aux | grep puma # Get puma pid
# kill -s SIGUSR2 pid # Restart puma
# kill -s SIGTERM pid # Stop puma
set :rbenv_type, :user # or :system, or :fullstaq (for Fullstaq Ruby), depends on your rbenv setup
set :rbenv_ruby, '2.5.8'
set :rbenv_ruby_dir, '/home/deploy/.rbenv/versions/2.5.8'
# in case you want to set ruby version from the file:
# set :rbenv_ruby, File.read('.ruby-version').strip
set :default_env, {
path: '/home/deploy/.rbenv/plugins/ruby-build/bin:/home/deploy/.rbenv/shims:/home/deploy/.rbenv/bin:$PATH',
rbenv_root: '/usr/bin/rbenv' # also tried '/home/deploy/.rbenv' here
}
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
set :rbenv_custom_path, '/usr/bin/rbenv' # also tried '/home/deploy/.rbenv' here
This helps in my case, I had same problem, capistrano was not able to find rbenv binary
set :rbenv_prefix, '/usr/bin/rbenv exec'
thanks to this issue on github, where it is noted
github

Capistrano Wont Deploy Rails App With Bundler

I'm trying to set up my rails app onto a DigitalOcean Server with postgres, following This Tutorial. I am at the point where I am trying to deploy my app with capistrano, but when I run $cap production deploy, it always fails with this error message:
** DEPLOY FAILED
** Refer to log/capistrano.log for details. Here are the last 20 lines:
DEBUG [1c7e325c] )
DEBUG [1c7e325c]
DEBUG [1c7e325c] To update to the latest version installed on your system, run `bundle update --bundler`.
To install the missing version, run `gem install bundler:2.0.1`
DEBUG [1c7e325c] from /home/deploy/.rbenv/versions/2.6.1/lib/ruby/2.6.0/rubygems.rb:302:in `activate_bin_path'
DEBUG [1c7e325c] from /home/deploy/.rbenv/versions/2.6.1/bin/bundle:23:in `<main>'
DEBUG [1c7e325c] Finished in 0.408 seconds with exit status 1 (failed).
INFO [5d18f10c] Running $HOME/.rbenv/bin/rbenv exec bundle install --path /home/deploy/myapp/shared/bundle --jobs 4 --without development test --deployment --quiet as deploy#1.2.3.4
DEBUG [5d18f10c] Command: cd /home/deploy/myapp/releases/20190408225913 && ( export RBENV_ROOT="$HOME/.rbenv" RBENV_VERSION="2.6.1" ; $HOME/.rbenv/bin/rbenv exec bundle install --path /home/deploy/myapp/shared/bundle --jobs 4 --without development test --deployment --quiet )
DEBUG [969718b0] /home/deploy/.rbenv/versions/2.6.1/lib/ruby/2.6.0/rubygems.rb:283:in `find_spec_for_exe'
DEBUG [969718b0] :
DEBUG [969718b0] Could not find 'bundler' (2.0.1) required by your /home/deploy/myapp/releases/20190408225913/Gemfile.lock.
DEBUG [969718b0] (
DEBUG [969718b0] Gem::GemNotFoundException
DEBUG [969718b0] )
DEBUG [969718b0]
DEBUG [969718b0] To update to the latest version installed on your system, run `bundle update --bundler`.
To install the missing version, run `gem install bundler:2.0.1`
DEBUG [969718b0] from /home/deploy/.rbenv/versions/2.6.1/lib/ruby/2.6.0/rubygems.rb:302:in `activate_bin_path'
DEBUG [969718b0] from /home/deploy/.rbenv/versions/2.6.1/bin/bundle:23:in `<main>'
This is my deploy file:
set :application, "myapp"
set :repo_url, "git#github.com:user/myapp.git"
append :rbenv_map_bins, 'puma', 'pumactl'
# Deploy to the user's home directory
set :deploy_to, "/home/deploy/#{fetch :application}"
set :default_environment, {
'PATH' => "$HOME/.rbenv/shims:$HOME/.rbenv/bin:$PATH"
}
append :linked_dirs, 'log', 'tmp/pids', 'tmp/cache', 'tmp/sockets', 'vendor/bundle', '.bundle', 'public/system', 'public/uploads'
# Only keep the last 5 releases to save disk space
set :keep_releases, 5
# Optionally, you can symlink your database.yml and/or secrets.yml file from the shared directory during deploy
# This is useful if you don't want to use ENV variables
# append :linked_files, 'config/database.yml', 'config/secrets.yml'
gemfile:
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
ruby '2.4.1'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.2.3'
# Use postgresql as the database for Active Record
gem 'pg', '>= 0.18', '< 2.0'
# Use Puma as the app server
gem 'puma', '~> 3.11'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'mini_racer', platforms: :ruby
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.2'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.5'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 4.0'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'
# Use ActiveStorage variant
# gem 'mini_magick', '~> 4.8'
# Use Capistrano for deployment
# gem 'capistrano-rails', group: :development
# Reduces boot times through caching; required in config/boot.rb
gem 'bootsnap', '>= 1.1.0', require: false
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platforms: [:mri, :mingw, :x64_mingw]
end
group :development do
# Access an interactive console on exception pages or by calling 'console' anywhere in the code.
gem 'web-console', '>= 3.3.0'
gem 'listen', '>= 3.0.5', '< 3.2'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
gem 'spring-watcher-listen', '~> 2.0.0'
end
group :test do
# Adds support for Capybara system testing and selenium driver
gem 'capybara', '>= 2.15'
gem 'selenium-webdriver'
# Easy installation and use of chromedriver to run system tests with Chrome
gem 'chromedriver-helper'
end
# Windows does not include zoneinfo files, so bundle the tzinfo-data gem
gem 'tzinfo-data', platforms: [:mingw, :mswin, :x64_mingw, :jruby]
gem 'capistrano', '~> 3.11'
gem 'capistrano-passenger', '~> 0.2.0'
gem 'capistrano-rails', '~> 1.4'
gem 'capistrano-rbenv', '~> 2.1', '>= 2.1.4'
I've already tried running gem install bundler:2.0.1 and gem install bundler on both the deploy#myapp and my local computer. Any suggestions on how I can fix this problem, and fix the error that occurs when I try to deploy my app to production with capistrano rbenv?
EDIT
Capfile:
# Load DSL and set up stages
require "capistrano/setup"
# Include default deployment tasks
require "capistrano/deploy"
# Load the SCM plugin appropriate to your project:
#
# require "capistrano/scm/hg"
# install_plugin Capistrano::SCM::Hg
# or
# require "capistrano/scm/svn"
# install_plugin Capistrano::SCM::Svn
# or
require "capistrano/scm/git"
install_plugin Capistrano::SCM::Git
# Include tasks from other gems included in your Gemfile
#
# For documentation on these, see for example:
#
# https://github.com/capistrano/rvm
# https://github.com/capistrano/rbenv
# https://github.com/capistrano/chruby
# https://github.com/capistrano/bundler
# https://github.com/capistrano/rails
# https://github.com/capistrano/passenger
#
# require "capistrano/rvm"
# require "capistrano/rbenv"
# require "capistrano/chruby"
# require "capistrano/bundler"
# require "capistrano/rails/assets"
# require "capistrano/rails/migrations"
# require "capistrano/passenger"
# Load custom tasks from `lib/capistrano/tasks` if you have any defined
Dir.glob("lib/capistrano/tasks/*.rake").each { |r| import r }
require 'capistrano/rails'
require 'capistrano/passenger'
require 'capistrano/rbenv'
set :rbenv_type, :user
set :rbenv_ruby, '2.6.1'
First check if you have bundler installed as the deploy user after switching into the proper rbenv:
rbenv global 2.6.1
bundler --version
If not install it: gem install bundler:2.0.1.
Otherwise make sure your Capfile has the rbenv label mentioned (see tutorial).
set :rbenv_type, :user
set :rbenv_ruby, '2.6.1'

Error when trying to deploy a Ruby on Rails server with Capistrano

I've been using this tutorial to deploy a Ruby on rails application via digitalocean. I'm using a 512 MB Memory with 20 GB space on Ubuntu 16.04.1 x64.
However, at the end of the tutorial when I try to run cap production deploy I get this:
[23e7b90d] Running [ -d $HOME/.rbenv/versions/2.3.1-p112 ] as deploy#example.com
[23e7b90d] Command: [ -d $HOME/.rbenv/versions/2.3.1-p112 ]
[a916b547] Running [ -d $HOME/.rbenv/versions/2.3.1-p112 ] as deploy#188.166.32.210
[a916b547] Command: [ -d $HOME/.rbenv/versions/2.3.1-p112 ]
DEBUG [a916b547] Finished in 0.418 seconds with exit status 1 (failed).
WARN rbenv: 2.3.1-p112 is not installed or not found in $HOME/.rbenv/versions/2.3.1-p112
EDIT 1:
I have installed ruby 2.3.1 on the server. When I ssh into the server and type ruby -v it gives me ruby 2.3.1p112 (2016-04-26 revision 54768) [x86_64-linux]
Here is my Capfile
# Load DSL and Setup Up Stages
require 'capistrano/setup'
# Includes default deployment tasks
require 'capistrano/deploy'
# Includes tasks from other gems included in your Gemfile
#
# For documentation on these, see for example:
#
# https://github.com/capistrano/rvm
# https://github.com/capistrano/rbenv
# https://github.com/capistrano/chruby
# https://github.com/capistrano/bundler
# https://github.com/capistrano/rails
#
# require 'capistrano/rvm'
# require 'capistrano/chruby'
require 'capistrano/bundler'
require 'capistrano/rails'
require 'capistrano/rbenv'
require 'capistrano/passenger'
# Loads custom tasks from `lib/capistrano/tasks' if you have any defined.
Dir.glob('lib/capistrano/tasks/*.cap').each { |r| import r }
production.rb
# Simple Role Syntax
# ==================
# Supports bulk-adding hosts to roles, the primary
# server in each group is considered to be the first
# unless any hosts have the primary property set.
# Don't declare `role :all`, it's a meta role
role :app, %w{deploy#example.com}
role :web, %w{deploy#example.com}
role :db, %w{deploy#example.com}
# Extended Server Syntax
# ======================
# This can be used to drop a more detailed server
# definition into the server list. The second argument
# something that quacks like a hash can be used to set
# extended properties on the server.
# Replace 127.0.0.1 with your server's IP address!
server '188.166.32.210', user: 'deploy', roles: %w{web app}
deploy.rb
# config valid only for Capistrano 3.1
lock '3.4.1'
set :application, 'myapp'
set :repo_url, 'git#github.com:royketelaar/myapp.git'
# Default branch is :master
# ask :branch, proc { `git rev-parse --abbrev-ref HEAD`.chomp }
# Default deploy_to directory is /var/www/my_app
set :deploy_to, '/home/deploy/myapp'
set :rbenv_type, :user # or :system, depends on your rbenv setup
set :rbenv_ruby, '2.3.1-p112'
# Default value for :scm is :git
# set :scm, :git
# Default value for :format is :pretty
# set :format, :pretty
# Default value for :log_level is :debug
# set :log_level, :debug
# Default value for :pty is false
# set :pty, true
# Default value for :linked_files is []
set :linked_files, %w{config/database.yml}
# Default value for linked_dirs is []
# set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system}
set :linked_dirs, %w{log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system}
# Default value for default_env is {}
# set :default_env, { path: "/opt/ruby/bin:$PATH" }
set :bundle_env_variables, { 'NOKOGIRI_USE_SYSTEM_LIBRARIES' => 1 }
# Default value for keep_releases is 5
# set :keep_releases, 5
namespace :deploy do
# desc 'Restart application'
# task :restart do
# on roles(:app), in: :sequence, wait: 5 do
# execute :touch, release_path.join('tmp/restart.txt')
# end
# end
# after :restart, :clear_cache do
# on roles(:web), in: :groups, limit: 3, wait: 10 do
# # Here we can do anything such as:
# # within release_path do
# # execute :rake, 'cache:clear'
# # end
# end
# end
end
gemfile (don't know if that's interesting)
source 'https://rubygems.org'
# Bundle edge Rails instead: gem 'rails', github: 'rails/rails'
gem 'rails', '~> 5.0.0', '>= 5.0.0.1'
# Use sqlite3 as the database for Active Record
gem 'sqlite3'
# Use Puma as the app server
gem 'puma', '~> 3.0'
# Use SCSS for stylesheets
gem 'sass-rails', '~> 5.0'
# Use Uglifier as compressor for JavaScript assets
gem 'uglifier', '>= 1.3.0'
# Use CoffeeScript for .coffee assets and views
gem 'coffee-rails', '~> 4.2'
# See https://github.com/rails/execjs#readme for more supported runtimes
# gem 'therubyracer', platforms: :ruby
gem 'normalize-rails'
gem "paperclip", "~> 5.0.0"
gem 'friendly_id', '~> 5.1.0' # Note: You MUST use 5.0.0 or greater for Rails 4.0+
gem 'devise'
gem 'cocoon'
gem 'pg'
# Use jquery as the JavaScript library
gem 'jquery-rails'
# Turbolinks makes navigating your web application faster. Read more: https://github.com/turbolinks/turbolinks
gem 'turbolinks', '~> 5'
# Build JSON APIs with ease. Read more: https://github.com/rails/jbuilder
gem 'jbuilder', '~> 2.5'
# Use Redis adapter to run Action Cable in production
# gem 'redis', '~> 3.0'
# Use ActiveModel has_secure_password
# gem 'bcrypt', '~> 3.1.7'
# Use Capistrano for deployment
gem 'capistrano', '~> 3.4.0'
gem 'capistrano-bundler', '~> 1.1.2'
gem 'capistrano-rails', '~> 1.1.1'
gem 'capistrano-rbenv'
gem 'capistrano-passenger'
group :development, :test do
# Call 'byebug' anywhere in the code to stop execution and get a debugger console
gem 'byebug', platform: :mri
end
group :development do
# Access an IRB console on exception pages or by using <%= console %> anywhere in the code.
gem 'web-console'
gem 'listen', '~> 3.0.5'
# Spring speeds up development by keeping your application running in the background. Read more: https://github.com/rails/spring
gem 'spring'
gem 'spring-watcher-listen', '~> 2.0.0'
end
Ok, for some reason I've fixed my error by changing a line in config/deploy.rb
from set :rbenv_ruby, '2.3.1-p112'
to
set :rbenv_ruby, '2.3.1'
I still don't know why though

Don't have 'fog' gem installed in production using carrierwave and unicorn

I am trying to use Fog with Carrierwave and am getting this error in the Unicorn log. I have fog in my gemfile and my NodeImageUploader calls
storage :fog
Gemfile
source 'http://rubygems.org'
gem 'rails', '3.2.8'
gem 'bcrypt-ruby', '3.0.1'
gem 'jquery-rails'
gem 'fog', '1.8.0'
gem 'bootstrap-sass', '~> 2.2.1.1'
gem 'devise'
gem 'omniauth-facebook'
gem 'rails-backbone'
gem 'simple_form'
# gem 'newrelic_rpm'
# gem 'client_side_validations'
gem 'inherited_resources'
gem 'friendly_id', '~> 4.0.1'
gem 'carrierwave'
gem 'activeadmin'
gem 'meta_search', '>= 1.1.0.pre'
gem 'will_paginate'
gem 'bootstrap-will_paginate'
gem 'faker', '1.0.1'
gem 'rack-contrib'
gem 'soulmate', :git => "git://github.com/allotrop/soulmate.git", :require => 'soulmate/server'
gem 'httparty'
gem 'mini_magick'
gem 'mime-types'
gem 'redis'
gem 'pg', '0.12.2'
gem 'nokogiri', '1.5.5'
# gem "rmagick"
group :development, :test do
gem 'rspec-rails', '2.11.0'
gem 'guard-rspec', '0.5.5'
end
group :development do
gem 'annotate', ">=2.5.0"
#gem 'open-uri'
end
# Gems used only for assets and not required
# in production environments by default.
group :assets do
gem 'sass-rails', '~> 3.2.3'
gem 'coffee-rails', '~> 3.2.1'
# gem 'therubyracer', :platform => :ruby
gem 'uglifier', '>= 1.0.3'
end
group :test do
gem 'capybara', '1.1.2'
gem 'guard-spork', '0.3.2'
gem 'spork', '0.9.0'
gem 'factory_girl_rails', '1.4.0'
gem 'rb-inotify', '0.8.8'
gem 'libnotify', '0.5.9'
gem 'database_cleaner', '0.7.0'
gem 'email_spec'
end
# To use ActiveModel has_secure_password
# gem 'bcrypt-ruby', '~> 3.0.0'
# To use Jbuilder templates for JSON
# gem 'jbuilder'
# Use unicorn as the app server
gem 'unicorn'
# Deploy with Capistrano
gem 'capistrano'
# To use debugger
gem 'debugger', group: [:development, :test]
# gem 'rubber'
# gem 'open4'
config/carrierwave.rb
CarrierWave.configure do |config|
config.fog_credentials = {
:aws_access_key_id => 'xxxx'
:aws_secret_access_key => 'yyyy'
:provider => 'AWS'
}
config.fog_directory = 'example-uploads'
config.fog_public = true
end
Doing bundle show fog gives me
/home/deployer/apps/allotrop/shared/bundle/ruby/1.9.1/gems/fog-1.3.1
Also, I am able to use Carrierwave's remote_image_url method in a rake task on production to successfully save images to S3. But, when I try to get any page of my app, I get this error.
unicorn.log
E, [2012-12-11T20:49:28.872957 #23518] ERROR -- : You don't have the 'fog' gem installed (RuntimeError)
/home/deployer/apps/allotrop/shared/bundle/ruby/1.9.1/gems/carrierwave-0.7.1/lib/carrierwave/uploader/configuration.rb:73:in `eval'
/home/deployer/apps/allotrop/shared/bundle/ruby/1.9.1/gems/carrierwave-0.7.1/lib/carrierwave/storage/fog.rb:3:in `<top (required)>'
(eval):1:in `storage'
/home/deployer/apps/allotrop/shared/bundle/ruby/1.9.1/gems/carrierwave-0.7.1/lib/carrierwave/uploader/configuration.rb:73:in `eval'
/home/deployer/apps/allotrop/shared/bundle/ruby/1.9.1/gems/carrierwave-0.7.1/lib/carrierwave/uploader/configuration.rb:73:in `storage'
/home/deployer/apps/allotrop/releases/20121211204621/app/uploaders/node_image_uploader.rb:24:in `<class:NodeImageUploader>'
/home/deployer/apps/allotrop/releases/20121211204621/app/uploaders/node_image_uploader.rb:5:in `<top (required)>'
/home/deployer/apps/allotrop/shared/bundle/ruby/1.9.1/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:251:in `require'
/home/deployer/apps/allotrop/shared/bundle/ruby/1.9.1/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:251:in `block in requi>
/home/deployer/apps/allotrop/shared/bundle/ruby/1.9.1/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:236:in `load_dependenc>
/home/deployer/apps/allotrop/shared/bundle/ruby/1.9.1/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:251:in `require'
/home/deployer/apps/allotrop/shared/bundle/ruby/1.9.1/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:359:in `require_or_loa>
/home/deployer/apps/allotrop/shared/bundle/ruby/1.9.1/gems/activesupport-3.2.8/lib/active_support/dependencies.rb:502:in `load_mis
The application works fine if I switch the uploader to storage :file and even when I use storage :fog in development. Where am I going wrong?
After trying to fix this problem for the better part of the day, I fixed it by following the steps from Ryan Bates' Railscasts episode on zero downtime deployment. The changes mentioned by him are to add preload_app in your unicorn template and use a USR2 signal for unicorn restart.
unicorn.rb.erb
# Load rails+github.git into the master before forking workers
# for super-fast worker spawn times
preload_app true
before_fork do |server, worker|
# Disconnect since the database connection will not carry over
if defined? ActiveRecord::Base
ActiveRecord::Base.connection.disconnect!
end
##
# When sent a USR2, Unicorn will suffix its pidfile with .oldbin and
# immediately start loading up a new version of itself (loaded with a new
# version of our app). When this new Unicorn is completely loaded
# it will begin spawning workers. The first worker spawned will check to
# see if an .oldbin pidfile exists. If so, this means we've just booted up
# a new Unicorn and need to tell the old one that it can now die. To do so
# we send it a QUIT.
#
# Using this method we get 0 downtime deploys.
old_pid = "#{server.config[:pid]}.oldbin"
if File.exists?(old_pid) && server.pid != old_pid
begin
Process.kill("QUIT", File.read(old_pid).to_i)
rescue Errno::ENOENT, Errno::ESRCH
# someone else did our job for us
end
end
end
after_fork do |server, worker|
##
# Unicorn master loads the app then forks off workers - because of the way
# Unix forking works, we need to make sure we aren't using any of the parent's
# sockets, e.g. db connection
# Start up the database connection again in the worker
if defined?(ActiveRecord::Base)
ActiveRecord::Base.establish_connection
end
# Redis and Memcached would go here but their connections are established
# on demand, so the master never opens a socket
end
unicorn_init.erb
restart|reload)
sig USR2 && echo reloaded OK && exit 0
echo >&2 "Couldn't reload, starting '$CMD' instead"
run "$CMD"
;;
Then you need to run cap:unicorn:setup unicorn:stop unicorn:start. If anyone is facing this issue, I highly recommend seeing the Railscasts on Capistrano recipes and zero downtime deployment.

Resources