Skip to content

Instantly share code, notes, and snippets.

@danielmacuare
Last active July 5, 2021 05:38
Show Gist options
  • Save danielmacuare/9b916540158040701aebaaf994bf88e7 to your computer and use it in GitHub Desktop.
Save danielmacuare/9b916540158040701aebaaf994bf88e7 to your computer and use it in GitHub Desktop.
To Install ANY python version on Linux boxes

Python - Building from source (Updated Dec 2020)

The following commands will help you to install Python 3.9.1 on a Centos 7 or Ubuntu 18.04 machine.

1 - Select the Python version you'll install.

https://www.python.org/ftp/python/ - We'll select the latest version as of Dec 2020 (3.9.1)

2 - Installing Python.

On Centos 7

sudo yum install gcc openssl-devel bzip2-devel libffi-devel
cd /usr/src
sudo wget https://www.python.org/ftp/python/3.9.1/Python-3.9.1.tgz
sudo tar zxf Python-3.9.1.tgz 
cd Python-3.9.1
sudo ./configure --enable-optimizations
sudo make altinstall

On Ubuntu

sudo apt-get install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev wget libsqlite3-dev
cd /usr/src
sudo wget https://www.python.org/ftp/python/3.9.1/Python-3.9.1.tgz
sudo tar zxf Python-3.9.1.tgz 
cd Python-3.9.1
sudo ./configure --enable-optimizations
sudo make altinstall

3 - Remove unnecessary source

rm /usr/src/Python-3.9.1.tgz

4 - Optional

touch ~/.bash_aliases
echo "alias python3='/usr/local/bin/python3.9'" >> ~/.bash_aliases
source ~/.bashrc
python3

5 - Shell script to install any Python version defined by $PYTHON_VERSION (Ubuntu)

#!/bin/bash
normal="\033[0m"

# Go here to check the list of available versions --> https://www.python.org/ftp/python/ and chage $PYTHON_VERSION to suit your needs.
PYTHON_VERSION="3.9.1"
PYTHON_PCKS=(
        build-essential
        zlib1g-dev
        libncurses5-devlibgdbm-dev
        libnss3-dev
        libssl-dev
        libreadline-dev
        libffi-dev
        wget
)

# =================================================================== # Functions # ===================================================================
INST_PACK(){
for PCKGS in "$@"; do
        printf "$green_bold[INSTALLING]$normal - Packet: $red_bold$PCKGS$normal\n"
        sudo apt-get -qqy --no-install-recommends install $PCKGS > /dev/null 2>&1
done
}

printf "$normal\n\n############################################################################$normal\n"
printf "$normal\t\t[BUILDING] PYTHON $PYTHON_VERSION FROM SOURCE $normal\n"
printf "$normal############################################################################$normal\n"

INST_PACK "${PYTHON_PCKS[@]}"
printf "\n"

cd /usr/src
printf "$green_bold[DOWNLOADING]$normal - Downloading:$red_bold Python $PYTHON_VERSION$normal"
sudo wget -q -nv https://www.python.org/ftp/python/$PYTHON_VERSION/Python-$PYTHON_VERSION.tgz

printf "$green_bold[EXTRACTING]$normal  - Extracting:$red_bold Python $PYTHON_VERSION source at: /usr/src$normal"
sudo tar zxf Python-$PYTHON_VERSION.tgz > /dev/null 2>&1

cd Python-$PYTHON_VERSION
printf "$green_bold[BUILDING]\t$normal   - Building:$red_bold Python $PYTHON_VERSION (This will take some minutes)$normal"
sudo ./configure --enable-optimizations > /dev/null 2>&1
sudo make altinstall > /dev/null 2>&1

printf "$green_bold[CLEANUP]\t$normal   - Cleaning:$red_bold Python $PYTHON_VERSION source files$normal"
sudo rm /usr/src/Python-$PYTHON_VERSION.tgz > /dev/null 2>&1
@hbibel
Copy link

hbibel commented Jan 8, 2021

Thank you for that gist, really helpful!

I suggest that you also add libsqlite3-dev to the apt packages to be installed, otherwise the sqlite3 API is missing.

@danielmacuare
Copy link
Author

I'm glad you found it useful and thanks for the tip. I've added libsqlite3-dev to the list of packages.

@Sanjay007
Copy link

I am continiuosly getting an error once upgraded to rocky linux . I have folllowed the below tutorial earlier .Any one facing the same ?

Here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment