I got it, you are new in this crazy and beautiful world of Ruby on Rails, you worked hard in understanding the basic principles of the framework, and when you don’t have a single error in your code and your app doesn’t break, you decide that is time to deploy your application and let all the world contemplate your awesome job, and then, Heroku happens.
Do you need to deploy your app? There are multiple services you can use, my top 3 are:
- Amazon Web Services
- Digital Ocean
For AWS you need to know what you are doing in every single step of the process, this means you need to study AWS and choose the right services for the right deployment, it’s the best option if you put enough time into it. Digital Ocean it’s a very good service, user-friendly, you need less configuration than AWS, but in the end, you need to configure multiple gems and files to achieve the deployment. Finally Heroku, my dear dear Heroku, you can expect ZERO configuration and ZERO issues, everything it’s done automatically, and your deployment’s ready in a few minutes, the faster and easier of all the deployments… but not so much.
The most probable scenario that you are here it’s because you tried to deploy by yourself and fail miserably, but don’t take it personally, I was there, I am there from time to time. Yes, Heroku packs everything automatically but, and this is a big “but”, you need to arrange your app before the deployment, the best way of think about Heroku is like a sugar daddy that can give you anything you want just if you dress only with the clothes he asks to, everything outside the options it’s a big fail.
So without any more delay, the perfect deployment is here, it’s easier if you take these steps at the beginning of your project, but you can configure everything at the end, although I don’t recommend it.
1. Install PostgreSQL
Heroku only works with this database, if you are trying to work with another DB, you are doomed, so go along with and install this DB globally (Here an incredible article), after you create your rails app, change the gems and set up the database.yml (Here is how).
Sometimes and maybe sometimes, you are gonna hit an error after run “Bundle”, don’t worry I got you cover, here you have the solution.
2. Check your Ruby version
By the time I’m writing this article (April 2021), Heroku works with something called Heroku-20 Stack, this Stack works better with ruby 2.7.2, so, if you are working with ruby 2.7.0, 2.7.3, or 3.0.X, my best suggestion is to set your app with ruby 2.7.2, check this article about how to set a specific ruby version.
Maybe you will be tempted to set another Stack, like Heroku-18 or Heroku-16, this can get you more trouble, so stay with me and go with the flow.
3. Install the right Bundle version
This is the most common mistake, the developers of Heroku really love a version and only one version of Bundler, if you try to deploy with another version you will epically fail; so if you have the right PostgreSQL installation and the right Ruby version, and after all, you are getting some weird errors on your console and your deployment fail(even if the name of the error is nothing reassemble to Bundler), trust me, it’s your Bundler version.
The magical Bundler version is 2.1.4, if you have another version, no matter which is, you are gonna hate everything and everyone in every single attempted deployment. Here is an article about how to set up a specific Bundle version in your Rails app.
4. Launch your Rails App
Now we are on the right path, congrats.
The next steps are super easy to follow. To start you need to tell Heroku you want the connection so:
Now you have a new Heroku app with an incredible name, something like stark-wave-72819.
Now you need to connect git with Heroku for this enter the next command:
git push -u heroku branch:master
The “branch” means that you can set whatever branch you are working on or the branch you want to deploy, e.g. `main:master` or `develop:master` or `my_awesome_branch:master`. There you go.
Now it turns to migrate some databases:
heroku run rails db:migrate
If you have a seed you can run the next command if you don’t, skip it:
heroku run rails db:seed
And that’s it, your app is finally deployed, just open it:
If for some reason, your deployment fails because you don’t believe me that the only good version of Bundler is 2.1.4 (or something else), you need to destroy your app and start over, hopefully with the right version of Bundler.
So, this is the command to destroy your app:
You don’t need to send me money, just share this article with another developer you care about. Happy coding for everyone and happy deployment this time.