DEV Community

Cover image for 3rd-party binaries in Windows Git Bash
Meir Gabay for ProdOps

Posted on • Originally published at prodops.io

3rd-party binaries in Windows Git Bash

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

"..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 in Git Bash terminal, while using Visual Studio Code.
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
  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. Download Terraform for Windows (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 Clicking the first search result got me here Apache for Windows

Important! I chose to download ApacheHaus 2.4.41 x64, no need to install Bitnami WAMP Stack or WampServer, we only need the binaries. Another alternative would be Apache Lounge

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

  3. Apply required configuration (if necessary), and test the execution of the relevant binary file

    • In our case, run Notepad/Notepad++ 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
    • 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):
       $ /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
    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:
     $ 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
      $ 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 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 or run a Virtual Machine with Linux installed, for example Oracle VirtualBox for Linux Hosts
  • 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

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

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

    Shutdown apache2 process

    $ 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.

Top comments (0)