Skip to content

Instantly share code, notes, and snippets.

@SerhatTeker

SerhatTeker/install-python

Last active Oct 16, 2020
Embed
What would you like to do?
Install Python (with desired version) on Ubuntu host
#!/bin/bash
set -eu
# INFO
# --------------------------------------------------------------------------------------
# A shell script that downloads and installs Python on Ubuntu host
# Default $PYTHON_VERSION is 3.9.0
#
# author : Serhat Teker <serhat.teker@gmail.com>
# version : 0.0.3
# Last updated : 2020/10/14
# --------------------------------------------------------------------------------------
#
#
# LICENSE
# --------------------------------------------------------------------------------------
# BSD 3-Clause License
#
# Copyright (c) 2020, 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
# --------------------------------------------------------------------------------------
# Run:
# wget -O - https://gist.githubusercontent.com/SerhatTeker/7d0fc99d27e9bf1d75b4435a38a89fe9/raw/install-python | bash
# --------------------------------------------------------------------------------------
# Python Version to Install
# --------------------------------------------------------------------------------------
# Default version to install is 3.9.0
# If you want to change the version
# Define $INSTALL_PYTHON_VERSION variable on your environment
# Or change below accordingly
# PYTHON_VERSION="3.9.0"
PYTHON_VERSION="${INSTALL_PYTHON_VERSION:-3.9.0}"
DIR=$HOME/tmp
# 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:
upgrade-system() {
sudo apt update
sudo apt upgrade
sudo apt dist-upgrade
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.
sudo make altinstall
}
clean() {
sudo rm -rf $DIR/Python-$PYTHON_VERSION
sudo rm $DIR/Python-$PYTHON_VERSION.tgz
}
main() {
upgrade-system
download-python
configure-python
altinstall
clean
}
make
exit 0
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.