heroku
Comprehensive guide to Heroku CLI: install, login, create apps, deploy with Git and Docker, and manage your applications effectively.
Heroku CLI Guide
Heroku Toolbelt Installation
The Heroku CLI (Command Line Interface), also known as the Heroku Toolbelt, is essential for managing your Heroku applications. Here's how to install it on various operating systems:
macOS Installation
brew tap heroku/brew && brew install heroku
Ubuntu Installation
sudo snap install --classic heroku
Other Installation Methods
For Unix-based systems (excluding Windows), you can use:
curl https://cli-assets.heroku.com/install.sh | sh
For Debian/Ubuntu systems using apt-get:
curl https://cli-assets.heroku.com/install-ubuntu.sh | sh
For Arch Linux (community maintained):
yay -S heroku-cli
For users on ARM and BSD systems, or if other methods fail:
npm install -g heroku
Verifying Installation
After installation, verify that the Heroku CLI is set up correctly by checking its version:
heroku --version
Getting Started with Heroku CLI
Logging In
To interact with your Heroku account, you need to log in:
heroku login
This command will typically open your web browser for authentication. If you prefer to stay in the command line environment:
heroku login -i
Creating a New Heroku Application
Navigate to your project directory and create a new Heroku application:
cd ~/myapp
heroku create
If you encounter login issues, try resetting your network configuration:
mv ~/.netrc ~/.netrc.backup
heroku login
Uninstalling Heroku CLI
macOS Uninstall
You can remove the Heroku CLI using:
rm -rf /usr/local/heroku /usr/local/lib/heroku /usr/local/bin/heroku ~/.local/share/heroku ~/Library/Caches/heroku
Alternatively, if installed via Homebrew:
brew uninstall heroku
rm -rf ~/.local/share/heroku ~/Library/Caches/heroku
Linux Uninstall (Standalone Installs)
rm /usr/local/bin/heroku
rm -rf /usr/local/lib/heroku /usr/local/heroku
rm -rf ~/.local/share/heroku ~/.cache/heroku
Linux Uninstall (Debian and Ubuntu Installs)
sudo apt-get remove heroku heroku-toolbelt
sudo rm /etc/apt/sources.list.d/heroku.list
Managing and Deploying Applications with Git
Heroku integrates seamlessly with Git for application deployment. Follow these steps:
Project Setup and Commit
cd myapp # Navigate to your project directory
git init # Initialize a new Git repository
git add -f example.json # Add specific files (even if ignored by .gitignore)
git add . # Add all project files (respecting .gitignore)
git commit -m "My first commit" # Commit your changes
Creating and Linking Heroku Apps
Create a new application on Heroku:
heroku create appname # 'appname' is your chosen application name
Verify that the Heroku remote has been added:
git remote -v
If you have an existing Heroku app, you can add its remote:
heroku git:remote -a thawing-inlet-61413 # Replace with your app name
You can rename remotes for clarity:
git remote rename heroku heroku-staging
Deploying Code
Push your code to Heroku:
git push heroku master # Deploy from the master branch
git push heroku master --force # Force push if needed (use with caution)
git push heroku testbranch:master # Deploy from a different local branch to Heroku's master
Using SSH Git Transport
For SSH-based Git transport:
heroku create --ssh-git
git config --global url.ssh://git@heroku.com/.insteadOf https://git.heroku.com/ # Configure SSH for all Heroku repos
git config --global --remove-section url.ssh://git@heroku.com/ # To revert this setting
Managing and Deploying Applications with Docker
Heroku supports deploying applications packaged as Docker containers.
Setting Stack to Container
heroku stack:set container
Container Registry Login
heroku container:login
Cloning Sample Application
git clone https://github.com/heroku/alpinehelloworld.git
Creating Heroku Application
heroku create appname # 'appname' is your chosen application name
Pushing and Releasing Docker Images
Build and push your Docker image:
heroku container:push web # Push the 'web' process type
heroku container:push --recursive # Push all process types recursively
heroku container:push web worker --recursive # Push 'web' and 'worker' process types recursively
Release the pushed image to your application:
heroku container:release web
Opening Your Application
Open your deployed application in a web browser:
heroku open