Rails - Resolve .env conflicts between development and Kamal deploy
Problem
We often use the gem Dotenv to handle environment variables on development machine or gem Foreman also automatic load ENV from .env file (Forceman - Per Process Environment Variables)
The problems is Kamal-deploy use .env too, for the deployment on target service. The command `kamal envify` will build the .env file from env.erb file, and use that .env as input for other command
Solution
Use gem Dotenv in your development repo, then create an .env.development or .env.development.local file to store ENV for development, don't use .env file
![](https://vinguyen.blog/content/images/2024/09/rails-gem-dot-env.png)
group :development, :test do
gem 'dotenv', '~> 3.1'
end
![](https://vinguyen.blog/content/images/2024/09/CleanShot-2024-09-23-at-10.49.04.png)
Then in application.rb, add an config to ignore .env file
![](https://vinguyen.blog/content/images/2024/09/dotenv-kamal-application-rb--1.png)
# application.rb
module Launchpad
class Application < Rails::Application
# Initialize configuration defaults for originally generated Rails version.
config.load_defaults 8.0
# ....
Dotenv::Rails.files.delete('.env') if defined?(Dotenv::Rails)
end
end
Dotenv will load variable from other .env file and ignore all content from .env.