Skip to content

Instantly share code, notes, and snippets.

@dennislwy
Last active April 27, 2023 10:03
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/318ac12b0b9fa0f4aa03e784f34f86c7 to your computer and use it in GitHub Desktop.
Save dennislwy/318ac12b0b9fa0f4aa03e784f34f86c7 to your computer and use it in GitHub Desktop.
A shell script for installing Python 3.10.11 on your Raspberry Pi
#!/bin/bash
# A bash script for installing Python 3.x.x on your Raspberry Pi (modified from @realSnosh).
# (c) 2023 Dennis Lee
#
# Open your terminal and type the following command:
# wget https://gist.github.com/dennislwy/318ac12b0b9fa0f4aa03e784f34f86c7/raw/raspberry_install_python3.sh && chmod +x raspberry_install_python3.sh && ./raspberry_install_python3.sh
PYTHON_VERSION=3.10
PYTHON_BUILD=11
set -e # Exit immediately if a command exits with a non-zero status.
function error {
echo -e "\e[31mError: $1\e[0m"
exit $2
}
# Update the system
sudo apt-get update -y
sudo apt-get upgrade
sudo apt-get dist-upgrade
# Install neccessary libraries and dependencies
sudo apt-get install -y 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
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 and extract the Python binaries
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
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
sudo ./configure --enable-optimizations
if [ $? -ne 0 ]; then error "Failed to configure Python $PYTHON_VERSION.$PYTHON_BUILD" $?; fi
sudo make -j 4
if [ $? -ne 0 ]; then error "Failed to compile Python $PYTHON_VERSION.$PYTHON_BUILD" $?; fi
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
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
# Upgrade pip and install it for Python 3
python3 -m pip install --upgrade pip
sudo python3 -m pip install --upgrade pip
# Set symbolic links for Python
sudo rm /usr/bin/python
sudo ln -s /usr/local/bin/python$PYTHON_VERSION /usr/bin/python
sudo rm /usr/bin/python3
sudo ln -s /usr/local/bin/python$PYTHON_VERSION /usr/bin/python3
# Set symbolic links pip
sudo update-alternatives --install /usr/bin/pip pip /usr/local/bin/pip$PYTHON_VERSION 1
sudo update-alternatives --install /usr/bin/pip3 pip /usr/local/bin/pip$PYTHON_VERSION 1
sudo rm /usr/bin/pip
sudo ln -s /usr/local/bin/pip$PYTHON_VERSION /usr/bin/pip
sudo rm /usr/bin/pip3
sudo ln -s /usr/local/bin/pip$PYTHON_VERSION /usr/bin/pip3
echo -e "\e[97mPython $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