Skip to content

Instantly share code, notes, and snippets.

@SerhatTeker
Last active July 28, 2022 18:32
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save SerhatTeker/7d0fc99d27e9bf1d75b4435a38a89fe9 to your computer and use it in GitHub Desktop.
Save SerhatTeker/7d0fc99d27e9bf1d75b4435a38a89fe9 to your computer and use it in GitHub Desktop.
Install Python (with desired version) on an Ubuntu Host
#!/usr/bin/env bash
# Bash safeties: exit on error, no unset variables, pipelines can't hide errors
set -o errexit
set -o nounset
set -o pipefail
# INFO
# --------------------------------------------------------------------------------------
# A shell script that downloads and installs Python on an Ubuntu host
# Default $PYTHON_VERSION to install is 3.9.6
#
# author : Serhat Teker <serhat.teker@gmail.com>
# version : 0.1.3
# last updated : 2021/10/21
# --------------------------------------------------------------------------------------
#
#
# LICENSE
# --------------------------------------------------------------------------------------
# BSD 3-Clause License
#
# Copyright (c) 2021, Serhat Teker
# All rights reserved.
# Redistribution and use in source and binary forms, with or without
# modification, are permitted provided that the following conditions are met:
#
# 1. Redistributions of source code must retain the above copyright notice, this
# list of conditions and the following disclaimer.
#
# 2. Redistributions in binary form must reproduce the above copyright notice,
# this list of conditions and the following disclaimer in the documentation
# and/or other materials provided with the distribution.
#
# 3. Neither the name of the copyright holder nor the names of its
# contributors may be used to endorse or promote products derived from
# this software without specific prior written permission.
#
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
# DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
# FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
# SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
# CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
# OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
# --------------------------------------------------------------------------------------
#
#
# USAGE
# --------------------------------------------------------------------------------------
# 1) - Define $INSTALL_PYTHON_VERSION variable on your environment
#
## Python Version to Install
## --------------------------------------------------------------------------------------
## Default version to install is 3.9.6
## If you want to change the version
## Define $INSTALL_PYTHON_VERSION variable on your environment
## Or change below accordingly
## PYTHON_VERSION="3.X.X"
PYTHON_VERSION="${INSTALL_PYTHON_VERSION:-3.9.6}"
#
# 2) - Run the script:
# $ wget -O - https://gist.githubusercontent.com/SerhatTeker/7d0fc99d27e9bf1d75b4435a38a89fe9/raw/install-python | bash
# Or run with shortened url:
# $ wget -O - https://git.io/JLQFl | bash
# --------------------------------------------------------------------------------------
# Directory to download and install python
DIR=/tmp
# FUNCTIONS
# --------------------------------------------------------------------------------------
# Before installing python, you must first install some required packages that are
# needed to build python from source. Maybe you have already installed them.
# However to get these packages installed, run the commands below:
install-packages() {
sudo apt update
sudo apt install \
build-essential \
zlib1g-dev \
libncurses5-dev \
libgdbm-dev \
libnss3-dev \
libssl-dev \
libreadline-dev \
libffi-dev \
wget
}
# Now create a temporary directory and download the python source code:
download-python() {
mkdir -p ${DIR}
cd ${DIR}
wget "https://www.python.org/ftp/python/${PYTHON_VERSION}/Python-${PYTHON_VERSION}.tgz"
}
# After downloading the package, run the commands below to extract the file and
# configure the python:
configure-python() {
tar -xvzf "${DIR}/Python-${PYTHON_VERSION}.tgz"
cd "${DIR}/Python-${PYTHON_VERSION}"
./configure
}
# To install the python:
altinstall() {
# Do not use the standard make install as it will overwrite the system’s default
# python3 binary —/usr/bin/python3. Since it will probably make a mess of
# your OS. However it is your choice.
cd "${DIR}/Python-${PYTHON_VERSION}"
sudo make altinstall
}
# Remove downdloads and extractions
clean() {
sudo rm -rf "${DIR}/Python-${PYTHON_VERSION}"
sudo rm "${DIR}/Python-${PYTHON_VERSION}.tgz"
}
main() {
install-packages
download-python
configure-python
altinstall
clean
}
main
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment