Day 7: Deployment

What is deployment?

Deployment makes our application available for use. For a web application it means putting it on the Internet in most cases. This is exactly what we’re going to do in this chapter. In order to put our application on the Internet, we need a server. A server is a computer which has an access to the Internet. When we run bundle exec rails server command and go to http://localhost:3000, our laptop is the server. It is ok for your own use. In order to make it available for everyone, we need to upload our app to a server and run it there. The server will have a URL assigned to it, so everyone can access it.

Setting up Heroku

We’re going to use a service called Heroku. Here are 3 reasons why:

  1. It’s free for our small app
  2. It’s easy to setup and deploy to it.
  3. They provide you with both: a server and a URL.

Let’s set it up, it’s really easy.

Sign up for Heroku account

Open up your browser, go to http://heroku.com/signup and sign up for an account.

Install heroku-toolbelt

Go to the terminal and install heroku-toolbelt. It’s a set of command line tools that we will use to deploy to Heroku

$ brew install heroku-toolbelt

Authenticate with heroku

Go back to the terminal and run

$ heroku login

It will ask you for your email and password from your account. Type it in (don’t copy and paste). **It will also ask you whether you want to upload a SSH key. Do it, it will make it easier to deploy. If you already have existing keys it will ask you which one you want to upload. If you don’t have one, it will generate a new key for you.

Install heroku-push plugin

It will allow us to push our app straight from our laptop. Go back to the terminal and run this command:

$ heroku plugins:install https://github.com/ddollar/heroku-push

Push to Heroku

We’ve created an account and installed heroku command line tools on our local machine. Now it’s time to deploy our todo application to Heroku.

First, let’s create an app on heroku. Go to your terminal. Navigate to todo project directory and run this:

Heroku app name and URL

Notice that it gave us the app name and the URL. App name is random for everyone. My app name is “still-taiga-7739”, your name will be different. Remember the app name. Put your app name in place of your_app_name variable for the next set of commands.

Next, add PostgreSQL database to you app. Don’t forget to use your app name instead of your_app_name variable.

$ heroku addons:add heroku-postgresql:dev --app your_app_name</pre>

Now it’s time to push our app to Heroku. Ready ? Let’s do it!

$ heroku push --app your_app_name

It might take a while to upload code and build an application. After it’s done, let’s run our migrations for the newly deployed app.

$ heroku run bundle exec rake db:migrate --app your_app_name</pre>

That’s it! Remember the URL from heroku create command ? Go to your browser, navigate to that URL. You new shiny todo app should be ready to use.

TODO app deployed to Heroku

What happened here

Now you understand what it takes to put your app on the Internet. Not only you can deploy your todo app. You can deploy pretty much any Rails app. This is very valuable! Now you can use this app from anywhere. You can even share it with your friends. Just send them a link.

We started from nothing. Now we have a web application running on the Internet. This is awesome!

Photo by jon_a_ross