Heroku: where did my gems go? - ruby

I have been deploying a Rails 3.2 application to heroku for several weeks now. I have also been executing rake tasks on the Cedar stack where my application is located.
One day after a deploy I noticed that rake was no longer working. I get, for example, the following:
$:~/dev/my_project$ heroku run rake -T
Running `rake -T` attached to terminal... up, run.7566
bundler: command not found: rake
Install missing gem executables with `bundle install`
Trying to run commands with bundle exec yields the same results.
What I've tried:
heroku run bundle install. This works and informs me that gems were installed into ./vendor/bundle. However, heroku run ls ./vendor/bundle yields only the following:
$:~/dev/my_project$ heroku run ls ./vendor/bundle/
Runningls ./vendor/bundle/attached to terminal... up, run.3458
bin ruby
bundle package. Although the deployment works it does not help my problem.
fiddling around with the rubygems-bundler gem (although I think this is now part of core bundler). This does not seem to have any effect.

On Heroku, gems are installed within the vendor/bundle/ruby/<version>/gems directory. I just checked my Heroku instance and confirmed this.

You are going to want to use bundle exec rake task because the gems are not in the users PATH.

Related

Ruby: Unable to start worker - check_for_activated_spec

I am unable to run some of my jobs in sidekiq. And it seems to be somehow related to Bundler.. Maybe.
when I run my puma server with pumactl start in the logs I am getting:
[156149] ! Unable to start worker
[156149] /home/todd/.rbenv/versions/2.7.0/lib/ruby/gems/2.7.0/gems/bundler-2.3.25/lib/bundler/runtime.rb:309:in `check_for_activated_spec!'
Currently I'm on sidekiq 6.5.6 and Bundler 2.3.25
Anyone know of any issues with those two versions or anything else that may be causing this?
EDIT:
The interesting thing is when I start puma with bundle exec pumactl start I get a totally different error:
[ActionDispatch::HostAuthorization::DefaultResponseApp] Blocked host:
But my host is defined in my development.rb file.. in fact I've added the following, so NO hosts should be blocked:
config.hosts << /.*\.ngrok\.io/
config.hosts.clear
Then finally if I just start puma with a rails s all is fine, just my sidekiq worker won't run correctly.
Let's say that you want to start using Rails and one day you follow the general installation instructions which say that you should run this command:
gem install rails
And you get this output:
...
Successfully installed rails-7.0.1
You also start working with puma and sidekiq and install those gems for the convenience of running pumactl start and sidekiq:
gem install puma
...
Successfully installed puma-5.6.2
gem install sidekiq
...
Successfully installed sidekiq-6.4.2
Then after a day or a week or a month of tinkering you create a new Rails app:
rails new app
And since you want to use Sidekiq you add that to your Gemfile, which looks something like this:
# frozen_string_literal: true
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
gem "puma", "~> 5.6.2"
gem "rails", "~> 7.0.1"
gem "sidekiq", "~> 6.0"
But you know that there are newer versions of those gems so you update your Gemfile to look like this:
# frozen_string_literal: true
source 'https://rubygems.org'
git_source(:github) { |repo| "https://github.com/#{repo}.git" }
gem "puma", "~> 6.0.0"
gem "rails", "~> 7.0.4"
gem "sidekiq", "~> 7.0"
And then you run bundle install and the gems update. Or maybe you don't change the versions, but some day run bundle update which uses the ~> versioning operator and updates the gems to newer versions.
Here's where you're going to start running into compatibility problems.
First problem:
When you installed the sidekiq and puma and rails gems to run their scripts like pumactl they were installed using gem install ... which installed them globally and with a specific version.
When you added them to your Rails app and updated the versions they were installed separately by bundler with specific versions that are noted in Gemfile.lock.
Now your global version of puma is 5.6.2 and your bundled version of puma is 6.0.0.
Trying to manage puma using an old version of the CLI with a new version of the gem isn't guaranteed to work and can introduce hard-to-pinpoint problems. The same is true of the rails and sidekiq gems and any gem with a CLI.
Second problem:
When you run scripts like pumactl they aren't necessarily going to look at your application's Gemfile.lock and they aren't guaranteed to see or respect bundler's configuration for your Rails app when it loads it.
When you run scripts prefixed with bundle exec (like bundle exec sidekiq) it uses bundler to look at your bundled environment and ensure that all the dependencies are properly loaded.
Trying to run a bundled application without bundle exec can introduce hard-to-pinpoint problems. The same is true for any gems that have CLI tools.
Short answer
Always use bundle exec ... to run gem CLIs in your app, whether it's bundle exec rails server or bundle exec puma or bundle exec sidekiq. This will ensure that your app is started or managed using the bundled gem rather than the global version.
If you see errors when starting your app using bundle exec ... then pay attention to them because they are indicative of actual problems that need to be addressed. Likewise, if you do see errors with bundle exec but don't see errors when starting your app using globally installed gems then pay attention to them because it means your app is not portable -- it's likely that it is papering over bugs to make the app run and that your app will not run on another computer.
Extended answer
pumactl start gives you an error -- probably because you aren't using bundle exec.
bundle exec pumactl start gives you a different error -- possibly because you're bypassing the standard way to start Rails; pumactl will read configu.ru and config/puma.rb and decide how it wants to start Rails. Use bundle exec rails server instead.
rails s doesn't load your sidekiq worker -- because you aren't using bundle exec rails s it likely can't see the things it's supposed to see to start properly, because it isn't using your bundled app config
Because the errors that you're reporting are due to a misconfiguration of your system and app I can't give you any more detailed answers. You need to fix your configuration first and determine which of the three different errors you're experiencing is valid. It's a lot of work to try to work through all three questions. A standard "vote to close" reason for questions is:
Needs more focus
This question currently includes multiple questions in one. It should focus on one problem only.
I'm not voting to close your question but I'm mentioning it in case it does get closed later.
I recommend that after you fix the misconfiguration you create a new post about that specific error with a minimal reproducible example.

referencing a ruby gem from git breaks aws beanstalk deployment

I have set up my aws environment succesfully. However i am having one probelem. In my Gemfile i have the following line:
gem 'activeadmin', :github => 'gregbell/active_admin'
When i do a git aws.push i can see the following error in the aws logs:
/usr/local/share/ruby/gems/2.0/gems/bundler-1.3.5/lib/bundler/source/git.rb:177:in `rescue in load_spec_files': git://github.com/gregbell/active_admin.git (at master) is not checked out. Please run `bundle install` (Bundler::GitError)
I was able to fix that problem by creating this file: .ebextensions/ruby.config
option_settings:
- option_name: BUNDLE_DISABLE_SHARED_GEMS
value: "1"
- option_name: BUNDLE_PATH
value: "vendor/bundle"
packages:
yum:
git: []
This packages everything into vendor/bundle and my app starts correctyl.
However I have two problems with this approach:
It takes very long to deploy because it needs to install all gems every time
I am not longer able to execute commands when I ssh into the EC2 instance. For example I have to start an rpush deamon. Locally this works with bundle exec rpush development but on EC2 this results in:
bundle exec rpush
/usr/local/share/ruby/gems/2.0/gems/bundler-1.3.5/lib/bundler.rb:284: warning: Insecure world writable dir /var/app/current/vendor/bundle/bin in PATH, mode 040777
git://github.com/gregbell/active_admin.git (at master) is not checked out. Please run bundle install
Is there an other way for installing the gems correctly and at the same time using the system gems? Or if that is not possible, how can I start rpush when the gems are bundled?
Update your Gemfile.lock (bundle install&& bundle update) in your local env. and push a commit with Gemfile and Gemfile.lock. Deploy to AWS again.
Can you check if using gem 'activeadmin', 'git://github.com:gregbell/active_admin' in your Gemfile works for you?
You can also get faster deployments utilizing vendor/cache by following the instructions given here:
http://blogs.aws.amazon.com/application-management/post/Tx2XVRWSS4E971S/Locally-Packaging-Gem-Dependencies-for-Ruby-Applications-in-Elastic-Beanstalk
What solved my problem: I modified the bundle install script and added the --deployment flag

