Skip to content

Instantly share code, notes, and snippets.

@ldjebran
Last active December 7, 2016 16:39
Show Gist options
  • Save ldjebran/62f2301ef9e822f5cdbdb77bd002eb5e to your computer and use it in GitHub Desktop.
Save ldjebran/62f2301ef9e822f5cdbdb77bd002eb5e to your computer and use it in GitHub Desktop.
#!/usr/bin/env bash
# Copyright 2016 Djebran Lezzoum All Rights Reserved.
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# build and install python to a local user folder
# build-python.sh ver target_folder
SCRIPT=$(readlink -f "${0}")
THIS_PATH=$(dirname "${SCRIPT}")
INSTALL_REQUIREMENTS=false
PY_FILE_BASE_URL="https://www.python.org/ftp/python"
cd ${THIS_PATH}
PY_MIN="${1}"
if [ ! ${PY_MIN} ]
then
# latest stable
PY_MIN="3.5"
fi
declare -A builds=(["2.7"]="12" ["3.4"]="5" ["3.5"]="2" ["3.6"]="0rc1")
PY_BUILD="${builds[$PY_MIN]}"
if [ ! ${PY_BUILD} ]
then
echo "no build defined for ${PY_MIN}"
exit 1
else
echo "> proceed to build python: ${PY_MIN}.${PY_BUILD}"
fi
PY_VER="${PY_MIN}.${PY_BUILD}"
PY_BIN="python${PY_MIN}"
TARGET_HOME="${2}"
if [ ! ${TARGET_HOME} ]
then
TARGET_HOME="${HOME}/bin"
else
if [ ! -d ${TARGET_HOME} ]
then
echo "target does not exits: ${TARGET_HOME}"
exit 1
fi
fi
LOCAL_PYTHON_HOME="${TARGET_HOME}/python/${PY_MIN}"
echo "> compile python to target folder: ${LOCAL_PYTHON_HOME}"
PY="${LOCAL_PYTHON_HOME}/bin/${PY_BIN}"
PY_FILE="Python-${PY_VER}.tar.xz"
if [[ ${PY_BUILD} == *"b"* || ${PY_BUILD} == *"a"* || ${PY_BUILD} == *"rc"* ]]
then
echo "> proceding with pre release build"
PY_FILE_URL="${PY_FILE_BASE_URL}/${PY_MIN}.0/${PY_FILE}"
else
PY_FILE_URL="${PY_FILE_BASE_URL}/${PY_VER}/${PY_FILE}"
fi
if [ ${INSTALL_REQUIREMENTS} == true ]
then
# install requirements
sudo apt-get update
sudo apt-get install build-essential
sudo apt-get install libncursesw5-dev libgdbm-dev libc6-dev libreadline-dev libncurses-dev
sudo apt-get install libssl-dev openssl
sudo apt-get install zlib1g-dev libbz2-dev libzip-dev libsqlite3-dev
# / install requirements
fi
if [ ! -d ${LOCAL_PYTHON_HOME} ]
then
echo "creating target folder: ${LOCAL_PYTHON_HOME}"
mkdir -p ${LOCAL_PYTHON_HOME}
fi
echo "dowloading file: ${PY_FILE_URL}"
wget "${PY_FILE_URL}"
echo "extract content from: ${PY_FILE} ..."
if [ ! -f ${PY_FILE} ]
then
echo "file does not exits: ${PY_FILE}"
exit 1
fi
tar -xf $PY_FILE
echo "remove ${PY_FILE}"
rm ${PY_FILE}
PY_SOURCE_FOLDER_NAME="Python-${PY_VER}"
cd ./${PY_SOURCE_FOLDER_NAME}
echo "configure with prefix: ${LOCAL_PYTHON_HOME}"
./configure --prefix=${LOCAL_PYTHON_HOME} --without-tk --with-ensurepip=install
# --enable-shared
make
make install
cd ${THIS_PATH}
# remove the extracted source folder
rm -r -f ${THIS_PATH}/${PY_SOURCE_FOLDER_NAME}
# export LD_LIBRARY_PATH="${LOCAL_PYTHON_HOME}/lib"
$PY -m pip install pip --upgrade
$PY -m pip install virtualenv --upgrade
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment