Skip to content

Instantly share code, notes, and snippets.

@danielmacuare
Last active February 1, 2025 12:02
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

@aaronleigh18
Copy link

iQIzBAABCAAdFiEE4/8oOcBIslwITevpsmmV4xAlBWgFAmbcKdUACgkQsmmV4xAl
BWhI0Q/+MWwQirWSn5ZApMU4EmKrZ0x8fuAeMHam6Dg1FaATYxJxgkf8ep5TN7Xp
VQpNVa40PqXbAf93x4KSavv/QS464Whv1PX5FsBkJBwzPPR3xY8CAfisPXchrOQV
12OKmhXnT7nebqaax56SLJsnen8EZB35X7d8RzDVqdNu+Xl7ICOY7d3LkPW14SGk
/pHgF2YgfClQSAh4MLRR0EYhUVlzKENJkGnMTdU9VicOnjJ5LtkGbHhVYPAEqALf
r6hrn2gyorPF8gtY+uN8d2/UKh9Xih/tRPhM3GGbX6Q9EBNpivvNK7829jEAkw1P
NZKtAOfiQGJU4I1PQkiYDMcbtc/fhDaj96nA+X3LxKih7dzLCXVGwDLsBYN6PJp4
ogR+nL7d5ZcikGrzHHaxZjncj2nPrbk5+aNoIZWHq29RiuZ0c74GRl9ILYYijk/E
vXsiBtuzwbA2UCF+gRVrZA3a9G4dMkLvftmbDemnUw5xGTWNEEInKD5Sv3fDhvMx
vdRI72RglYy1VNrgcG5mcH7bviXFy+voExUUjvsHOGkqHvyK+tVV0T+EMfEtRp6k
rCVdswN0LFIkmerVfr6XXHFty1mlKRvX+hyeiRXIhdFZieDQHdVH+3rvaft31LcQ
S2Mv+EGDeUP9llrF80Slbw7XPFfclkOBtzOOKwcTN+2NlpvczOw=
=9/j+

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