Capistrano: Gem bundler is not installed - ruby-on-rails

Capistrano raises the error "ERROR: Gem bundler is not installed, run gem install bundler first."
But when I clone the project to /tmp/project of the targe server bundle runs (as root) smoothly.
The server is a Ubuntu 12.04.2 LTS, ruby was installed via rvm.
I added "source /usr/local/rvm/scripts/rvm" to the /root/.bashrc but it did not solve the problem.
require "bundler/capistrano"
set :application, 'myproject'
set :repository, "git#myproject.git"
set :branch, 'staging'
set :user, 'root'
set :domain, "mydomain"
set :deploy_to, "/var/webapps/#{application}"
role :web, domain # Your HTTP server, Apache/etc
role :app, domain # This may be the same as your `Web` server
role :db, domain, :primary => true # This is where Rails migrations will run
role :db, domain
default_run_options[:pty] = true
# [and then the tasks]
TIA.
EDIT
This very same deploy script worked in another server, with ruby installed in the same way. The only difference I remember is that that server was a CentOS.
Also tried rvm wrapper rvm current bundle bundle as suggested here
Edit 2
I compared the env command via ssh and via capistrano. The following env variables were missing when env was issued by capistrano:
__array_start=0
_first=0
_second=1
escape_flag=1
GEM_HOME=/usr/local/rvm/gems/ruby-1.9.3-p392
GEM_PATH=/usr/local/rvm/gems/ruby-1.9.3-p392:/usr/local/rvm/gems/ruby-1.9.3-p392#global
IRBRC=/usr/local/rvm/rubies/ruby-1.9.3-p392/.irbrc
LC_ADDRESS=pt_BR.UTF-8
LC_IDENTIFICATION=pt_BR.UTF-8
LC_MEASUREMENT=pt_BR.UTF-8
LC_MONETARY=pt_BR.UTF-8
LC_NAME=pt_BR.UTF-8
LC_NUMERIC=pt_BR.UTF-8
LC_PAPER=pt_BR.UTF-8
LC_TELEPHONE=pt_BR.UTF-8
LC_TIME=pt_BR.UTF-8
LESSCLOSE=/usr/bin/lesspipe %s %s
LESSOPEN=| /usr/bin/lesspipe %s
LS_COLORS=rs=0:di=01;34:ln=01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:su=37;41:sg=30;43:ca=30;41:tw=30;42:ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01;31:*.txz=01;31:*.zip=01;31:*.z=01;31:*.Z=01;31:*.dz=01;31:*.gz=01;31:*.lz=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*.rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*.7z=01;31:*.rz=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga=01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx=01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v=01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc=01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01;35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.mid=00;36:*.midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.axa=00;36:*.oga=00;36:*.spx=00;36:*.xspf=00;36:
MY_RUBY_HOME=/usr/local/rvm/rubies/ruby-1.9.3-p392
RUBY_VERSION=ruby-1.9.3-p392
rvm_bin_path=/usr/local/rvm/bin
rvm_debug_clr=
rvm_error_clr=
rvm_notify_clr=
rvm_path=/usr/local/rvm
rvm_prefix=/usr/local
rvm_reset_clr=
rvm_version=1.19.5 (stable)
rvm_warn_clr=
The following env vars were different:
_=/usr/bin/env # ssh
_=/bin/sh # capistrano
LANG=en_US.UTF-8 # ssh
LANG=C # capistrano
LANGUAGE=en # ssh
LANGUAGE=C # capistrano
# ssh
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/rvm/bin
# capistrano
PATH=/usr/local/rvm/gems/ruby-1.9.3-p392/bin:/usr/local/rvm/gems/ruby-1.9.3-p392#global/bin:/usr/local/rvm/rubies/ruby-1.9.3-p392/bin:/usr/local/rvm/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

I had a similar problem with one of my setups, though I use rbenv to manage ruby versions. The issue was that bundler was not installed on the Ruby version my project was pointing to.
I did a quick check online and you have a few options:
run rvm gemset use global && gem install bundler (installs to
global version)
edit your /.bashrc ... something along those lines
([[ -s "$HOME/.rvm/scripts/rvm" ]] && . "$HOME/.rvm/scripts/rvm")
Running gem env may show you that you are forcing a path, and you would then need to remove the entry from ~/.gemrc or /etc/gemrc
My 2-cents

Related

Capistrano Production Deploy -> rbenv

