paint-brush
How to Deploy a Ruby on Rails App on Herokuby@ioilmio
1,622 reads
1,622 reads

How to Deploy a Ruby on Rails App on Heroku

by ioilmioAugust 31st, 2020
Read on Terminal Reader
Read this story w/o Javascript
tldt arrow

Too Long; Didn't Read

How to Deploy a Ruby on Ruby on Rails App on Heroku is a free and ‘simple” push-to-deploy system. Heroku doesn't allow you to deploy an application running on SQLite3; in production the de-facto standard for database is Postgresql. You have to install the Heroku CLI as a deploy tool, it is very simple and convenient to use. To visit and use this app you have to enable at least a Dyno on your app.

Companies Mentioned

Mention Thumbnail
Mention Thumbnail
featured image - How to Deploy a Ruby on Rails App on Heroku
ioilmio HackerNoon profile picture

The most satisfying thing beyond building something is to make it available to the world. Heroku is great for beginners because it’s a free and “simple” push-to-deploy system.

Always check that your app is working in your development environment, because it's more easy to fix something here, than on a production one.

After you created your app, you developed it,loved it and hated it, it's time to share this masterpiece to the world.

To do this, you have to sign-up for Heroku at

https://signup.heroku.com/

Disclaimer:Here we assuming you run Ruby 2.6.5 and Rails 5.2.4.3 running on Ubuntu 20.04

Then you have to install the Heroku CLI as a deploy tool. It is very simple and convenient to use.

To install the correct version for your system follow instructions at

https://devcenter.heroku.com/articles/heroku-cli#download-and-install

Working with Ruby is a pleasure, you know, but Heroku doesn't allow you to deploy an application running on SQLite3;

In production the de-facto standard for database on Heroku is PostgreSQL.

So you have to

gem install pg
in your system, then change
gem sqlite3
to
gem pg
into your
Gemfile
run
bundle install
in your terminal, ensure that your
config/database.yml
file is using the postgresql adapter like this :

default: &default  
adapter: postgresql
encoding: unicode
pool: <%= ENV.fetch("RAILS_MAX_THREADS") { 5 } %>
timeout: 5000

development: <<: *default
database: db/development.sqlite3
# Warning: The database defined as "test" will be erased and# re-generated from your development database when you run "rake".
# Do not set this db to the same as development or production.

test: <<: *default
database: db/test.sqlite3

production: <<: *default
database: db/production.sqlite3


If it's your first time using or installing PostgreSQL i suggest to follow this little walk-through, to get rid of any errors.

Heroku CLI

git add
,
git commit
, and
git push
your code onto the master branch. Now that everything is setup correctly, fresh&clean and your DB adapter is fine it's time to call the Heroku CLI.

Make sure you are in the directory that contains your Rails app, then create an app on Heroku:

$ heroku create - 
this will set your remote branch for deployment. You can check with:
$ git config --list | grep heroku

Deploy your code:

$ git push heroku master

This will fetch, install and set gem, compile your code, run bundle and all the tasks needed for your app to stay live on the web.

If you are using the database in your application, you need to manually migrate the database by running:

$ heroku run rake db:migrate

Now your app is on the Heroku server but to be able to visit and use this app you have to enable at least a Dyno on your app.

To be sure, run:

$ heroku ps:scale web=1

Your Web app is now online up and running, you can check by running:

$ heroku open

You can also check in the browser with the URL provided by Heroku.

If you want to change the URL of your app you can go to your dashboard and change the URL to whatever you like, but remember to change also remote as Heroku suggests:

$ git remote rm heroku
$ heroku git:remote -a newname

It is better to change directly from your app Git repository with:

$ heroku apps:rename newname

Here,
new name
is the name you want to give to your URL application.

Hope this will be helpful for your first but not last deploy.

Have a nice day and happy coding to everyone.

You can follow me on Twitter or Linkedin ,or follow, star and check out my project on Github.