Locked learning resources

Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Locked learning resources

This lesson is for members only. Join us and get access to thousands of tutorials and a community of expert Pythonistas.

Unlock This Lesson

Deploying the Application to Heroku

In this lesson, you’ll sign up for a Heroku account, install a CLI, and deploy your Flask app to Heroku.

To learn more about concepts covered in this lesson, you can check out:

00:00 Deploying the Application to Heroku. Heroku makes building and deploying applications really friendly for developers. It removes much of the burden related to building and running web applications, taking care of most infrastructure details and letting you focus on creating and improving the app.

00:21 Some of the details handled by Heroku include provisioning HTTPS certificates, managing DNS records, and running and maintaining servers. In this part of the course, you’ll learn how to deploy the previously created web application to the internet using Heroku. By the end of this section, your app will be publicly available under a nice URL and served using HTTPS.

00:46 Your first step is to create a Heroku account. If you don’t have one already, you can use the Free and Hobby Plan. It allows you to deploy non-commercial applications, personal projects, and experiments without spending money.

01:01 Follow the link seen on-screen to the Heroku signup page and create an account. You’ll be able to start using Heroku after completing the required information and confirming your email address.

01:13 The Heroku command-line interface, CLI, is a tool that allows you to create and manage Heroku applications from the terminal. It’s the quickest and the most convenient way to deploy your application.

01:26 The installation method for Heroku’s CLI depends on the operating system you’re using, so follow the link on-screen and install the Heroku CLI using the instructions that you find there.

01:38 Once you have the Heroku CLI installed, you’ll need to log in. You can do this by running the following command from a terminal. This opens a website with a button to complete the login process.

01:51 Click Log In to complete the authentication process and start using the Heroku CLI. After logging in, you’re ready to start using the Heroku CLI to manage your applications and workflows.

02:06 Now you’ll learn how to use the Heroku CLI and Git to deploy your web application. The first step is to create a file named Procfile in that project’s root directory. This file tells Heroku how to run the app.

02:19 You can create it by running the command seen on-screen.

02:27 Note that this filename must start with a capital letter, and the file cannot contain any comments. This file tells Heroku to serve your application using Gunicorn, a Python Web Server Gateway Interface (WSGI) HTTP server compatible with various web frameworks, including Flask.

02:49 Next, install gunicorn

02:57 and update the requirements file by using pip.

03:05 As files have been added and changed, they’ll need to be committed to Git. This can be done using the commands seen on-screen. Firstly, the latest versions of Procfile and requirements.txt are added to the Git repository, and then a commit with an appropriate name is created.

03:27 You’re now ready to deploy the application to Heroku. You’ll start by creating a Heroku application using the Heroku CLI. Note that this course uses realpython-flask-app as the application name. This command initializes the Heroku application, creating a git remote named heroku.

03:48 Since application names need to be unique on Heroku, you’ll need to choose a different name for your deployment. Next, you can push the Git repository to this remote to trigger the build and deployment process.

04:02 After pushing the master branch to the Heroku remote, you’ll see that the output displays information about the building and deployment process. The output indicates that Python 3.9 will be used at your application’s runtime.

04:15 This was the default version at the time of this course being recorded.

04:24 Congratulations, the app is now online. The output shows the build process, including the installation of dependencies in the deployment. Towards the bottom, you’ll find the URL for your application. In this case, it’s realpython-flask-app.herokuapp.com, as seen on-screen. The app name is before herokuapp, so now you can see why the app name needs to be unique.

04:52 You can also use the heroku open command to open your app’s URL. This command will open the application using your default web browser.

05:06 To learn how you can customize the Python version and other runtime settings, check out Heroku’s Python runtime documentation at the link seen on-screen.

05:17 Now let’s make a small change to the app and see how you can redeploy it. Edit app.py and modify the string returned by index(), as seen on-screen.

05:30 "Hello World!" is replaced by "Hello this is the new version!". This new version can be deployed on Heroku by committing and pushing the changes to the Heroku remote. Firstly, the app file is added.

05:46 Next, the commit is made.

05:53 Finally, the changes are pushed to the Heroku remote.

06:01 This triggers the build and deployment process again. You can repeat these steps whenever you need to deploy a new version of your application. You’ll notice that subsequent deployments usually take less time because the requirements are already installed.

06:22 For more details about how to use the Heroku CLI to deploy Python applications, follow the link on-screen to the Heroku documentation. In the next section of the course, you’ll see how to use Heroku pipelines to aid your development workflow.

Avatar image for Mark Fraher

Mark Fraher on Dec. 21, 2022

It looks like its no longer possible to get a free account with heroku, I had to pay for a paid account.

Avatar image for Martin Breuss

Martin Breuss RP Team on Jan. 2, 2023

@Mark Fraher yes, unfortunately Heroku recently terminated their free plans. You can check on their pricing page for the cheapest option to give this a try.

Avatar image for Peter Lukacs

Peter Lukacs on Sept. 22, 2023

Instead of git push heroku master, now you need to type git push heroku main (around 4:10 in the video)

Become a Member to join the conversation.