How to Deploy Ruby on Rails App to Heroku Quickly
To deploy a Ruby on Rails app to Heroku, first install the
heroku CLI and log in. Then create a Heroku app with heroku create, push your code using git push heroku main, and run database migrations with heroku run rails db:migrate.Syntax
Here are the main commands to deploy a Rails app to Heroku:
heroku login: Log in to your Heroku account.heroku create: Create a new Heroku app.git push heroku main: Push your local code to Heroku's remote repository.heroku run rails db:migrate: Run database migrations on Heroku.
Each command prepares or updates your app on Heroku for deployment.
bash
heroku login
heroku create your-app-name
git push heroku main
heroku run rails db:migrateExample
This example shows deploying a simple Rails app to Heroku step-by-step.
bash
cd my-rails-app
heroku login
heroku create my-rails-app-example
git push heroku main
heroku run rails db:migrate
heroku openOutput
Logging in... done.
Creating app... done, ⬢ my-rails-app-example
https://my-rails-app-example.herokuapp.com/ | https://git.heroku.com/my-rails-app-example.git
Counting objects: 10, done.
Delta compression using up to 4 threads.
Compressing objects: 100% (8/8), done.
Writing objects: 100% (10/10), 1.2 KiB | 0 bytes/s, done.
Total 10 (delta 2), reused 0 (delta 0)
remote: -----> Ruby app detected
remote: -----> Installing dependencies using bundler
remote: -----> Detecting rake tasks
remote: -----> Preparing app for Rails asset pipeline
remote: -----> Running: rake assets:precompile
remote: -----> Launching...
remote: Released v1
To https://git.heroku.com/my-rails-app-example.git
* [new branch] main -> main
Running rails db:migrate on ⬢ my-rails-app-example... up, run.1234
== 20230601000000 CreateUsers: migrating ======================================
-- create_table(:users)
-> 0.0020s
== 20230601000000 CreateUsers: migrated (0.0021s) =============================
Opening https://my-rails-app-example.herokuapp.com/ in your browser...
Common Pitfalls
Some common mistakes when deploying Rails apps to Heroku include:
- Not committing code before pushing to Heroku. Always
git add .andgit commitfirst. - Forgetting to run
rails db:migrateon Heroku, which causes database errors. - Not setting the Ruby version in
Gemfile, which can cause build failures. - Missing the
pggem for PostgreSQL, which Heroku requires instead of SQLite.
Fix example:
# Wrong: No commit before push $ git push heroku main # Right: Commit changes first $ git add . $ git commit -m "Prepare for Heroku" $ git push heroku main
Quick Reference
Summary tips for smooth Rails deployment to Heroku:
- Use PostgreSQL by adding
gem 'pg'in yourGemfile. - Specify Ruby version in
Gemfilewithruby '3.x.x'. - Commit all changes before pushing.
- Run migrations after deployment.
- Use
heroku logs --tailto check app logs for errors.
Key Takeaways
Install and log in to Heroku CLI before deploying your Rails app.
Always commit your code changes before pushing to Heroku.
Run database migrations on Heroku after deployment to avoid errors.
Use PostgreSQL and specify Ruby version in your Gemfile for compatibility.
Check Heroku logs to troubleshoot deployment or runtime issues.