I think I have set up Capistrano3 correctly on my machine and that it should be connecting to my server. When I run cap production deploy (on my machine) I receive the following error messenger:
DEBUG [2959f403] Running /usr/bin/env [ -d ~/.rbenv/versions/2.1.2 ] as [user#myIP]
DEBUG [2959f403] Command: [ -d ~/.rbenv/versions/2.1.2 ]
DEBUG [2959f403] Finished in 0.420 seconds with exit status 1 (failed).
ERROR rbenv: 2.1.2 is not installed or not found in ~/.rbenv/versions/2.1.2
(Backtrace restricted to imported tasks)
cap aborted!
SSHKit::Runner::ExecuteError: Exception while executing as [user#myIP]: exit
SystemExit: exit
Tasks: TOP => rbenv:validate
(See full trace by running task with --trace)
On my machine (iMac):
$ ruby -v
ruby 2.1.2p95
$ rbenv -v
rbenv 0.4.0
$ which rebind
/usr/local/bin/rbenv
On my server (ubuntu)
$ ruby -v
ruby 2.1.2p95
$ rbenv -v
rbenv 0.4.0-151-g83ac0fb
$ which rebind
/home/[user]/.rbenv/bin/rbenv
Any ideas how to fix this?
** More Info From Comments **
deploy.rb
# config valid only for current version of Capistrano
lock '3.4.0'
set :application, [app name]
set :repo_url, [repo location]
set :repository, [repo location]
set :scm, :git
set :deploy_user, [user]
set :user, [user]
set :use_sudo, false
set :rails_env, "production"
set :deploy_via, :copy
# setup rvm.
# how many old releases do we want to keep
set :keep_releases, 5
# files we want symlinking to specific entries in shared.
set :linked_files, %w{config/database.yml config/application.yml}
# dirs we want symlinking to shared
set :linked_dirs, %w{bin log tmp/pids tmp/cache tmp/sockets vendor/bundle public/system}
# what specs should be run before deployment is allowed to
# continue, see lib/capistrano/tasks/run_tests.cap
set :tests, []
# which config files should be copied by deploy:setup_config
# see documentation in lib/capistrano/tasks/setup_config.cap
# for details of operations
set(:config_files, %w(
nginx.conf
database.example.yml
log_rotation
monit
unicorn.rb
unicorn_init.sh
))
# which config files should be made executable after copying
# by deploy:setup_config
set(:executable_config_files, %w(
unicorn_init.sh
))
# files which need to be symlinked to other parts of the
# filesystem. For example nginx virtualhosts, log rotation
# init scripts etc.
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)}"
},
{
source: "monit",
link: "/etc/monit/conf.d/#{fetch(:full_app_name)}.conf"
}
])
namespace :deploy do
# make sure we're deploying what we think we're deploying
before :deploy, "deploy:check_revision"
# only allow a deploy with passing tests to deployed
before :deploy, "deploy:run_tests"
# compile assets locally then rsync
after :finishing, 'deploy:cleanup'
after 'deploy:publishing', 'deploy:restart'
end
Server
$ ls /home/[user]/.rbenv/versions/
[blank... ]
$
If /home/[user]/.rbenv/versions is an empty directory on the server, then you have no versions of ruby installed.
Try running rbenv install 2.1.2 on the server. You may need to install the ruby-build plugin first.

Capistrano doesn't set RAILS_ENV for bundler

