A large part of what makes Heroku special is the user’s ability to execute a variety of tasks within the command line. Any user can create & scale apps, manage packages, adjust settings, and check the health of their Heroku app without ever loading the Heroku Dashboard. Now that we’ve taken a dive into the basics of the Unix command line (which was covered in Part One), we can start using the Heroku CLI with it.
Let’s dive in!
Before we get too far, please note that this article was written on a macOS laptop and some of the commands may be slightly different if you’re on a Windows or Linux machine.
Installing the Heroku CLI and Logging In
Before we do anything too exciting, we have to install the Heroku CLI in the command line. This is an easy task if you have Git and Homebrew installed. You will need to sign up for Heroku to get started :)
You can use the Heroku installer to add the Heroku CLI to your system. The Heroku CLI documentation goes over the installation options in more detail.
if the above method doesn’t work, you can use homebrew on most computers
If you don’t have Homebrew installed, the instructions are on the Homebrew site
Run brew tap heroku/brew && brew install heroku
inside of the command line and everything should be ready to go.
Initialize your app
You’ll need to initialize a git repository in the folder with your code. Navigate to the directory of a project that you’d like to host on Heroku and run git init
.
A common point of confusion for most early developers is between git and GitHub. The software tool git can be used locally on your own machine, or with many different online (generally called “remote”) repositories. GitHub is the largest and most popular option for a remote repository service. In this tutorial we’re going to use git to create a local repository, and Heroku as our remote repository
Authenticating with Heroku
Once you’ve got the CLI installed, you’ll need to log in to your Heroku account before you can make the most out of the tool. Run heroku login
and enter your credentials to log in this way. Next, navigate to the directory of a project that you’d like to host on Heroku and run heroku create
to create a new app. Doing this will generate a random name for your app, or you can assign a name by running heroku create [app-name-here]
.
Deploying your app to Heroku
If you browse to the URL that was generated for you when you created the app, you just get a default page that says that your app is under construction. The next phase of deployment takes just a few minutes. We need to populate our app with some code.
If your app isn’t already set up as a git repository, you’ll want to run git init.
If you’re unfamiliar with git from the command line, this is a solid guide that doesn’t go too deep into its complexity.
Once you’ve created your Heroku app, run git push heroku master
after committing to push your project directory to the URL that was established when you ran heroku create
. Do note that there are some other configuration settings that must be adjusted for this to work depending on the language that your app is written in. Luckily, you can find information on working with a variety of languages in the Heroku documentation. Heroku has beginner guides and “starter apps” available for seven different languages in the dev center.
Running your app locally
It takes perhaps half a minute to send your code up to the Heroku cloud. Pushing an app to Heroku just to have it break because of a configuration error can cause a headache.
You’ll need to install the dependencies for your application locally before this step. With a node app you can do this with npm install
The Heroku CLI offers functionality to run your app locally so that you can avoid this problem altogether. Run heroku local
to launch the app and navigate to http://localhost:5000 in your browser to view the site.
Other CLI features - Rename apps, view apps, get logs, and more
The ability to create and deploy applications is an important aspect of the Heroku CLI, but it can do a variety of other things as well. If you want to rename your app, run heroku apps:rename [newname]
in the app folder and swap out newname with the name that you want to change to. You can also see a complete list of Heroku apps that you’re a creator or contributor to by running heroku apps
. If you’re running into issues and want to see the logs associated with your project, run heroku logs
from the app directory. A complete list of commands and their functions can be found by running heroku help
.
The Heroku CLI is a versatile and powerful tool with a variety of use cases. Working with the client inside of the command line is a breeze once you’ve established some basic familiarity with it. If you’re ever looking to utilize a particular feature or need a refresher on some of the capabilities that the CLI has, you can always view the Heroku documentation here.