Created
December 6, 2015 22:42
-
-
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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 |
Statically building python does not prevents it from dynamically loading extra libraries. Those libraries needs to be build with musl-libs though.
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
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?
@GTP95 It's still available in the Web Archive: https://web.archive.org/web/20180926104719/http://general-purpose.io/2015/12/06/compiling-python-and-libpython-statically-using-musl-libc/
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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):To build a working python linked to Musl libc instead of GNU glibc:
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!