[Galaxy S 4 Q&A, Help & Troubleshooting] myudkowsky:
This post explains how to use heimdall and adb to install TWRP and then root a Galaxy S4, using MacOS and Linux tools. In particular, this post touches on problems related to the installation. I will start with a list of the problems in order to help people such as myself who are running across these issues.

Disclaimer

This worked for me on an SGH-M919 Galaxy S4 running 4.4.4. That doesn't mean you won't brick your phone, and all these procedures are at your own risk.

Problems

Perhaps the first thing to know is that some versions of adb, fastboot, and heimdall will not work on Linux. On a Ubuntu 16.0.4 LTS system, I found that the current version of adb and fastboot, both 1:6.0.1+r16-3, did not work.

While adb does connect to the S4, once the S4 is in bootloader mode (reached via adb boot bootloader but also via the boot+hold-keys method), fastboot cannot "see" the S4.

On Linux, Heimdall -- in this case, heimdall-flash version 1.4.1-1 -- also fails to work with the S4. Running in verbose mode, I received multiple messages about "empty strings." I tried to download PIT files via heimdall, and this failed as well - it would download perhaps 16 descriptions instead of 31.

After advice from [MENTION=912474]Dees_Troy[/MENTION] (for which I am quite grateful) via the trwp IRC channel, I attempted this on MacOS instead. I used "brew" to install version 1.0.36 of adb and fastboot. Again, once the S4 was in bootloader mode, both adb and fastboot also failed to find the S4.

I downloaded heimdall for MacOS directly from the official heimdall web site. This also failed: it refused to install on my version of MacOS.

Prerequisite

The S4 must be in developer mode to allow USB access for adb and heimdall.

Solutions

The solution started with finding this post on the heimdall Github site. The post contains a link to an "unofficial" version of the heimdall installer. This installer worked, and now I had the ability to continue.

Steps to installation:

Make certain you know how to boot the S4 into bootloader mode and into recovery mode. In particular, you will need to know how to boot into recovery mode using the keys. (Recovery: Hold volume up and home keys while pressing the power key; you can release the power key after the Samsung logo displays.)

Get a copy of TWRP and of SuperSu. Using adb or some other method, copy SuperSu to the S4 in a location you can easily find.

The copy of TWRP I had came in a zip file. I unzipped it and found a file called "recovery.img." This file remains on your computer.

Boot the S4 into bootloader mode. Since I had a cable already in place, I used adb to boot into bootloader.

Enter this heimdall command:

heimdall flash --no-reboot --RECOVERY /path/to/recovery.img

Please note that "--no-reboot" is essential here. If the device reboots into the standard OS, it will overwrite the recovery file you just put into memory. See the explanation on the official TWRP site.

When the download finishes, the S4 will not reboot -- it stays in bootloader mode.

First, verify that heimdall is finished. On my Mac, the download is complete when I get a prompt on my screen for the next command.

Second, turn the S4 off by pressing the power switch and holding it down until the S4 shuts down. Next, restart the S4 in recovery mode using the keys.

If you make a mistake and boot into regular OS instead of recovery mode you will end up in the old standard recovery will show up instead. This means you must re-download the recovery.img file and try again.

TWRP's recovery mode will ask permission to modify the phone -- grant it, and swipe the slider to start the process. When finished, you'll end up with a screen similar to the one shown here. Select Reboot; select System from the next screen. TWRP will offer you the opportunity to root your phone. It will ask you to locate the SuperSu file. Do so, grant permission, and you'll reboot into a rooted phone.

To access the root capabilities, you'll want the SuperSu app installed on the phone itself. You'll also find, searching the web, hints on how to use adb with root capabilities.

If you didn't download SuperSu.zip to the S4 prior to the reboot, that's fine. You can download it to the phone; reboot into recovery; select Install; and that will install SuperSu to reboot your phone. After all, the ability to perform this install is one of the reasons to flash the S4 with TWRP.