In my Gemfile I specify what branch to use on a git repository based on RAILS_ENV. However when Capistrano deploys, it runs the bundle install command - and since it's run through a shell, the proper environment (staging) isn't set. It defaults to development and gives me an error stating that there's a mismatch between Gemfile.lock and what is installed.
You are trying to install in deployment mode after changing your
Gemfile. Run bundle install elsewhere and add the updated
Gemfile.lock to version control.
You have added to the Gemfile:
* source: git#bitbucket.org:MyRepository/manager-engine.git (at develop)
You have deleted from the Gemfile:
* source: git#bitbucket.org:MyRepository/manager-engine.git (at master)
You have changed in the Gemfile:
* manager from git#bitbucket.org:MyRepository/manager-engine.git (at develop) to no specified source
Gemfile:
RAILS_ENV = ENV['RAILS_ENV'] || 'development'
gem 'manager', git: "git#bitbucket.org:MyRepository/manager-engine.git", branch: "#{ [:production, :staging].include?(RAILS_ENV.to_sym) ? :master : :develop }"
i.e., use the 'develop' branch if the rails environment is anything other than 'production' or 'staging'.
deploy/staging.rb:
set :branch, :master
set :keep_releases, 2
set :stage, :staging
set :rails_env, 'staging'
set :bundle_without, [:development, :test]
set :deploy_to, '/home/useraccount/rails_deployments/staging.www'
server 'localhost', user: 'useraccount', roles: %w{web app db}
So to be the most concise:
In regular SSH terminal, to install the repository gem under the proper environment, I have to issue RAILS_ENV=staging bundle install. Otherwise, just running bundle install installs the repository from the develop branch. Since Capistrano just runs bundle install and doesn't include the RAILS_ENV, this problem is occurring. But doesn't Capistrano set :rails_env, or is that not a real system environment variable?
I guess if there's no better way...
I ended up using a method illustrated somewhere else in SO and modified it for my needs.
Modify Gemfile to contain:
# Read environment from A) Explicit set through command, B) Existence of override file's contents or C) Default to development
RAILS_ENV = ENV['RAILS_ENV'] || `cat .rails-env`.to_s.split.first || 'development'
If .rails-env contains nothing or doesn't exist, it defaults to development. Otherwise it takes the first word as the environment. To create the file from the command line, just type echo "your-environment-name-here" > .rails-env, assuming you're in the app's root directory.
You can also create the file upon every deploy with capistrano using the command above, or just create a symlink to the file and share it between deployments:
Deploy.rb:
set :linked_files, %w{ .rails-env }
So now the environment can be forced via a file in the root of your app called .rails-env. Explicit RAILS_ENV calls such as RAILS_ENV=test bundle exec ... will still work as advertised.

Passenger standalone fails to start with capistano

