Skip to content

Instantly share code, notes, and snippets.

@nikcub
Last active December 12, 2018 16:14
Show Gist options
  • Save nikcub/49885e62cf8b0bff8cf9780aeb2bf7e3 to your computer and use it in GitHub Desktop.
Save nikcub/49885e62cf8b0bff8cf9780aeb2bf7e3 to your computer and use it in GitHub Desktop.
Install Python 2.7.11 on Ubuntu 14.04

Install latest Python 2.7.11 on Ubuntu 14.04

This script will install the latest version of the 2.7.x branch of Python alongside the system Python in the users local directory and it will symlink all the binaries in ~/bin

Any app or daemon that you require to run with the latest Python just run it as either the separate user or make sure it is running from the Python linked into `/bin

Arguments

install-python.sh <version> <path>

version defaults to 2.7.11

path defaults to /opt/$USER

Install as a separate user

$ sudo useradd python-latest -m -s /bin/bash
$ sudo adduser python-latest sudo
$ sudo passwd python-latest
$ su -l python-latest

Download Script

$ wget "https://gist.githubusercontent.com/nikcub/49885e62cf8b0bff8cf9780aeb2bf7e3/raw/99713739596dc71584abc02cc4c56a7020a649d7/install-python.sh" -O install-python.sh

Install Script

$ sh install-python.sh
(python will install)
$ python -V
Python 2.7.11
$ which python
/home/user/bin/python
#!/bin/bash
# Install Python in users local directory linked from ~/bin
# Specify version on command line or defaults to 2.7.11
VERSION=${1:-2.7.11}
ENV_BASE=${2:-"/opt/$USER"}
BUILD_ESSENTIALS="dpkg-dev g++ gcc libc-dev make"
PYTHON_REQUIREMENTS="libreadline-gplv2-dev libncursesw5-dev libssl-dev libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev"
PYTHON_BINS="python python2.7 2to3 idle pydoc python2 python2-config python2.7-config python-config"
PYTHON_BINS_EXTRAS="easy_install pip pip2 pip2.7 easy_install-2.7"
echo "Installing Python $VERSION"
function _func_checkroot {
if [ $EUID -eq 0 ]; then
echo "Don't run as root";
exit 1;
fi
}
function _func_cansudo {
_CAN_SUDO=`sudo -n uptime 2>&1|grep "load"|wc -l`
if [ ${_CAN_SUDO} -gt 0 ]
then
return 0
else
return 1
fi
}
function _func_package_exists {
return dpkg -l $1 2>/dev/null | grep 'ii' | wc -l
}
function _func_ifinstall {
if ! _func_package_exists $@ ; then
echo "Found $@"
else
echo "Installing $@"
sudo apt-get install -y $@
fi
}
function _func_ifclean {
if _func_package_exists $@ ; then
echo "Removing $@"
sudo apt-get purge -y $@
fi
}
function _sys_installreq {
for pkg in $BUILD_ESSENTIALS $PYTHON_REQUIREMENTS; do
if dpkg --get-selections | grep -q "^$pkg[[:space:]]*install$" >/dev/null; then
echo -e "$pkg is already installed"
else
# if ! _func_cansudo; then
# echo "Could not sudo please run as user who can"
# exit 1
# fi
if sudo apt-get -qq install $pkg; then
echo "Successfully installed $pkg"
else
echo "Error installing $pkg"
fi
fi
done
}
function _sys_update {
echo "Running system update commands"
sudo apt-get -y update
}
function _sys_clean {
_func_ifclean $BUILD_ESSENTIALS $PYTHON_REQUIREMENTS
}
function _sys_prep {
echo " * Preparing environment in $ENV_BASE"
sudo mkdir -p $ENV_BASE/{packages,builds}/python
sudo chown -R $USER:$USER $ENV_BASE
}
function _sys_link {
if [ ! -d "$HOME/bin" ] ; then
mkdir "$HOME/bin"
fi
for bin in $@; do
if [ -h "$HOME/bin/$bin" ] ; then
rm "$HOME/bin/$bin"
fi
if [ -f $ENV_BASE/builds/python/current/bin/$bin ] ; then
ln -s $ENV_BASE/builds/python/current/bin/$bin "$HOME/bin/$bin"
else
echo "Could not link $bin not found: $ENV_BASE/builds/python/current/bin/$bin"
fi
done
source ~/.profile
}
function _py_download {
echo " * Downloading and unpacking Python $VERSION"
wget -nv https://python.org/ftp/python/$VERSION/Python-$VERSION.tgz -O $ENV_BASE/packages/python/Python-$VERSION.tgz
if [ ! -f $ENV_BASE/packages/python/Python-$VERSION.tgz ] ; then
echo "Could not download - file not at $ENV_BASE/packages/python/Python-$VERSION.tgz"
exit 0
fi
tar -xf $ENV_BASE/packages/python/Python-$VERSION.tgz -C $ENV_BASE/packages/python/
if [ -h "$ENV_BASE/packages/python/current" ] ; then
rm $ENV_BASE/packages/python/current
fi
ln -s $ENV_BASE/packages/python/Python-$VERSION $ENV_BASE/packages/python/current
}
function _py_build {
echo " * Building Python in $ENV_BASE/builds/python/$VERSION"
# compile python source to new directory
cd $ENV_BASE/packages/python/current
./configure --prefix="$ENV_BASE/builds/python/$VERSION" 1>/dev/null
make install 1>/dev/null
if [ -h "$ENV_BASE/builds/python/current" ] ; then
rm $ENV_BASE/builds/python/current
fi
if [ ! -d "$ENV_BASE/builds/python/$VERSION/bin" ] ; then
echo "Didn't install properly something went wrong"
exit 1
fi
ln -s $ENV_BASE/builds/python/$VERSION $ENV_BASE/builds/python/current
}
function _py_install_setuptools {
wget -nv https://bootstrap.pypa.io/ez_setup.py -O - | $ENV_BASE/builds/python/current/bin/python
}
function _py_install_pip {
# use setuptools to install pip
$ENV_BASE/builds/python/current/bin/easy_install -s $ENV_BASE/builds/python/current/bin/ -d $ENV_BASE/builds/python/current/lib/python2.7/site-packages/ pip
$ENV_BASE/builds/python/current/bin/pip -V
}
function _py_install_virtualenv {
# install virtualenv
$ENV_BASE/builds/python/current/bin/pip install virtualenv
$ENV_BASE/builds/python/current/bin/pip install virtualenvwrapper
export WORKON_HOME=~/.environments
mkdir -p $WORKON_HOME
echo "export WORKON_HOME=~/.environments" >> ~/.profile
. ~/.profile
}
function _py_unlink {
for bin in $PYTHON_BINS; do
if [ -h "$HOME/bin/$bin" ] ; then
echo "Unlinked $HOME/bin/$bin"
rm "$HOME/bin/$bin"
fi
done
}
function _py_test {
PYVER=$(python -c 'import sys; print(".".join(map(str, sys.version_info[:3])))')
echo "Running Python from `which python` version "$PYVER
}
function _main {
_func_checkroot
_sys_installreq
_sys_prep
_py_download
_py_build
_py_unlink
_sys_link $PYTHON_BINS
_py_test
read -p "Install setuptools and pip? " -n 1 -r
echo # (optional) move to a new line
if [[ ! $REPLY =~ ^[Yy]$ ]]
then
_py_install_setuptools
_py_install_pip
_sys_link $PYTHON_BINS_EXTRAS
read -p "Install virtualenv and virtualenvwrapper? " -n 1 -r
echo # (optional) move to a new line
if [[ ! $REPLY =~ ^[Yy]$ ]]
then
_py_install_virtualenv
fi
fi
}
_main
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment