3rd-party binaries in Windows Git Bash
Tags: bash, git, windows, tutorial
Published: 2019-10-16 07:46:40

# The Claim
>  "Git for Windows provides a BASH emulation used to run Git from the command line.

>  **\*NIX users should feel right at home**, as the BASH emulation behaves just like the "git" command in LINUX and UNIX environments."
[Git For Windows](https://gitforwindows.org/)


"..**Linux users should feel right at home**.." - Running `git` commands is quite straight forward, but what about running 3rd-party Windows binaries, and make them available in Windows Git Bash?

To make sure we're on the same page - when I refer to binary files, I mean Windows executable (*.exe) files.

A possible solution to that is adding the binary file to PATH, but what if there's a simpler solution?

# The Demand
Sometimes I work from home on my Desktop machine which has Windows installed on it. 
I need the ability to run [aws-vault](https://github.com/99designs/aws-vault) in [Git Bash](https://gitforwindows.org/) terminal, while using [Visual Studio Code](https://code.visualstudio.com/).
Since aws-vault doesn't come out-of-the-box with Git Bash, I am seeking for a simple solution.

# The How
I'm going to show you how we can supply the demand by presenting two examples. 

## 1st Example: One Binary File
And of course, let's take `aws-vault` for this example, a binary file that doesn't have any dependencies on other data.
1. Download **Windows** release of  the binary
   In our case, [aws-vault/releases](https://github.com/99designs/aws-vault/releases)
2. Rename the binary file to a name that would make sense
   In our case, `aws-vault-windows-386.exe` to `aws-vault.exe`
3. Copy the file you renamed to `C:\Program Files\Git\usr\bin\`
     In our case, `aws-vault.exe` to `C:\Program Files\Git\usr\bin\`
4. Open a new Git Bash terminal, and execute `aws-vault`

### What just happened?
We've made the command `aws-vault` available because we copied the file `aws-vault.exe` to the `/usr/bin/` folder. All binary files that reside in this folder are available in Windows Git Bash, from any *current working directory*.

And of course, you can do the same thing with [Terraform](https://www.terraform.io). Download [Terraform for Windows](https://www.terraform.io/downloads.html) (I'm using 64bit). Extract `terraform.exe` from Zip file and copy to `C:\Program Files\Git\usr\bin\`. 
Voila! You can now use `terraform` in Git Bash. 
As long as you run Terraform in Git Bash, there's no need to add anything to PATH, since we're using Git Bash `/usr/bin` executables instead of using Windows's environment variables.

## 2nd Example: Library of binaries
It's going to be a bit trickier, but once you truly understand how it works, you'll be able to implement this method with any library of binaries.

Let's take `apache2` for this example. First of all, we need to [Google for Apache's Windows release](https://www.google.com/search?q=apache%20windows) Clicking the first search result got me here [Apache for Windows](https://httpd.apache.org/docs/current/platform/windows.html)

![](https://i.ibb.co/PTy1p5k/apache-windows-download.png)

**Important**! I chose to download [ApacheHaus](https://www.apachehaus.com/cgi-bin/download.plx) 2.4.41 x64, no need to install Bitnami WAMP Stack or WampServer, we only need the binaries. Another alternative would be [Apache Lounge](https://www.apachelounge.com/download/)

And now for the recipe

1. Extract zip file to a folder, give the folder a meaningful name
    - In our case, `httpd-2.4.41-o102s-x64-vc14-r2.zip` to `apache2`

2. Copy the folder to `C:\Program Files\Git\usr\lib\`
    - In our case, `C:\Program Files\Git\usr\lib\apache2`

      ![](https://i.ibb.co/GnQJLLp/apache-lib-folder.png)

3.  Apply required configuration (if necessary), and test the execution of the relevant binary file
     - In our case, run Notepad/[Notepad++](https://notepad-plus-plus.org/downloads/) in elevated mode (Run as Administrator)
     - Edit the file `C:\Program Files\Git\usr\lib\apache2\conf\httpd.conf`
     - Replace `Define SRVROOT "/Apache24"`
     With `Define SRVROOT "C:\Program Files\Git\usr\lib\apache2"`
     If you wonder how I know that - [Stackoverflow answer](https://stackoverflow.com/a/49740137/5285732)
     - The file we need to execute is `C:\Program Files\Git\usr\lib\apache2\bin\httpd.exe`
    Execute in Git Bash (must be with elevated permissions):
       ```bash
       $ /usr/lib/apache2/bin/httpd.exe
       ```
       If all goes according to plan (fingers crossed), Windows Firewall will prompt to *Allow Access*, and then the Apache server will start running. To verify that it works, open your browser and navigate to [localhost](http://localhost:80)
       To shutdown Apache server, click on the Git Bash window, hit ESC and then CTRL+C
4. Create a script in `/usr/bin/` that will execute the relevant binary file (sort of a shortcut)
   - In our case, execute in Git Bash:
     ```bash
     $ echo "/usr/lib/apache2/bin/httpd.exe" > apache2
     ```
     Now each time that we execute `apache2`, it will run `httpd.exe`
5. Execute the script to make sure everything works as expected
    - In our case, execute in Git Bash
       ```bash
      $ apache2
      ```
      Apache is up and running
      To shutdown Apache server, click on the Git Bash window, hit ESC and then CTRL+C

# Tips & Tricks
- [Symlinks](https://linux.die.net/man/1/ln) doesn't work well in Git Bash, so avoid using them. Instead, use the methods described above
- Avoid using this method with binary folders that have multi-configuration steps. Instead, use [Windows Subsystem for Linux](https://docs.microsoft.com/en-us/windows/wsl/install-win10) or run a Virtual Machine with Linux installed, for example [Oracle VirtualBox for Linux Hosts](https://www.virtualbox.org/wiki/Linux_Downloads)
-   Create a shortcut to Git Bash as an administrator; it's especially useful if you use Git Bash in Visual Studio Code. 
Here's how you can 
do it in 6 clicks

    | Mouse Button |    Color    |
    | -----------  | ----------- |
    |    Right     |    Yellow   |
    |    Left      |    Green    |

    ![](https://i.ibb.co/X5BJbxJ/vscode-as-administrator.png)

-   To run Apache in the background (or any other process), add an ampersand (&) after the command

    ```bash
    $ apache2 &
    [1] 2420 # <-- process parent ID (PPID), don't kill that
    ```

    Shutdown apache2 process
    ```bash
    $ ps  # report a snapshot of the current processes
             PID    PPID    PGID     WINPID   TTY         UID    STIME COMMAND
    20652       1   20652      20652  cons1     197609 18:59:21 /usr/bin/bash
     7496       1    7496       7496  cons0     197609 18:59:20 /usr/bin/bash
    19852   20652   19852      19852  cons1     197609 19:05:46 /usr/bin/bash
     7132       1    7132       7132  ?         197609 18:56:36 /usr/bin/ssh-agent
     5816   20652    5816       8208  cons1     197609 19:05:49 /usr/bin/ps
    11520   19852   19852      10000  cons1     197609 19:05:46 /usr/lib/apache2/bin/httpd
    $ kill 19852  # <-- process id (PID)
    [1]+  Terminated              apache2
    ```

* Now it really feels like home


Did you like this tutorial? Clap/heart/unicorn and share it with your friends and colleagues.