Programming

Change a Rails application to production

27 September 2026 · 6 min read

Change a Rails application to production

Transitioning a Ruby on Rails application from a development environment to a live production server is a critical phase for any software project. It’s not merely about copying files; it involves a meticulous process of configuration, optimization, and security hardening to ensure your application runs efficiently, reliably, and securely for your users. Understanding the nuances of how to change a Rails application to production is essential for minimizing downtime, maximizing performance, and providing a seamless user experience. This guide will walk you through the indispensable steps and best practices, equipping you with the knowledge to deploy your Rails application with confidence, transforming it from a local experiment into a robust, publicly accessible service.

Essential Pre-Deployment Checklist for Rails Production

Before you even think about pushing your Rails application live, a thorough pre-deployment checklist is paramount. This foundational step ensures that your application is configured correctly for the production environment, which differs significantly from development. In production, performance, security, and resource efficiency become top priorities. Overlooking these initial settings can lead to unexpected errors, security vulnerabilities, or poor application performance once live.

One of the first tasks is to configure the Rails environment settings. The config/environments/production.rb file is where you define production-specific configurations, such as caching, asset serving, and logging levels. Ensure that config.eager_load = true is set to optimize class loading, and config.log_level = :info for appropriate logging. A crucial security measure is to generate and set a unique SECRET_KEY_BASE. This key is vital for session management and preventing tampering. You can generate one using rake secret and then set it as an environment variable on your production server, never committing it directly into your repository.

Furthermore, prepare your database for the production environment. This typically involves using a robust database like PostgreSQL or MySQL instead of SQLite, which is suitable for development but not production workloads. You’ll need to create a dedicated database user with appropriate permissions and configure your config/database.yml file to reflect these production credentials. Remember to use environment variables for sensitive information like database passwords, enhancing your application’s security posture. Ignoring these preliminary steps can lead to significant headaches down the line, including data breaches or application crashes.

Database Configuration and Migrations for Live Deployment

Successfully changing a Rails application to production heavily relies on proper database configuration and execution of migrations. The database is the backbone of most applications, storing all dynamic content and user data. A misconfigured database can lead to data loss, integrity issues, or performance bottlenecks, directly impacting user experience and application stability.

The config/database.yml file dictates how your Rails application connects to its database. For production, you’ll specify the adapter (e.g., postgresql), the host, port, database name, username, and crucially, the password. It is a fundamental security practice to use environment variables (e.g., ENV['DATABASE_USERNAME']) for these credentials, preventing them from being hardcoded in your version control system. This approach safeguards your sensitive information, making your deployment more secure and flexible across different environments.

Once your database connection is configured, the next critical step is to run your database migrations. Migrations update your database schema to match the current state of your Rails application’s models. This is typically done with the command rake db:migrate RAILS_ENV=production. This command applies all pending schema changes to your production database, creating tables, adding columns, and ensuring data integrity. After migrations, you might also need to seed your production database with initial data using rake db:seed RAILS_ENV=production, especially for essential lookups or administrative accounts. Neglecting these steps can result in an application that cannot store or retrieve data correctly, rendering it non-functional.

Optimizing the Asset Pipeline and Performance

Optimizing the asset pipeline is a cornerstone when you change a Rails application to production, directly impacting load times and overall user experience. Rails’ asset pipeline streamlines the management of JavaScript, CSS, and images, but it requires specific handling for a live environment to deliver static assets efficiently. A slow-loading application can deter users, leading to higher bounce rates and a negative perception of your service.

For optimal performance in production, all your application’s assets (stylesheets, JavaScript files, images) must be precompiled. This process compresses and concatenates your assets, generating unique filenames (fingerprinting) that include a SHA256 hash. This fingerprinting allows browsers to cache assets indefinitely, and when an asset changes, its filename also changes, forcing browsers to download the new version. The command rake assets:precompile RAILS_ENV=production executes this vital step. Post-compilation, these static assets are ready to be served directly by your web server (like Nginx) or a Content Delivery Network (CDN), significantly reducing the load on your Rails application server.

Beyond asset precompilation, consider implementing other performance optimizations. This includes leveraging a CDN for global distribution of your static assets, which minimizes latency for users worldwide. Tools like Cloudflare or Amazon CloudFront can cache your assets closer to your users. Additionally, ensure your application is configured for caching, both at the application level (e.g., fragment caching) and HTTP level (e.g., using Rack::Cache). Monitoring tools can help identify bottlenecks and further refine your application’s speed. For more detailed insights on enhancing performance, explore resources like this comprehensive guide on optimizing Ruby on Rails application performance.

Infographic here
Server Setup and Automated Deployment Strategies ------------------------------------------------

To effectively change a Rails application to production, setting up the server infrastructure and implementing an automated deployment strategy are crucial. This involves selecting appropriate web and application servers, configuring them for robust operation, and choosing a deployment tool that streamlines the release process. A well-designed deployment pipeline minimizes human error, reduces downtime, and ensures consistent deployments.

A typical Rails production setup involves a combination of a reverse proxy server and an application server. Nginx (or Apache) often serves as the reverse proxy, handling incoming requests, serving static assets, and forwarding dynamic requests to your Rails application server. Popular choices for the Rails application server include Puma or Unicorn. Puma, being multi-threaded, is generally preferred for its efficiency in handling concurrent requests. You’ll need to configure Nginx to listen on port 80 (and 443 for HTTPS) and proxy requests to your Puma server, which typically runs on a UNIX socket or a specific port.

For automating the deployment process, tools like Capistrano are invaluable. Capistrano is a remote server automation and deployment tool, written in Ruby, that helps you deploy your web application to any number of machines simultaneously. It provides a Question & Answer :

How can I change my Rails application to run in production mode? Is there a config file, environment.rb for example, to do that?

This would now be

rails server -e production 

Or, more compact

rails s -e production 

It works for rails 3+ projects.