How do I properly install and run Thin?

How does one install and run Thin? According to the official page, I can start it with bundle exec thin start, but no dice. Instead I get Could not locate Gemfile. If I restart Terminal and run gem install thin again, it seems to install it again as if it weren't properly installed the first time.
The guide assumes you're using it in a Ruby project. Using bundle ... requires there to be a Gemfile in the current directory. It'll work if you cd to a Ruby project and add gem thin to your Gemfile.
Here's some info on Gemfiles: http://bundler.io/v1.3/gemfile.html

windows heroku run rake db:migrate error "/usr/bin/env: ruby.exe: No such file or directory"

I'm pretty new to Rails here and I've followed Ruby on Rails Tutorial for most of it. I have since decided to make my own application, but using the Rails 4 gem in beta. I have installed PostgreSQL on my local machine (Windows 7) and done development. Everything works great! (I love Rails much more than C#, ASP, .NET work that I do for a living!)
However, once I decided to push to Heroku, I've been pulling my hair out. Prior to pushing to Heroku, I did the standard:
c:\Sites\elms>git add .
c:\Sites\elms>git commit -m "My comment"
c:\Sites\elms>git push
c:\Sites\elms>git push heroku master
I've downloaded and installed the Heroku Toolbelt (not heroku gem.) The first error told me I needed to make sure that my Gemfile specified Ruby >= 1.9.3, so I added "ruby 1.9.3" to the top of my Gemfile:
source 'https://rubygems.org'
ruby "1.9.3"
gem 'rails', '4.0.0.beta1'
gem 'pg'
gem 'haml-rails'
gem 'httparty'
gem 'devise', git: 'git://github.com/plataformatec/devise.git', branch: 'rails4'
gem 'cancan'
gem 'bootstrap-sass'
gem 'formtastic'
gem 'formtastic-plus-bootstrap'
group :development do
gem 'annotate'
gem 'better_errors'
gem 'binding_of_caller'
gem 'guard-spork'
gem 'guard-rspec'
gem 'meta_request'
gem 'rb-notifu'
gem 'schema_to_scaffold'
gem 'wdm', :platforms => [:mswin, :mingw], :require => false
gem 'win32console'
end
group :development, :test do
gem 'rspec-rails'
gem 'pry'
gem 'spork-rails', git: 'git://github.com/sahilm/spork-rails.git', branch: 'rails-4'
gem 'spork', '~> 1.0.0rc3'
end
group :test do
gem 'capybara'
gem 'factory_girl_rails'
gem "shoulda-matchers"
end
group :assets do
gem 'sass-rails', '~> 4.0.0.beta1'
gem 'coffee-rails', '~> 4.0.0.beta1'
gem 'uglifier', '>= 1.0.3'
end
gem 'jquery-rails'
gem 'turbolinks'
gem 'jbuilder', '~> 1.0.1'
The push finally worked without any issues, but when I tried to migrate the database, I got the following:
c:\Sites\elms>heroku run rake db:migrate
Running `rake db:migrate` attached to terminal... up, run.2205
/usr/bin/env: ruby.exe: No such file or directory
I have also tried: heroku run bin/rake db:migrate, and it gives same error. I am using Getting Started with Rails 4.x on Heroku to push my Rails 4 app to Heroku. Any help would be greatly appreciated!
Oh, I also had an issue after running:
c:\Sites\elmx>gem install rails --pre
I solved this by adding C:\RailsInstaller\Ruby1.9.3\lib\ruby\gems\1.9.1\bin; to my environment Path variable in Windows 7 (it was giving me an error that it couldn't find rails when trying to execute anything such as:
rails -v
rails g controller User
But if I typed just:
rails
It would give me all of the commands I could use (as if running rails -h)
I set up my machine by using the Windows Rails Installer, then installing the Rails 4 gem. This is extremely frustrating and I've scoured the internet trying to find someone with the same problem.
EDIT 1:
I just deleted my user Path environment variable and modified my system Path environment variable to look like such:
C:\RailsInstaller\Ruby1.9.3\bin;C:\RailsInstaller\Ruby1.9.3\lib\ruby\gems\1.9.1\bin;C:\Program Files (x86)\git\cmd;C:\Program Files (x86)\Heroku\bin
Updated my path variable to the above and uninstalled Heroku Toolbelt, then re-installed it (hence why the Heroku piece is in the path variable) and tried the following:
c:\Sites\elms>ruby -v
ruby 1.9.3p125 (2012-02-16) [i386-mingw32]
c:\Sites\elms>rails -v
Rails 4.0.0.beta1
c:\Sites\elms>heroku -v
! `-v` is not a heroku command.
! Perhaps you meant `-h`.
! See `heroku help` for a list of available commands.
c:\Sites\elms>heroku run rake db:migrate
Running `rake db:migrate` attached to terminal... up, run.2100
/usr/bin/env: ruby.exe: No such file or directory
c:\Sites\elms>heroku run bin/rake db:migrate
Running `bin/rake db:migrate` attached to terminal... up, run.2872
/usr/bin/env: ruby.exe: No such file or directory
EDIT 2:
So I got a little further in this venture. I found out that I could execute:
c:\Sites\elms>heroku run bash
~ $ cd bin
~/bin $ ruby rake db:migrate
And that would then run rake and migrate my database on Heroku. But the problem gets worse! I try to navigate to the page and it tells me that there is something wrong, so I run the log file and this is what I get:
c:\Sites\elms>heroku logs
2013-03-16T05:34:20+00:00 heroku[api]: Enable Logplex by
2013-03-16T05:34:20+00:00 heroku[api]: Release v2 created by
2013-03-16T05:34:36+00:00 heroku[slugc]: Slug compilation started
2013-03-16T05:35:37+00:00 heroku[web.1]: Starting process with command `bin/rails server -p 13423 -e $RAILS_ENV`
2013-03-16T05:35:37+00:00 heroku[slugc]: Slug compilation finished
2013-03-16T05:35:37+00:00 app[web.1]: bash: bin/rails: No such file or directory
2013-03-16T05:35:39+00:00 heroku[web.1]: Process exited with status 127
2013-03-16T05:35:39+00:00 heroku[web.1]: Starting process with command `bin/rails server -p 33539 -e $RAILS_ENV`
2013-03-16T05:35:40+00:00 app[web.1]: /usr/bin/env: ruby.exe: No such file or directory
2013-03-16T05:35:41+00:00 heroku[web.1]: Process exited with status 127
2013-03-16T05:35:41+00:00 heroku[web.1]: State changed from starting to crashed
2013-03-16T05:35:41+00:00 heroku[web.1]: State changed from crashed to starting
2013-03-16T05:35:44+00:00 heroku[web.1]: Starting process with command `bin/rails server -p 23452 -e $RAILS_ENV`
2013-03-16T05:35:44+00:00 app[web.1]: /usr/bin/env: ruby.exe: No such file or directory
This is getting really frustrating. I have no clue what the heck /usr/bin/env is. The only thing I can guess is that it's like PATH for Windows. If this is the case, why does the server not know where ruby.exe is?
I did make a non Rails 4 application and was able to post it to Heroku and view it with no issues. This is really getting on my nerves.
You need to change in your application first line of the 3 files:
bin/bundle
bin/rails
bin/rake
Instead
#!/usr/bin/env ruby.exe
must be
#!/usr/bin/env ruby
That's why is error:
/usr/bin/env: ruby.exe: No such file or directory
Then you do successfully:
heroku run rake db:migrate
It's work for me.
I had a similar issue and as others have already stated, changing the below helped.
#!/usr/bin/env ruby.exe to #!/usr/bin/env ruby
However, I also needed to remove some hidden carriage returns which were still present from developing on Windows originally (I guess?).
I used dos2unix to do this.
dos2unix bin/bundle bin/rake bin/rails
After that, I committed my changes, pushed to heroku and all was good.
I had this same problem, changed ruby.exe to ruby in the three files as described above, executed the
git push heroku master
step, which told me everything was up to date. Trying the
heroku run rake db:migrate
step again, it still didn't work; I got the same error about no such file or directory for ruby.exe. I examined my gemfile.lock as directed, but didn't see anything that looked windows-y.
I then executed these:
git add .
git commit -m "My comment"
and tried again with
git push heroku master
This time, boatloads of messages went by, including:
Removing Gemfile.lock because it was generated on Windows.
so perhaps there was something wrong with it and I just didn't know what I should be looking for in it.
Trying again to
heroku run rake db:migrate
looks to have worked, as I got
Running rake db:migrate attached to terminal... up, run.3260
== CreateUsers: migrating ====================================================
-- create_table(:users)
-> 0.0409s
== CreateUsers: migrated (0.0412s) ===========================================
My best guess is that the change to remove the .exe from bundle, rake, and rails did not actually get pushed up on the first try of
git push heroku master
and the
git add .
and
git commit -m "My comment"
were necessary to make it get pushed up. This is my first attempt at anything with git/heroku/ruby, so I don't know enough to say why the other two commands were required, but leave my experience documented here in case it helps anybody else.
Thanks so much for the clue above that it was the .exe in those three files that caused the problem.
bin/bundle
bin/rails
bin/rake
Instead
#!/usr/bin/env ruby.exe
must be
#!/usr/bin/env ruby
(thanks to previous post)
make sure there are no references to windows in the gemfile.lock - they can just be deleted
then
git push heroku master
if there are still problems use
heroku logs (copying text to a text editor makes it easier to cut and paste for solutions)
Try this:
sudo apt-get install build-essential patch ruby-dev zlib1g-dev liblzma-dev
and then install Nokogiri:
gem install nokogiri

Rake aborted ! Issue installing Octopress

I m trying to install the Octopress on my windows. As per their Tutorial on their website. But when I type the command
rake install
It gives me the following error
C:\Documents and Settings\admin\octopress>rake install
rake aborted!
You have already activated rake 0.9.2.2, but your Gemfile requires rake 0.9.2. U
sing bundle exec may solve this.
(See full trace by running task with --trace)
And as the error says I need to install rake 0.9.2
I tried it doing with the following command.
C:\Documents and Settings\admin\octopress>gem install rack -v=0.9.2.0
ERROR: Could not find a valid gem 'rack' (= 0.9.2.0) in any repository
ERROR: Possible alternatives: rack
I already have Ruby & Gems installed on my PC. But I m not able to figure out how to solve this issue. How do I even use the "bundle exec" to install Octopress ?
You probably need to use the bundle exec in this way
bundle exec rake install
This might just solve you problem.
I solve this with bundle update
it's a dependency problem and not big deal
The first time you spelled it rake and the second time you spelled it rack
First update the documentation to say,
bundle exec rake install
Then update the Gemfile.lock with,
bundle update

Resources