Deploying a Vapor 4 app to Heroku Cloud

Sai Balaji
Mac O’Clock
Published in
3 min readJul 28, 2020
Photo by Jefferson Santos on Unsplash

Introduction

Hello everyone. In this article, we are going to see how to deploy a Vapor 4 app to Heroku.

Requirements

  1. Mac OS or Linux(with Swift intalled)
  2. Terminal with HomeBrew installed.
  3. A Vapor 4 app

Note: If you are wondering what HomeBrew is it is a Package manager used to installs additional packages that Apple (or your Linux system) didn’t.You can install HomeBrew by visiting following link https://brew.sh

What is Vapor?

Vapor is an open source web framework written in Swift. It can be used to create RESTful APIs, web apps, and real-time applications using WebSockets.iOS developers can use their Swift skill not only to develop iOS client apps but also they can build backend database service, webservice etc using Swift. https://github.com/vapor/vapor

What is Heroku?

Heroku is a cloud platform as a service supporting several programming languages. Using Heroku we can host websites, deploy REST API etc. And also Heroku offers a free plan to help you learn and get started on the platform.

Steps to install Heroku in Mac

  1. You’ll need a Heroku account, if you don’t have one, please sign up here: https://signup.heroku.com/
  2. Make sure that you’ve installed the Heroku CLI tool through HomeBrew if not use the below command:
brew install heroku/brew/heroku

3. After installing Heroku CLI login to your account using the below command. Then enter your account details:

heroku login

4. To check your account name use the below command:

heroku auth:whoami

Steps to Deploy a Vapor 4 app to Heroku

  1. Heroku uses Git repository to deploy your app so create a Git repo using the below command if you have not created yet. Make sure you are inside your project folder before creating a Git repo:
git init

2. Add the file to the staging area by using below command:

git add .

3. Commit your changes:

git commit -m “Initial commit”

4. Initialize Heroku to start the deployment process:

vapor heroku init

5. It may ask some questions answer it by selecting the options. Make sure you select deployment option as build pack.

7. Then it will start compiling and push your app to Heroku.

Note⚠️ : Sometimes it may give the following error

missing LinuxMain.swift file in the Tests directory remote: ! Push rejected, failed to compile Swift app.

Then you can resolve this error by using the following command which will create the LinuxMain.swift in the test directory.

swift test — enable-test-discovery

8. After resolving the error try step 7 again. This time it should compile and push your app to Heroku.

9. After a successful deploy, it will give your app’s URL with a default random app name. The URL will not have any routes name so if you use endpoint names in your app then you should append your endpoint name to your app URL before testing it. You can also check your app status by logging into the Heroku dashboard in the browesr.

Renaming Heroku App

In some cases, you may want to rename your Heroku app or want to change your default app name in such case use the below command. Make sure you are inside your project directory before using this command.

heroku apps:rename NEWNAME_HERE

If you make any changes to your app make sure you commit and push it to Heroku remote repo.

vapor heroku push

All set! Now you have deployed your Vapor app to Heroku.

If you like this article useful then give some claps and also share with others.

--

--