Aug 31 2010
How to build git for a host with no compiler
Recently I found myself with shell access on a host without a git client installed, and also without the necessary build tools to compile it (gcc, make, etc…). However, I did have access to a machine with the same processor architecture (in fact, the same exact processor). If you mange to find yourself in this situation, what you need to do is compile git statically on the machine which does have gcc and make installed.
What’s static mean?
In the compilation process, the compiler usually must link in shared dynamic libraries. In Windows, these are called .dll files, and in linux they are usually .so files. These libraries are simply collections of compiled functions that can be reused for many different programs that require them to do a specific task. By sharing these libraries, the computer can save RAM and hard drive space by only requiring one copy of a specific library to be present for many programs that have been compiled for it.
In order to avoid unexpected behavior, a program must sometimes be compiled with a specific version of a dynamic library in mind. This isn’t always true, but in order to ensure portability and expected behavior it’s important. In linux, your package manager takes care of making sure these version dependencies are satisfied correctly. However, this can be a problem when you’re stuck on a machine for which you have no control over. You can’t know for sure what version of a specific library is installed, or when it will be upgraded. You could build your program on another machine with the same processor architecture, and with the same libraries and then just copy it over, but that leaves room for breakage down the line in case your target machine’s libraries are upgraded, or if any of the libraries on the target machine are compromised or replaced by malicious versions. Here’s where statically building comes in handy.
How to build git with static linking
This example assumes you already have access to a machine with build tools already installed. This build machine is also assumed to have the same processor architecture as your target machine. You can find the latest stable release of git at: http://git-scm.com
Here are the steps to take:
1) On your build machine, get the source code for git, unpack it, and go into the source directory:
$ wget http://kernel.org/pub/software/scm/git/git-1.7.2.2.tar.bz2
$ tar -jxvf git-1.7.2.1.tar.bz2
$ cd git-1.7.2.2
2) Configure git to install to a predetermined directory, with static linking. (Replace /home/myuser/git-static
with whatever path you want):
$ mkdir /home/myuser/git-static
$ ./configure --prefix=/home/myuser/git-static CFLAGS="${CFLAGS} -static"
3) Make git and install it to your directory
$ make
# Optional: make man pages and documentation
# Assumes you have asciidoc and other required programs on your build machine
$ make doc
# Install to your target directory
$ make install
4) Assuming all went well, now you can pack it up into a tarball for transfer to your target machine.
$ cd /home/myuser/git-static/
$ tar -cjvf git-static.tar.bz2 ./*
5) Copy it over to your target machine however you can, and unpack it to your home directory there with tar:
$ cd ~
$ tar -jxvf git-static.tar.bz2
# Check that you can use git.
# If you can't, make sure that your ~/bin directory exists in your environment's $PATH
$ git --version
6) Enjoy!
claude keswani
February 28, 2012 @ 3:19 am
Hey! This was EXTREMELY helpful. 1 addition: I had to modify the ./configure to
./configure –prefix=/home/ckeswani/git CFLAGS=”${CFLAGS} `pkg-config –static –libs libcurl`”
to resolve a
Unable to find remote helper for ‘https’
error I was getting when trying to clone or push to https hosts.
Administrator
March 13, 2012 @ 3:12 pm
I’m glad that it helped you! Also, thanks for the extra pkg-config tip!
I’ll be sure to remember that next time I’m doing a static compile ^_^
How to build git with static linking? - feed99
July 19, 2012 @ 4:58 pm
[…] the tutorial here says that I should […]
patrickw
March 29, 2013 @ 7:02 am
Thank you for the helpful post! It seems that your timing was perfect as I had just encountered this issue last night…
although it appears that I am out of luck. Once built and copied to my server I receive the following error upon entering git –version
FATAL: kernel too old
Segmentation fault (core dumped)
Are there any ways around this? as my shared hosting provider is unlikely to upgrade the kernel.
Administrator
August 15, 2013 @ 3:20 pm
Hi Patrick,
I think you’ve compiled git against a newer kernel version than your hosting provider has. I’d expect that you could note your hosting provider’s kernel version and compile git against that using an older glibc toolchain. I’m no expert at cross compilation yet myself, but perhaps these articles could be of use:
How to build git with static linking? | BlogoSfera
September 12, 2013 @ 2:02 am
[…] the tutorial here says that I should […]
epinapala
February 8, 2014 @ 2:06 am
Thanks for this, I was using Ubuntu and ran into a couple of problems and the below fixes helped me resolve the issues. sharing here If anybody else run into the same problems.
Problem 1:
undefined reference to `dlopen’ etc.
Fix : $ ./configure –prefix=/home/myuser/git-static CFLAGS=”${CFLAGS} -static” NO_OPENSSL=1 NO_CURL=1
in step 2.
http://stackoverflow.com/questions/14698013/undefined-reference-at-linking-in-static-git-build
Problem 2:
* tclsh failed; using unoptimized loading
MSGFMT po/de.msg make[1]: *** [po/de.msg] Error 127
make: *** [all] Error 2
Fix: sudo apt-get install gettext
http://scottschulz.us/2008/06/07/ubuntu-hardy-the-10-minute-git-install/