Skip to content

Instantly share code, notes, and snippets.

@cactus cactus/py27.txt
Last active Jun 3, 2019

Embed
What would you like to do?
python-2.7.14 and python-3.7.0 -- for centos7 (rpm fpm recipes)
##### Configurable options
BUILD_VER=2.7.14
# strip symbols to slightly reduce runtime memory footprint
STRIP="yes"
# use altinstall to NOT symlink python and python2 to the python2.7 executable
ALTINSTALL="yes"
# build as user, requires configured sudo (for yum and fpm gem install).
# if not "yes", then build should be done as root
USE_SUDO="yes"
##### Build section
PY_BUILD_VER="${BUILD_VER%.*}"
PY_SHORT="${PY_BUILD_VER//.}"
INSTALL_DIR=/tmp/pybuild2x/installdir
BUILD_DIR=/tmp/pybuild2x/download
if [ "$USE_SUDO" = "yes" ]; then
CMD_SUDO="sudo"
else
if [ "$(id -u)" -ne 0 ] || [ "$(whoami)" != root ]; then
printf "%s\n" "USE_SUDO != 'yes', so must be run as root. aborting."
exit 1
fi
CMD_SUDO=""
fi
mkdir -p "${INSTALL_DIR}" "${BUILD_DIR}"
cd "${BUILD_DIR}"
curl -LO http://python.org/ftp/python/${BUILD_VER}/Python-${BUILD_VER}.tgz
tar xf Python-${BUILD_VER}.tgz
cd Python-${BUILD_VER}
# install compilers, devtools
$CMD_SUDO yum groupinstall -y "development tools"
# install build deps
$CMD_SUDO yum -y install \
bzip2-devel \
db4-devel \
expat-devel \
gdbm-devel \
libffi-devel \
ncurses-devel \
openssl-devel \
readline-devel \
sqlite-devel \
xz-devel \
zlib-devel
# install fpm
$CMD_SUDO yum install ruby ruby-devel rubygems
$CMD_SUDO gem install fpm
# note: if you want multiple minor version of python on the system, eg system python is 2.7.5 and you want 2.7.13,
# then remove --enable-shared from the configure line below.
# this will remove the ability to load python as a shared library (include python in other apps), but also avoides python
# loading the wrong minor library version depending on your libary path (and avoids messing with the system python)
# ./configure --prefix=/usr/local --enable-unicode=ucs4 --enable-ipv6 --enable-shared LDFLAGS="-Wl,-rpath /usr/local/lib"
./configure --prefix=/usr/local --enable-unicode=ucs4 --enable-ipv6 LDFLAGS="-Wl,-rpath /usr/local/lib"
# build
make -j2
if [ "$ALTINSTALL" = "yes" ]; then
make altinstall DESTDIR="${INSTALL_DIR}"
else
make install DESTDIR="${INSTALL_DIR}"
fi
if [ "$STRIP" = "yes" ]; then
for f in /tmp/installdir/usr/local/lib/libpython*; do
[ ! -L "$f" ] && chmod 755 "$f" && strip "$f"
done
strip ${INSTALL_DIR}/usr/local/bin/python${PY_BUILD_VER}
strip ${INSTALL_DIR}/usr/local/bin/python${PY_BUILD_VER}m
fi
echo '/sbin/ldconfig' > ${INSTALL_DIR}/run-ldconfig.sh
fpm -s dir -t rpm -n python${PY_SHORT} -v ${BUILD_VER} -C ${INSTALL_DIR} \
--after-install /tmp/installdir/run-ldconfig.sh \
-d 'bzip2' \
-d 'db4' \
-d 'expat' \
-d 'gdbm' \
-d 'libffi' \
-d 'ncurses' \
-d 'openssl' \
-d 'readline' \
-d 'sqlite' \
-d 'xz' \
-d 'zlib' \
--directories=/usr/local/lib/python${PY_BUILD_VER}/ \
--directories=/usr/local/include/python${PY_BUILD_VER}/ \
usr/local
# move rpm to download dir
mv python${PY_SHORT}*.rpm ../
# note: after install, you may need to do the following, depending no use of --enable-shared in the configure step:
# echo '/usr/local/lib' > /etc/ld.so.conf.d/usr-local.conf
# ldconfig -v
##### Configurable options
BUILD_VER=3.7.0
# strip symbols to slightly reduce runtime memory footprint
STRIP="yes"
# use altinstall to NOT symlink python3 to the python3.x executable (note: does not symlink python->python3.x either way)
ALTINSTALL="no"
# build as user, requires sudo (for yum and fpm gem install)
USE_SUDO="yes"
##### Build section
PY_BUILD_VER="${BUILD_VER%.*}"
PY_SHORT="${PY_BUILD_VER//.}"
INSTALL_DIR=/tmp/pybuild/installdir
BUILD_DIR=/tmp/pybuild/download
if [ "$USE_SUDO" = "yes" ]; then
CMD_SUDO="sudo"
else
if [ "$(id -u)" -ne 0 ] || [ "$(whoami)" != root ]; then
printf "%s\n" "USE_SUDO != 'yes', so must be run as root. aborting."
exit 1
fi
CMD_SUDO=""
fi
mkdir -p "${INSTALL_DIR}" "${BUILD_DIR}"
cd "${BUILD_DIR}"
curl -LO http://python.org/ftp/python/${BUILD_VER}/Python-${BUILD_VER}.tgz
tar xf Python-${BUILD_VER}.tgz
cd Python-${BUILD_VER}
# install compilers, devtools
$CMD_SUDO yum groupinstall -y "development tools"
# install build deps
$CMD_SUDO yum -y install \
bzip2-devel \
db4-devel \
expat-devel \
gdbm-devel \
libffi-devel \
ncurses-devel \
openssl-devel \
readline-devel \
sqlite-devel \
xz-devel \
zlib-devel
# install fpm
$CMD_SUDO yum install ruby ruby-devel rubygems
$CMD_SUDO gem install fpm
# note: if you want multiple minor version of python on the system, eg system
# python is 2.7.5 and you want 2.7.13, then remove --enable-shared from the
# configure line below.
# this will remove the ability to load python as a shared library (include
# python in other apps), but also avoides python # loading the wrong minor
# library version depending on your libary path (and avoids messing with the
# system python)
./configure --prefix=/usr/local --enable-optimizations --enable-ipv6 --enable-shared LDFLAGS="-Wl,-rpath /usr/local/lib"
# ./configure --prefix=/usr/local --enable-optimizations --enable-ipv6 LDFLAGS="-Wl,-rpath /usr/local/lib"
# build
make -j2
if [ "$ALTINSTALL" = "yes" ]; then
make altinstall DESTDIR="${INSTALL_DIR}"
else
make install DESTDIR="${INSTALL_DIR}"
fi
if [ "$STRIP" = "yes" ]; then
for f in ${INSTALL_DIR}/usr/local/lib/libpython*; do
[ ! -L "$f" ] && chmod 755 "$f" && strip "$f"
done
strip ${INSTALL_DIR}/usr/local/bin/python${PY_BUILD_VER}
strip ${INSTALL_DIR}/usr/local/bin/python${PY_BUILD_VER}m
fi
echo '/sbin/ldconfig' > ${INSTALL_DIR}/run-ldconfig.sh
fpm -s dir -t rpm -n python${PY_SHORT} -v ${BUILD_VER} -C ${INSTALL_DIR} \
--after-install ${INSTALL_DIR}/run-ldconfig.sh \
--provides "python(abi) = ${PY_SHORT}" \
--provides "python${PY_SHORT} = ${BUILD_VER}" \
-d 'bzip2' \
-d 'db4' \
-d 'expat' \
-d 'gdbm' \
-d 'libffi' \
-d 'ncurses' \
-d 'openssl' \
-d 'readline' \
-d 'sqlite' \
-d 'xz' \
-d 'zlib' \
--directories=/usr/local/lib/python${PY_BUILD_VER}/ \
--directories=/usr/local/include/python${PY_BUILD_VER}m/ \
usr/local
# move rpm to download dir
mv python${PY_SHORT}*.rpm ../
# note: after install, you may need to do the following, depending no use of --enable-shared in the configure step:
# echo '/usr/local/lib' > /etc/ld.so.conf.d/usr-local.conf
# ldconfig -v
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.