Skip to content

Instantly share code, notes, and snippets.

@dennislwy
Forked from vbe0201/raspberry_python.sh
Last active December 4, 2023 01:32
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save dennislwy/a91e83826d6d9c111c995765f3a6779c to your computer and use it in GitHub Desktop.
Save dennislwy/a91e83826d6d9c111c995765f3a6779c to your computer and use it in GitHub Desktop.
A shell script for installing Python 3.x.x on your Raspberry Pi.
#!/bin/bash
# A bash script for installing Python 3.x.x on your Raspberry Pi.
# (c) 2023 Dennis Lee
#
# Open your terminal and type the following command:
# wget https://gist.githubusercontent.com/dennislwy/a91e83826d6d9c111c995765f3a6779c/raw/install_raspberry_python3.sh -O install_raspberry_python3.sh && chmod +x install_raspberry_python3.sh && ./install_raspberry_python3.sh
#set -e # Exit immediately if a command exits with a non-zero status.
# Prompt user for Python version and build
read -p "Enter the Python version (e.g., 3.10): " PYTHON_VERSION
read -p "Enter the Python build number (e.g., 11): " PYTHON_BUILD
function error {
echo -e "\e[31mError: $1\e[0m"
exit $2
}
# Prompt user to proceed with the installation
read -p "Do you wish to proceed with the installation of Python $PYTHON_VERSION.$PYTHON_BUILD? (y/N): " choice
if [[ ! "$choice" =~ ^[Yy]$ ]]; then
echo "Aborted by user"
exit 0
fi
# to measure upgrade elapsed time
start_time=$(date +%s)
# Update the system
read -p "Do you wish to update & upgrade the system? (Y/n): " choice
if [[ ! "$choice" =~ ^[Nn]$ ]]; then
echo -e "\e[32mUpdating the system\e[0m"
sudo apt-get update -y --allow-unauthenticated
echo -e "\e[32mUpgrading the system\e[0m"
sudo apt-get upgrade -y --allow-unauthenticated
echo -e "\e[32mDist upgrading the system\e[0m"
sudo apt-get dist-upgrade -y --allow-unauthenticated
fi
# Install necessary libraries and dependencies
echo -e "\e[32mInstall necessary libraries and dependencies\e[0m"
sudo apt-get install build-essential libncurses5-dev libncursesw5-dev libreadline6-dev libdb5.3-dev libgdbm-dev libc6-dev libbz2-dev libexpat1-dev liblzma-dev zlib1g-dev libsqlite3-dev tk-dev libssl-dev openssl libffi-dev -y
if [ $? -ne 0 ]; then error "Failed to install necessary libraries and dependencies." $?; fi
# Create a directory for Python installation
mkdir Python-Installation
cd Python-Installation
# Download Python binaries
echo -e "\e[32mDownloading Python $PYTHON_VERSION.$PYTHON_BUILD binaries\e[0m"
wget https://www.python.org/ftp/python/$PYTHON_VERSION.$PYTHON_BUILD/Python-$PYTHON_VERSION.$PYTHON_BUILD.tgz
if [ $? -ne 0 ]; then error "Failed to download Python $PYTHON_VERSION.$PYTHON_BUILD binaries" $?; fi
# Extracting Python binaries
echo -e "\e[32mExtracting Python binaries\e[0m"
tar xzvf Python-$PYTHON_VERSION.$PYTHON_BUILD.tgz
rm -f Python-$PYTHON_VERSION.$PYTHON_BUILD.tgz
# Configure, compile, and install Python
cd Python-$PYTHON_VERSION.$PYTHON_BUILD
echo -e "\e[32mConfiguring Python $PYTHON_VERSION.$PYTHON_BUILD\e[0m"
sudo ./configure --enable-optimizations
if [ $? -ne 0 ]; then error "Failed to configure Python $PYTHON_VERSION.$PYTHON_BUILD" $?; fi
echo -e "\e[32mCompiling Python $PYTHON_VERSION.$PYTHON_BUILD\e[0m"
sudo make -j 4
if [ $? -ne 0 ]; then error "Failed to compile Python $PYTHON_VERSION.$PYTHON_BUILD" $?; fi
echo -e "\e[32mInstalling Python $PYTHON_VERSION.$PYTHON_BUILD\e[0m"
sudo make altinstall
if [ $? -ne 0 ]; then error "Failed to install Python $PYTHON_VERSION.$PYTHON_BUILD" $?; fi
# Remove the Python installation directory
cd ../..
sudo rm -rf Python-Installation
# Remove unnecessary libraries and dependencies
echo -e "\e[32mRemove unnecessary libraries and dependencies\e[0m"
sudo apt-get --purge remove build-essential libncurses5-dev libncursesw5-dev libreadline6-dev libdb5.3-dev libgdbm-dev libc6-dev libz2-dev libexpat1-dev liblzma-dev zlib1g-dev libsqlite3-dev tk-dev libssl-dev openssl libffi-dev -y
sudo apt-get autoremove -y
sudo apt-get clean
# Set symbolic links for Python
echo -e "\e[32mSetting up symbolic link for Python\e[0m"
read -p "Do you wish to create symbolic link /usr/bin/python -> /usr/local/bin/python$PYTHON_VERSION? (Y/n): " choice
if [[ ! "$choice" =~ ^[Nn]$ ]]; then
sudo update-alternatives --install /usr/bin/python python /usr/local/bin/python$PYTHON_VERSION 1
fi
read -p "Do you wish to create symbolic link /usr/bin/python3 -> /usr/local/bin/python$PYTHON_VERSION? (Y/n): " choice
if [[ ! "$choice" =~ ^[Nn]$ ]]; then
sudo update-alternatives --install /usr/bin/python3 python /usr/local/bin/python$PYTHON_VERSION 1
fi
# Install or upgrade pip for Python
echo -e "\e[32mInstall or upgrade pip for Python\e[0m"
sudo python3 -m pip install --upgrade pip
if [ $? -ne 0 ]; then error "Failed to install or upgrade pip" $?; fi
# Set symbolic links pip
echo -e "\e[32mSetting up symbolic link for pip\e[0m"
read -p "Do you wish to create symbolic link /usr/bin/pip -> /usr/local/bin/pip$PYTHON_VERSION? (Y/n): " choice
if [[ ! "$choice" =~ ^[Nn]$ ]]; then
sudo update-alternatives --install /usr/bin/pip pip /usr/local/bin/pip$PYTHON_VERSION 1
fi
read -p "Do you wish to create symbolic link /usr/bin/pip3 -> /usr/local/bin/pip$PYTHON_VERSION? (Y/n): " choice
if [[ ! "$choice" =~ ^[Nn]$ ]]; then
sudo update-alternatives --install /usr/bin/pip3 pip /usr/local/bin/pip$PYTHON_VERSION 1
fi
end_time=$(date +%s)
elapsed_time=$((end_time - start_time))
echo -e "\e[32mElapsed time: $(humanize_time $elapsed_time)\e[0m"
echo -e "\e[32mPython $PYTHON_VERSION.$PYTHON_BUILD installed\e[0m"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment