Skip to content

Instantly share code, notes, and snippets.

@maddouri
Created December 6, 2015 22:42
Show Gist options
  • Star 27 You must be signed in to star a gist
  • Fork 7 You must be signed in to fork a gist
  • Save maddouri/c4b97474f21fabc9ef61 to your computer and use it in GitHub Desktop.
Save maddouri/c4b97474f21fabc9ef61 to your computer and use it in GitHub Desktop.
A simple script that builds static versions of Python and LibPython using musl-libc
#!/bin/bash
# set -eux
# This a simple script that builds static versions of Python and LibPython using musl-libc
# Find the associated article at: http://general-purpose.io/2015/12/06/compiling-python-and-libpython-statically-using-musl-libc/
WORKING_DIR="/code/static-python"
MUSL_PREFIX="/code/static-python/musl"
PY_PREFIX="/code/static-python/python"
# COMPILER="gcc"
# COMPILER_VERSION="4.8"
COMPILER="clang"
COMPILER_VERSION="3.7"
# make the compiler's actual command name
export CC="${COMPILER}-${COMPILER_VERSION}"
# prepare the working directory
mkdir --parents "${WORKING_DIR}"
# download/extract/build static musl libc
cd "${WORKING_DIR}"
wget "http://www.musl-libc.org/releases/musl-1.1.12.tar.gz"
tar -xzf musl-1.1.12.tar.gz
cd musl-1.1.12
./configure --prefix="${MUSL_PREFIX}" --disable-shared
make
make install
# enable the "musl compiler"
export CC="${MUSL_PREFIX}/bin/musl-${COMPILER}"
# download/extract/build static python/libpython
cd "${WORKING_DIR}"
wget "https://www.python.org/ftp/python/3.5.0/Python-3.5.0.tar.xz"
tar -xJf Python-3.5.0.tar.xz
cd Python-3.5.0
./configure --prefix="${PY_PREFIX}" --disable-shared \
LDFLAGS="-static" CFLAGS="-static" CPPFLAGS="-static"
make
make install
# done ! (ignore any error that might happen during "make install")
# we now have:
# ${MUSL_PREFIX}/bin/musl-gcc : "static gcc" that uses musl
# ${PY_PREFIX}/bin/python3.5 : static python interpreter
# ${PY_PREFIX}/lib/libpython3.5m.a : static libpython
# ${PY_PREFIX}/include/python3.5m : include directory for libpython
@grobbie
Copy link

grobbie commented Sep 24, 2018

Thanks for the pointers.
I discovered that statically building python prevents it from dynamically loading extra libraries so I switched to dynamic linking.
I was only able to get this to work (mostly) by forcing the GNU libraries out of scope. Note I used python 2.7.
I had to build musl with fPIC enabled (I also had to remove some duplicate macros from the musl header files to get it to build):

CC="gcc"
./configure --prefix=/work/target/musl --exec-prefix=/work/target/sysroot --syslibdir=/work/target/sysroot CFLAGS="-fPIC" LDFLAGS="-fPIC"
make
make install

To build a working python linked to Musl libc instead of GNU glibc:

export CC="musl-gcc"
sudo mv /usr/include/x86_64-linux-gnu /usr/include/x86_64-linux-gnu.temp
./configure --prefix=/work/target/sysroot/opt/python27 CFLAGS="-I/work/target/sysroot/include -I/work/musl/musl-1.1.20/include -I/work/kernel-headers/x86_64/include -I/usr/include/x86_64-linux-musl/" --disable-shared LDFLAGS="-idirafter -L/work/target/sysroot/musl/lib -L/work/target/sysroot/include -L/work/musl/musl-1.1.20/include -L/work/kernel-headers/x86_64/include -L/usr/include/x86_64-linux-musl/ -fPIC -lgcc"
make
make install

I know it doesn't depend on glibc now, it can be demonstrated by building a "FROM: scratch" docker image with no glibc onboard and watching it go!

@kindofblue
Copy link

Statically building python does not prevents it from dynamically loading extra libraries. Those libraries needs to be build with musl-libs though.

@zn3x
Copy link

zn3x commented Apr 17, 2020

I did try this many times on a normal distribution that use's glibc. My script will always load shared libraries when I add some modules like paramiko. Best way to do it is on alpine

@GTP95
Copy link

GTP95 commented Jan 22, 2024

The associated article at http://general-purpose.io/2015/12/06/compiling-python-and-libpython-statically-using-musl-libc/ doesn't exist anymore, could you please provide a new link?

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