<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" lang="" xml:lang="">
<head>
<meta charset="utf-8" />
<meta name="generator" content="pandoc" />
<meta name="viewport" content="width=device-width, initial-scale=1.0, user-scalable=yes" />
<title>3rd-party binaries in Windows Git Bash</title>
<style>
code{white-space: pre-wrap;}
span.smallcaps{font-variant: small-caps;}
span.underline{text-decoration: underline;}
div.column{display: inline-block; vertical-align: top; width: 50%;}
</style>
<style>
code.sourceCode > span { display: inline-block; line-height: 1.25; }
code.sourceCode > span { color: inherit; text-decoration: inherit; }
code.sourceCode > span:empty { height: 1.2em; }
.sourceCode { overflow: visible; }
code.sourceCode { white-space: pre; position: relative; }
div.sourceCode { margin: 1em 0; }
pre.sourceCode { margin: 0; }
@media screen {
div.sourceCode { overflow: auto; }
}
@media print {
code.sourceCode { white-space: pre-wrap; }
code.sourceCode > span { text-indent: -5em; padding-left: 5em; }
}
pre.numberSource code
{ counter-reset: source-line 0; }
pre.numberSource code > span
{ position: relative; left: -4em; counter-increment: source-line; }
pre.numberSource code > span > a:first-child::before
{ content: counter(source-line);
position: relative; left: -1em; text-align: right; vertical-align: baseline;
border: none; display: inline-block;
-webkit-touch-callout: none; -webkit-user-select: none;
-khtml-user-select: none; -moz-user-select: none;
-ms-user-select: none; user-select: none;
padding: 0 4px; width: 4em;
background-color: #232629;
color: #7a7c7d;
}
pre.numberSource { margin-left: 3em; border-left: 1px solid #7a7c7d; padding-left: 4px; }
div.sourceCode
{ color: #cfcfc2; background-color: #232629; }
@media screen {
code.sourceCode > span > a:first-child::before { text-decoration: underline; }
}
code span. { color: #cfcfc2; } /* Normal */
code span.al { color: #95da4c; } /* Alert */
code span.an { color: #3f8058; } /* Annotation */
code span.at { color: #2980b9; } /* Attribute */
code span.bn { color: #f67400; } /* BaseN */
code span.bu { color: #7f8c8d; } /* BuiltIn */
code span.cf { color: #fdbc4b; } /* ControlFlow */
code span.ch { color: #3daee9; } /* Char */
code span.cn { color: #27aeae; } /* Constant */
code span.co { color: #7a7c7d; } /* Comment */
code span.cv { color: #7f8c8d; } /* CommentVar */
code span.do { color: #a43340; } /* Documentation */
code span.dt { color: #2980b9; } /* DataType */
code span.dv { color: #f67400; } /* DecVal */
code span.er { color: #da4453; } /* Error */
code span.ex { color: #0099ff; } /* Extension */
code span.fl { color: #f67400; } /* Float */
code span.fu { color: #8e44ad; } /* Function */
code span.im { color: #27ae60; } /* Import */
code span.in { color: #c45b00; } /* Information */
code span.kw { color: #cfcfc2; } /* Keyword */
code span.op { color: #cfcfc2; } /* Operator */
code span.ot { color: #27ae60; } /* Other */
code span.pp { color: #27ae60; } /* Preprocessor */
code span.re { color: #2980b9; } /* RegionMarker */
code span.sc { color: #3daee9; } /* SpecialChar */
code span.ss { color: #da4453; } /* SpecialString */
code span.st { color: #f44f4f; } /* String */
code span.va { color: #27aeae; } /* Variable */
code span.vs { color: #da4453; } /* VerbatimString */
code span.wa { color: #da4453; } /* Warning */
</style>
<!--[if lt IE 9]>
<script src="//cdnjs.cloudflare.com/ajax/libs/html5shiv/3.7.3/html5shiv-printshiv.min.js"></script>
<![endif]-->
</head>
<body>
<header id="title-block-header">
<h1 class="title">3rd-party binaries in Windows Git Bash</h1>
</header>
<h1 id="the-claim">The Claim</h1>
<blockquote>
<p>“Git for Windows provides a BASH emulation used to run Git from the command line. <strong>*NIX users should feel right at home</strong>, as the BASH emulation behaves just like the”git" command in LINUX and UNIX environments." <a href="https://gitforwindows.org/">Git For Windows</a></p>
</blockquote>
<p>“..<strong>Linux users should feel right at home</strong>..” - Running <code>git</code> commands is quite straight forward, but what about running 3rd-party Windows binaries, and make them available in Windows Git Bash?</p>
<p>To make sure we’re on the same page - when I refer to binary files, I mean Windows executable (*.exe) files.</p>
<p>A possible solution to that is adding the binary file to PATH, but what if there’s a simpler solution?</p>
<h1 id="the-demand">The Demand</h1>
<p>Sometimes I work from home on my Desktop machine which has Windows installed on it. I need the ability to run <a href="https://github.com/99designs/aws-vault">aws-vault</a> in <a href="https://gitforwindows.org/">Git Bash</a> terminal, while using <a href="https://code.visualstudio.com/">Visual Studio Code</a>. Since aws-vault doesn’t come out-of-the-box with Git Bash, I am seeking for a simple solution.</p>
<h1 id="the-how">The How</h1>
<p>I’m going to show you how we can supply the demand by presenting two examples.</p>
<h2 id="st-example-one-binary-file">1st Example: One Binary File</h2>
<p>And of course, let’s take <code>aws-vault</code> for this example, a binary file that doesn’t have any dependencies on other data.</p>
<ol type="1">
<li>Download <strong>Windows</strong> release of the binary
<ul>
<li>In our case, <a href="https://github.com/99designs/aws-vault/releases">aws-vault/releases</a></li>
</ul></li>
<li>Rename the binary file to a name that would make sense
<ul>
<li>In our case, <code>aws-vault-windows-386.exe</code> to <code>aws-vault.exe</code></li>
</ul></li>
<li>Copy the file you renamed to <code>C:\Program Files\Git\usr\bin\</code>
<ul>
<li>In our case, <code>aws-vault.exe</code> to <code>C:\Program Files\Git\usr\bin\</code></li>
</ul></li>
<li>Open a new Git Bash terminal, and execute <code>aws-vault</code></li>
</ol>
<h3 id="what-just-happened">What just happened?</h3>
<p>We’ve made the command <code>aws-vault</code> available because we copied the file <code>aws-vault.exe</code> to the <code>/usr/bin/</code> folder. All binary files that reside in this folder are available in Windows Git Bash, from any <em>current working directory</em>.</p>
<p>And of course, you can do the same thing with <a href="https://www.terraform.io">Terraform</a>. Download <a href="https://www.terraform.io/downloads.html">Terraform for Windows</a> (I’m using 64bit). Extract <code>terraform.exe</code> from Zip file and copy to <code>C:\Program Files\Git\usr\bin\</code>. Voila! You can now use <code>terraform</code> 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 <code>/usr/bin</code> executables instead of using Windows’s environment variables.</p>
<h2 id="nd-example-library-of-binaries">2nd Example: Library of binaries</h2>
<p>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.</p>
<p>Let’s take <code>apache2</code> for this example. First of all, we need to <a href="https://www.google.com/search?q=apache%20windows">Google for Apache’s Windows release</a> Clicking the first search result got me here <a href="https://httpd.apache.org/docs/current/platform/windows.html">Apache for Windows</a></p>
<p><img src="https://i.ibb.co/PTy1p5k/apache-windows-download.png" /></p>
<p><strong>Important</strong>! I chose to download <a href="https://www.apachehaus.com/cgi-bin/download.plx">ApacheHaus</a> 2.4.41 x64, no need to install Bitnami WAMP Stack or WampServer, we only need the binaries. Another alternative would be <a href="https://www.apachelounge.com/download/">Apache Lounge</a></p>
<p>And now for the recipe</p>
<ol type="1">
<li>Extract zip file to a folder, give the folder a meaningful name
<ul>
<li>In our case, <code>httpd-2.4.41-o102s-x64-vc14-r2.zip</code> to <code>apache2</code></li>
</ul></li>
<li>Copy the folder to <code>C:\Program Files\Git\usr\lib\</code>
<ul>
<li><p>In our case, <code>C:\Program Files\Git\usr\lib\apache2</code></p>
<p><img src="https://i.ibb.co/GnQJLLp/apache-lib-folder.png" /></p></li>
</ul></li>
<li>Apply required configuration (if necessary), and test the execution of the relevant binary file
<ul>
<li><p>In our case, run Notepad/<a href="https://notepad-plus-plus.org/downloads/">Notepad++</a> in elevated mode (Run as Administrator)</p></li>
<li><p>Edit the file <code>C:\Program Files\Git\usr\lib\apache2\conf\httpd.conf</code></p></li>
<li><p>Replace <code>Define SRVROOT "/Apache24"</code> With <code>Define SRVROOT "C:\Program Files\Git\usr\lib\apache2"</code> If you wonder how I know that - <a href="https://stackoverflow.com/a/49740137/5285732">Stackoverflow answer</a></p></li>
<li><p>The file we need to execute is <code>C:\Program Files\Git\usr\lib\apache2\bin\httpd.exe</code> Execute in Git Bash (must be with elevated permissions):</p>
<div class="sourceCode" id="cb1"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb1-1"><a href="#cb1-1"></a>$ <span class="ex">/usr/lib/apache2/bin/httpd.exe</span></span></code></pre></div>
<p>If all goes according to plan (fingers crossed), Windows Firewall will prompt to <em>Allow Access</em>, and then the Apache server will start running. To verify that it works, open your browser and navigate to <a href="http://localhost:80">localhost</a> To shutdown Apache server, click on the Git Bash window, hit ESC and then CTRL+C</p></li>
</ul></li>
<li>Create a script in <code>/usr/bin/</code> that will execute the relevant binary file (sort of a shortcut)
<ul>
<li><p>In our case, execute in Git Bash:</p>
<div class="sourceCode" id="cb2"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb2-1"><a href="#cb2-1"></a>$ <span class="bu">echo</span> <span class="st">"/usr/lib/apache2/bin/httpd.exe"</span> <span class="op">></span> apache2</span></code></pre></div>
<p>Now each time that we execute <code>apache2</code>, it will run <code>httpd.exe</code></p></li>
</ul></li>
<li>Execute the script to make sure everything works as expected
<ul>
<li><p>In our case, execute in Git Bash</p>
<div class="sourceCode" id="cb3"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb3-1"><a href="#cb3-1"></a>$ <span class="ex">apache2</span></span></code></pre></div>
<p>Apache is up and running. To shutdown Apache server, click on the Git Bash window, hit ESC and then CTRL+C</p></li>
</ul></li>
</ol>
<h1 id="tips-tricks">Tips & Tricks</h1>
<ul>
<li><p><a href="https://linux.die.net/man/1/ln">Symlinks</a> doesn’t work well in Git Bash, so avoid using them. Instead, use the methods described above</p></li>
<li><p>Avoid using this method with binary folders that have multi-configuration steps. Instead, use <a href="https://docs.microsoft.com/en-us/windows/wsl/install-win10">Windows Subsystem for Linux</a> or run a Virtual Machine with Linux installed, for example <a href="https://www.virtualbox.org/wiki/Linux_Downloads">Oracle VirtualBox for Linux Hosts</a></p></li>
<li><p>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</p>
<table>
<thead>
<tr class="header">
<th>Mouse Button</th>
<th>Color</th>
</tr>
</thead>
<tbody>
<tr class="odd">
<td>Right</td>
<td>Yellow</td>
</tr>
<tr class="even">
<td>Left</td>
<td>Green</td>
</tr>
</tbody>
</table>
<p><img src="https://i.ibb.co/X5BJbxJ/vscode-as-administrator.png" /></p></li>
<li><p>To run Apache in the background (or any other process), add an ampersand (&) after the command</p>
<div class="sourceCode" id="cb4"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb4-1"><a href="#cb4-1"></a>$ <span class="ex">apache2</span> <span class="kw">&</span></span>
<span id="cb4-2"><a href="#cb4-2"></a>[<span class="ex">1</span>] 2420 <span class="co"># <-- process parent ID (PPID), don't kill that</span></span></code></pre></div></li>
<li><p>Shutdown apache2 process</p>
<div class="sourceCode" id="cb5"><pre class="sourceCode bash"><code class="sourceCode bash"><span id="cb5-1"><a href="#cb5-1"></a>$ <span class="fu">ps</span> <span class="co"># report a snapshot of the current processes</span></span>
<span id="cb5-2"><a href="#cb5-2"></a> <span class="ex">PID</span> PPID PGID WINPID TTY UID STIME COMMAND</span>
<span id="cb5-3"><a href="#cb5-3"></a><span class="ex">20652</span> 1 20652 20652 cons1 197609 18:59:21 /usr/bin/bash</span>
<span id="cb5-4"><a href="#cb5-4"></a><span class="ex">7496</span> 1 7496 7496 cons0 197609 18:59:20 /usr/bin/bash</span>
<span id="cb5-5"><a href="#cb5-5"></a><span class="ex">19852</span> 20652 19852 19852 cons1 197609 19:05:46 /usr/bin/bash</span>
<span id="cb5-6"><a href="#cb5-6"></a><span class="ex">7132</span> 1 7132 7132 ? 197609 18:56:36 /usr/bin/ssh-agent</span>
<span id="cb5-7"><a href="#cb5-7"></a><span class="ex">5816</span> 20652 5816 8208 cons1 197609 19:05:49 /usr/bin/ps</span>
<span id="cb5-8"><a href="#cb5-8"></a><span class="ex">11520</span> 19852 19852 10000 cons1 197609 19:05:46 /usr/lib/apache2/bin/httpd</span>
<span id="cb5-9"><a href="#cb5-9"></a>$ <span class="bu">kill</span> 19852 <span class="co"># <-- process id (PID)</span></span>
<span id="cb5-10"><a href="#cb5-10"></a>[<span class="ex">1</span>]+ Terminated apache2</span></code></pre></div></li>
<li><p>Now it really feels like home</p></li>
</ul>
<p>Did you like this tutorial? Clap/heart/unicorn and share it with your friends and colleagues.</p>
</body>
</html>