Deploy Golang App to Heroku

January 07, 2022 Kehinde Olawuwo 2 minutes

    This is a step-by-step tutorial on how to deploy a Golang app on Heroku Server.

    It is short, simple and straightforward.

    Requirements.

    • Heroku Account
    • Heroku CLI
    • Golang v1.14 and above

    Step 1 - Install Heroku CLI.

    Create a Heroku account using your email and password or log in if you already have an account.

    Download the Heroku CLI by running the command below on macOS or you can also download depending on your operating system

    $ brew tap heroku/brew && brew install heroku
    

    Login to Heroku from your terminal using the command

    $ heroku login
    

    This will open your web browser to complete the login process.

    Step 2 - Create Procfile.

    Create Procfile on your Go project root folder by running the command

    $ touch Procfile
    

    or just create it from project directory.

    Run echo “web: [directory]” > Procfile replace [directory] e.g.

    $ echo “web: currency” > Procfile
    

    Step 3 - Set up Go Modules.

    Generate a go.mod file using go mod init github.com/[user_name]/[project_name] e.g.

    $ go mod init github.com/heavykenny/currency
    

    Step 4 - Set Environment Variable.

    Set variable names in .env file e.g. PORT=8080 using

    $ heroku config:set PORT=8080
    

    You can also set other variables using this command.

    Step 5 - Commit Code.

    Commit your code using the following commands

    $ git init
    
    $ git add . && git commit -m "Deploying to Heroku"
    

    Step 6 - Deploy to Heroku.

    Create a Heroku application using heroku create [project_name] e.g.

    $ heroku create go-currency
    
    $ git push heroku master
    

    You can then proceed to test your app using https://[project_name].herokuapp.com i.e.

    You can check the sample website https://go-currency.herokuapp.com

    Source code available at https://github.com/heavykenny/currency

    If you find this useful, kindly share and repost. Thank you 😊!

    NOTE: Previously posted on Medium