Deploying your first app is exciting! Let's take a look at what it takes in a Rails case.
First, a couple of key disctions between a local server and a production server:
A production server needs to be accessible at all times so anyone can go check it out.
The database will not be transmitted so once deployed, you need to create your own production database - only application code gets transmitted.
Quick aside: all of the instructions covered will be for Mac users. No worries Windows users! I will provide links to help guide you to the info you need along the way. This blog also assumes that you are currently using a popular version control system - Git.
Once you create your free Heroku account, you'll need the Heroku command-line interface (CLI) tools. It's time to install the Heroku CLI. To do so run the following command:
brew tap heroku/brew && brew install heroku
You can verify the installation went smoothly but running
heroku in the command line. If installed correctly you will get the version as well as a list of commands available to you. Windows users check here.
Next, you will need to log into the Heroku account you just created. Run this command:
Then follow the instructions in the terminal. It should take you to the browser to log in.
Now, following along with the instructions which can be found here the next step is to create a new rails app. For this blog, I have already created a simple Rails app and I am assuming you have already made one too if you are reading this - I am skipping this step.
So, now it's time to deploy the application to Heroku. If you are following along here, you should scroll down until you find the title "Deploy your application to Heroku". Run the following:
Great! Now we have a shell app created by Heroku with a link available to us. However, before our application code is deployed we need to get it production-ready which involves setting up our database gem for production.
Ruby comes with SQLite which is fine for development (especially small apps) but not so great for production (it's not a production-ready database) and will not work with Heroku. Heroku uses PostgreSQL as its default. To solve this we will change some things in the
Gemfile of our project:
The bit that was added to the
group :production do gem 'pg' end
The gem for PostgreSQL (aka Postgre) was added.
Now, look back at the
Gemfile, or the code snippet of mine above, and notice this line -
gem 'sqlite3', '~> 1.4'. At this position in the
Gemfile, this gem is made available universally in our app and we no longer want SQLite enabled for production. We want SQLite to be available only for development and test environments. Cut
gem 'sqlite3', '~> 1.4' and move it:
group :development, :test do # Call 'byebug' anywhere in the code to stop execution and get a debugger console gem 'byebug', platforms: [:mri, :mingw, :x64_mingw] gem 'sqlite3', '~> 1.4' end
Because we have altered our
Gemfile it's time to run
bundle install. However, we do not want the gem we added for production -
gem 'pg' to be installed locally. So, we run this:
bundle install --without production
This step makes the necessary updates to the
Gemfile.lock for production and our application code is ready to be deployed. Now is a good time to make a commit.
Now it's finally time to push our master/main branch to Heroku:
git push heroku main
A quick note about the code above. If you see an error message that looks like this:
error: src refspec main does not match any error: failed to push some refs to 'https://git.heroku.com/sheltered-ocean-64484.git'
git push heroku master
I won't go into exactly why this happens right now. Just know that some in the industry are changing terminology from "master" to "main".
Once it is working, it is going to deploy to the Heroku app that we created! This step can take a minute or two and when finished you should see a line in the terminal that looks something like this:
remote: https://sheltered-ocean-64484.herokuapp.com/ deployed to Heroku
Go to that link to check out your deployed app!
Now let's change the auto-generated name Heroku gave our app, "sheltered-ocean-64484" in my case, by running the following from the terminal while in your app's directory:
heroku rename myappname
Now you can go to:
http://mynameapp.herokuapp.com/ to view your app!
That's it for now. If we added some more features like database tables and migration files we would need to perform a couple more steps from our terminal. Check here for that info. Scroll to the "Migrate your database" section.
This blog covered:
- Installing Heroku
- Creating a Heroku app
- Making the necessary changes to our application code in preparation for deployment
- Deploying the app and changing its name
I hope you found this tutorial to be useful. Please ask questions, leave feedback, and share.