I'm a bit of a newbie when it comes to deploying and related things, so please be lenient.
I'm building a staging server for a bunch of hobby projects, and to do that, I'll need to
Support multiple Ruby versions
Support multiple running rails/other applications
Support multiple databases and related software
I also want the deploy script to allow deploying to production, if the day ever comes.
This background necessitates the use of RVM, Capistrano-multistage and Passenger Standalone (the other option being Mongrel, but as I wish to use Passenger in production, I figured it'd be safer to use Passenger in staging too). All of this makes deployment a little hairy.
I've gotten things cobbled together quite nicely, and everything else seems to work, but for some reason Passenger fails to start when commanded to do so from Capistrano. If I SSH into the box and type the command in myself it works nicely. What's wrong?
To aid you in your efforts to help me, here is a copy of my config/deploy.rb
set :application, "Appname"
set :repository, "path-to-git-repo-over-ssh"
set :scm, :git
default_run_options[:pty] = true
set :rvm_ruby_string, ENV['GEM_HOME'].gsub(/.*\//,"")
set :rvm_install_ruby_params, '--1.9' # for jruby/rbx default to 1.9 mode
set :rvm_install_pkgs, %w[libyaml openssl] # package list from https://rvm.io/packages
set :rvm_install_ruby_params, '--with-opt-dir=/usr/local/rvm/usr' # package support
set :use_sudo, false
before 'deploy:setup', 'rvm:install_rvm' # install RVM
before 'deploy:setup', 'rvm:install_ruby' # install Ruby and create gemset
require "rvm/capistrano"
require "bundler/capistrano"
require 'sidekiq/capistrano'
set :deploy_via, :remote_cache
set :stages, %w(staging production)
set :default_stage, "staging"
require 'capistrano/ext/multistage'
set :rails_env, lambda { stage }
set :startcmd, lambda { "cd #{current_path} && bundle exec passenger start -d -p #{passenger_port} -e #{rails_env} --pid-file=#{current_path}/tmp/pids/passenger.#{passenger_port}.pid #{current_path}" }
namespace :deploy do
task :stop do
run("cd #{current_path} && bundle exec passenger stop -p #{passenger_port}")
end
task :restart do
run("cd #{current_path} && touch tmp/restart.txt")
end
task :start do
run("#{startcmd}")
end
end
after "deploy:update_code" do
run "(echo \"#reboot /bin/bash -l -c '#{startcmd}' >>log/boot.out 2>>log/boot.err # from capistrano \" && cat #{release_path}/crontab.#{stage}) | crontab -"
end
And here is config/deploy/staging.rb
set :deploy_to, "/var/www/appname"
set :user, 'app-specific-user'
set :password, 'super-secret-password'
set :domain, '1.2.3.4'
server domain, :app, :web
role :db, domain, :primary => true
set :passenger_port, 1234
Well bloody hell.
On complete hunch, I removed the
default_run_options[:pty] = true
Setting from the file, and like magic it started working. Now if only someone would tell me why.

gems not found on target server after Capistrano deployment

I deployed my rails 3 app onto the production server using Capistrano. The deployment is correct, the application server is correctly restarted but when I log onto the production server and go into "my_path/current", I cannot run "rails c":
The program 'rails' is currently not installed. You can install it by typing:
sudo apt-get install rails
I checked the gem list and everything seems to be correctly installed though.
My config/deploy.rb file is:
require "bundler/capistrano"
# Add RVM's lib directory to the load path.
$:.unshift(File.expand_path('./lib', ENV['rvm_path']))
# Load RVM's capistrano plugin.
require "rvm/capistrano"
set :rvm_ruby_string, '1.9.3'
set :rvm_type, :user # Don't use system-wide RVM
set :application, "MyApp"
set :repository, "git#github.com:user/myapp.git"
set :scm, :git
set :branch, "master"
set :scm_verbose, true
set :scm_username, "user" # GITHUB user name
set :user, "user" # VPS user name
set :scm_passphrase, "pass" # VPS password
set :deploy_to, "/home/luc/be"
set :use_sudo, false
server "my_server", :web, :app, :db, :primary => true
# start / stop application server
namespace :deploy do
task :start do
invoke_command "cd /home/luc/be/current; bundle exec thin start -C config/thin.config"
end
task :stop do
invoke_command "cd /home/luc/be/current; bundle exec thin stop -C config/thin.config"
end
task :restart do
invoke_command "cd /home/luc/be/current; bundle exec thin restart -C config/thin.config"
end
end
Try bundle exec rails c. Also check what error, if any, is thrown when you visit the page in your browser

Error running capistrano in ubuntu 10.04 64 bits

I'm a novice with capistrano, and I'm going to deploy my app in linode on SO ubuntu 10.04 lucid 64 bits.
I'm trying to make my deploy with capistrano gem, but I get the error:
[000.00.000.00] executing command
[000.00.000.00] rvm_path=$HOME/.rvm/ $HOME/.rvm/bin/rvm-shell 'default' -c 'cd /home/user/project/current; git fetch origin; git reset --hard master'
** [out :: 109.74.195.41] bash: /home/user/.rvm/bin/rvm-shell: No such file or directory
command finished in 330ms
failed: "rvm_path=$HOME/.rvm/ $HOME/.rvm/bin/rvm-shell 'default' -c 'cd /home/user/project/current; git fetch origin; git reset --hard master'" on 000.00.000.00
ubuntu#ubuntu:~/Desktop/project$
This is my deploy.rb
# RVM
$:.unshift(File.expand_path('./lib', ENV['rvm_path']))
require "rvm/capistrano"
set :rvm_ruby_string, 'default'
set :rvm_type, :user
# Bundler
require "bundler/capistrano"
# General
set :application, "project"
set :user, "user"
set :deploy_to, "/home/user/project"
set :deploy_via, :copy
set :use_sudo, false
# Git
set :scm, :git
set :repository, "~/project/.git"
set :branch, "master"
# VPS
role :web, "my vps ip"
role :app, "my vps ip"
role :db, "my vps ip", :primary => true
set :server, :nginx
require 'capistrano_recipes'
I have installed nginx and I want to use unicorn with nginx.
I have a directory created in home/user/project in remote vps to deploy my application there.
My ruby version in vps remote host is:
ruby 1.9.2p318 (2012-02-14 revision 34678) [x86_64-linux]
Can anyone help shed some light on what I'm doing wrong?
RVM (head or 1.11.0) now supports tasks for installing RVM/Ruby, I have an article to describe the process http://niczsoft.com/2012/03/fast-deployment-using-capistrano-rvm-and-more/
You can find my deploy.rb here: https://github.com/mpapis/ad/blob/master/config/deploy.rb
And here is description on RVM site: http://beginrescueend.com/integration/capistrano/
Also worth mentioning are the new file formats not requiring trusting: http://beginrescueend.com/workflow/projects/#ruby-versions

Resources