Skip to content

Instantly share code, notes, and snippets.

@xhd2015
Last active September 30, 2025 10:31
Show Gist options
  • Select an option

  • Save xhd2015/f9718938c4a1211881defe4856e83330 to your computer and use it in GitHub Desktop.

Select an option

Save xhd2015/f9718938c4a1211881defe4856e83330 to your computer and use it in GitHub Desktop.
Cross build git for linux/amd64 on MacOS M1, using podman
# install podman
arch -arm64 brew install podman
# init podman
podman machine init
podman machine start
# build
podman build --platform linux/amd64 -t git-static-builder -f Dockerfile-build-git-static .
# verify
podman run --platform linux/amd64 -d --name git-runner git-static-builder /usr/local/git-bin/bin/git --version
podman run --platform linux/amd64 -d --name git-runner git-static-builder /usr/local/git-bin/bin/git clone https://github.com/xhd2015/xgo
# copy
podman cp git-runner:/usr/local/git-bin.tar.gz ./git-linux-amd64.tar.gz
# remove runner
podman rm -f git-runner
# check file (40M+)
file ./git-linux-amd64.tar.gz
# untar on target machine
tar -xzf git-linux-amd64.tar.gz -C /usr/local
# Stage 1: The Builder
# Use a Ubuntu latest(24.04) base image, specifying the target architecture.
FROM --platform=linux/amd64 ubuntu:latest AS builder
# debug info
# RUN lsb_release -c
RUN cat /etc/os-release
# add deb-src to sources.list so that we can build git from source code
RUN echo "deb-src http://archive.ubuntu.com/ubuntu/ noble main restricted universe multiverse" >> /etc/apt/sources.list
# download git source code that is used by official deb package repository
WORKDIR /git
RUN apt update
RUN apt install dpkg-dev -y
RUN apt source git
RUN ls
# install build dependencies
RUN apt-get install build-essential dh-autoreconf libcurl4-openssl-dev libexpat1-dev gettext libz-dev libssl-dev asciidoc xmlto docbook2x -y
RUN apt-get build-dep git -y
# Enter source directory
WORKDIR /git/git-2.43.0
# Configure and compile Git as a static binary
# LDFLAGS="--static" is the key flag for static linking.
# NO_GETTEXT=1 avoids a dependency that is difficult to link statically.
RUN make configure
RUN ./configure LDFLAGS="-static" --prefix=/usr/local/git-bin --without-tcltk --with-curl --with-openssl --with-expat
RUN make -j$(nproc) LDFLAGS="--static" NO_GETTEXT=1 all
RUN ./git --version
# install and pack all binaries(NOTE: git is not a single-binary program)
RUN make install
# ls would show: bin/, libexec/ and share/
RUN ls /usr/local/git-bin
RUN cd /usr/local/git-bin && tar -czf /usr/local/git-bin.tar.gz .
@xhd2015
Copy link
Author

xhd2015 commented Sep 30, 2025

There is another way to install newest git without building it:

# on newer ubuntu
apt-get download git
mv *.deb git-v2.43.0-linux-amd64.deb

# on another OS
dpkg -i git-v2.43.0-linux-amd64.deb
apt update
apt install -f # apply fix

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment