How can I set my Rails environment to development?
According to this question: How do I set my rails 3 app to development mode?, you add ENV['RAILS_ENV'] = 'development' to config/environment.rb.
I did this, but when I try to bundle install, it still tries to install gems for 'production'. I've placed the environment variable line at the start, middle, and end of file.
# Load the Rails application.
require File.expand_path('../application', __FILE__)
# Initialize the Rails application.
Grafly::Application.initialize!
ENV['RAILS_ENV'] = 'development'
This is normal. Bundler is a general-purpose dependency manager for Ruby. It has no idea that Rails exists. The group directives are exposing Bundler's groups feature, not a function of Rails.
If you don't instruct Bundler otherwise, it will install every gem from every group. It doesn't know what groups you do and don't want installed; it just knows that you defined some groups.
If you don't want to install all of your gems (or can't install all of your gems), you can skip production:
bundle install --without production
Similarly, you can skip development and testing gems when you deploy:
bundle install --without development test
(This is how, for example, Heroku and Cloud66 install only the gems you need for production.)
Related
Why does my spring gem load in the wrong (or all) environment(s)?
I have this in my Gemfile and spring gem is not listed anywhere else in the file:
group :development do
gem 'listen', '~> 3.1.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
When I ran bundle exec rails console test (for the test environment), spring processes started and the Listen module was loaded in the rails console. I made sure all spring processes were stopped beforehand.
To do a sanity check, I removed the whole development group above and bundled. Spring and listen gems were no longer loaded, as I expected.
I faced this misunderstanding in production.
Here's how I solved it:
You can also fix this issue permanently by upspringing (removing the spring gem from) your bin/ executables:
bin/spring binstub --remove --all
Or
spring binstub --remove --all
You can now run the command below to get into rails console in production
rails c --environment=production
Also, to avoid this from occurring in subsequent occasions endeavour to make sure that the spring gem is only present in development and test groups in your Gemfile.
Moreso, when you're in production make sure you always provide the --without development test argument to the bundle install command
bundle install --without development test
and not the usual or common
bundle install
Please note: As an indication, whenever you run the command rails c or rails console and you see the output below:
Running via Spring preloader in process 26651
WARNING: Spring is running in production. To fix this make sure the spring gem is only present in development and test groups in your Gemfile and make sure you always use bundle install --without development test in production
It's an indication that the spring gem is running in your production environment, and it should be stopped or removed entirely from your bin executables.
That's all.
I hope this helps
Spring is generally used through binstubs - did you install the binstubs? If so this is the file your rails command is running through.
#!/usr/bin/env ruby
begin
load File.expand_path('../spring', __FILE__)
rescue LoadError => e
raise unless e.message.include?('spring')
end
APP_PATH = File.expand_path('../config/application', __dir__)
require_relative '../config/boot'
require 'rails/commands'
As you can see it will load spring anytime you use the rails command. There is no check for the environment. If you do not want to load spring you can use DISABLE_SPRING=1 rails c test.
According the spring gem github page, it looks like rails console will load up spring:
rails console, rails generate, rails runner
These execute the rails command you already know and love. If you run
a different sub command (e.g. rails server) then spring will
automatically pass it through to the underlying rails executable
(without the speed-up).
Also, this is worrying:
You must not install Spring on your production environment. To
prevent it from being installed, provide the --without development
test argument to the bundle install command
rails console (development) would make sense, but not rails console test (or another environment). It seems buggy to me, and a reason now why I don't like the gem.
I am developing both an app and a gem for it. The gem will be available from Gemfury when the app will be in production. But while developing, I would like to use the local path of the gem, so that I can modify both the gem and the app and see changes faster. How can I do this?
I know there is bundle config local.GEM GEM_PATH, but this only works for git sources, not for Gemfury.
I can set an env var and conditionally specify gems in the Gemfile, but I hope there is a better approach to this.
if ENV['RAILS_ENV'] == 'development'
gem 'your_gem', path: '/path/to/gem'
else
gem 'your_gem'
end
Then, locally, run
RAILS_ENV=development bundle install
It's a hack, for sure, but then again, so is all of this :)
I have installed Capistrano 3 and I'm not able to get my app to call bundle install and precompile my assets on deploy.
I've seen that I have to configure my environment for RVM there http://rvm.io/deployment/capistrano#environment
But I was wondering, I have a dev computer A, and a deployment computer B, which both have RVM user-installed.
Should I configure Capistrano to use RVM on my dev computer or on the deployment computer?
Does your Capfile have the following?
require 'capistrano/rails'
require 'capistrano/rvm'
capistrano/rails includes dependencies for bundler, assets and migrations.
https://github.com/capistrano/rails/blob/master/lib/capistrano/rails.rb
https://github.com/capistrano/rvm
Also make sure to read the readme on capistrano/rvm, as you need to have the correct capistrano/bundler version.
In the rails project I'm working on I inserted support for rspec, cucumber and autotest with this Gemfile (partial)
gem 'rspec-rails'
gem 'cucumber-rails'
gem 'autotest-standalone'
gem 'autotest-rails-pure'
gem 'zentest-without-autotest'
however in order to run tests with autotest i need to execute bundle exec autotest otherwise it fails with this message
$ autotest
loading autotest/cucumber_rails_rspec_rspec2
Error loading Autotest style autotest/cucumber_rails_rspec_rspec2 (no such file to load -- autotest/cucumber_rails_rspec_rspec2). Aborting.
Now I'm developing on a Mac and I'd like to enable autotest-growl and autotest-fsevents gem, but if I insert those lines in my ~/.autotest
require 'autotest/growl'
require 'autotest/fsevent'
then I need to insert the corresponding gems in the Gemfile and everything works, but it breaks builds on my CI server (which is on Linux)
How to solve this without maintaining a different Gemfile for local and CI environments?
EDIT:
For the moment I solved with these lines in Gemfile
if RUBY_PLATFORM.downcase.include?("darwin") # I'm on Mac
gem 'autotest-fsevent'
gem 'autotest-growl'
end
It works both locally and on the CI server, I don't know if it mess something, for the moment it seems to work flawlessly.
Any cleaner way to do that is still welcome.
EDIT2:
I switched to groups solutions. While the previous monkeypatch works pretty well both in development and for continuous integration, it will gives you an error in production if you use capistrano bundler tasks for deployments or if you use bundle install --deployment option (which is advised in production)
When using the if RUBY_PLATFORM.downcase.include?("darwin") line you'll get this error on deploy.
# bundle install --deployment --without development test
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 deleted from the Gemfile:
* autotest-fsevent
* autotest-growl
So my final solution to this problem is to include platform specific gems in a given group, say osx, and then in production and on CI server exclude it using bundle.
If you use capistrano to deploy put this in your config.rb
set :bundle_without, [:development, :test, :osx]
# capistrano bundler task
require "bundler/capistrano"
You might want to use groups in your gemfile, something like:
group :development do
gem "autotest-growl"
gem "autotest-fsevents"
end
and on the server you use: $ bundle install --without development
You can handle this by taking advantage of the different Gemfile environments (testing, development, production).
Your local box can be development while the CI server is your "production" environment.
With this in mind you can edit your Gemfile to use the appropriate gems depending on the environment.
Edit: Sorry, I think I scanned your post too quickly. But you can add your ~/.autotest to .gitignore so it wont be included on your CI server.
Here's probably a very "newbieish" question on Bundler, but I'm wondering how bundle install knows what environment to use or how to set it? Or do I even need to? My problem is that I've grouped my gems (in Gemfile) by environments and when deploying now I only want production gems to be installed.
At the top of the application.rb file you can see
# If you have a Gemfile, require the gems listed there, including any gems
# you've limited to :test, :development, or :production.
Bundler.require(:default, Rails.env) if defined?(Bundler)
When Rails is booted, Bundler automatically loads all the dependencies for the :default group and current environment.
Please note that when you run bundle install, Bundler resolves and install dependencies for all the environments, unless you specify a --without option
$ bundle install --without staging development test
In production, you might also want to add the --deployment flag.
More info about bundle install.
You can use the "group" option in the gem deependency declaration. Check this ASCIICast: http://asciicasts.com/episodes/201-bundler