Skip to content

Instantly share code, notes, and snippets.

@SeppPenner
Last active May 9, 2024 21:52
Show Gist options
  • Save SeppPenner/6a5a30ebc8f79936fa136c524417761d to your computer and use it in GitHub Desktop.
Save SeppPenner/6a5a30ebc8f79936fa136c524417761d to your computer and use it in GitHub Desktop.
Installing Python 3.7.4 on Raspbian

Installing Python 3.7.4 on Raspbian

As of July 2018, Raspbian does not yet include the latest Python release, Python 3.7.4. This means we will have to build it ourselves, and here is how to do it.

  1. Install the required build-tools (some might already be installed on your system).

    sudo apt-get update -y
    sudo apt-get install build-essential tk-dev libncurses5-dev libncursesw5-dev libreadline6-dev libdb5.3-dev libgdbm-dev libsqlite3-dev libssl-dev libbz2-dev libexpat1-dev liblzma-dev zlib1g-dev libffi-dev -y

    If one of the packages cannot be found, try a newer version number (e.g. libdb5.4-dev instead of libdb5.3-dev).

  2. Download and install Python 3.7.4. When downloading the source code, select the most recent release of Python, available on the official site. Adjust the file names accordingly.

    wget https://www.python.org/ftp/python/3.7.4/Python-3.7.4.tar.xz
    tar xf Python-3.7.4.tar.xz
    cd Python-3.7.4
    ./configure
    make -j 4
    sudo make altinstall
  3. Optionally: Delete the source code and uninstall the previously installed packages. When uninstalling the packages, make sure you only remove those that were not previously installed on your system. Also, remember to adjust version numbers if necesarry.

    sudo rm -r Python-3.7.4
    rm Python-3.7.4.tar.xz
    sudo apt-get --purge remove build-essential tk-dev -y
    sudo apt-get --purge remove libncurses5-dev libncursesw5-dev libreadline6-dev -y
    sudo apt-get --purge remove libdb5.3-dev libgdbm-dev libsqlite3-dev libssl-dev -y
    sudo apt-get --purge remove libbz2-dev libexpat1-dev liblzma-dev zlib1g-dev libffi-dev -y
    sudo apt-get autoremove -y
    sudo apt-get clean

or simply copy the setup.sh content to a file called setup.sh, do a chmod +x setup.sh and execute the script via sudo ./setup.sh

Afterwards, execute any of your scripts (yourscript.py is just a placeholder) using

python3.7 yourscript.py.

This guide is pretty much taken from the following tutorial: https://liudr.wordpress.com/2016/02/04/install-python-on-raspberry-pi-or-debian/ and https://gist.github.com/BMeu/af107b1f3d7cf1a2507c9c6429367a3b

sudo apt-get update -y
sudo apt-get install build-essential tk-dev libncurses5-dev libncursesw5-dev libreadline6-dev libdb5.3-dev libgdbm-dev libsqlite3-dev libssl-dev libbz2-dev libexpat1-dev liblzma-dev zlib1g-dev libffi-dev -y
wget https://www.python.org/ftp/python/3.7.4/Python-3.7.4.tar.xz
tar xf Python-3.7.4.tar.xz
cd Python-3.7.4
./configure
make -j 4
sudo make altinstall
cd ..
sudo rm -r Python-3.4.0
rm Python-3.7.4.tar.xz
sudo apt-get --purge remove build-essential tk-dev libncurses5-dev libncursesw5-dev libreadline6-dev libdb5.3-dev libgdbm-dev libsqlite3-dev libssl-dev libbz2-dev libexpat1-dev liblzma-dev zlib1g-dev libffi-dev -y
sudo apt-get autoremove -y
sudo apt-get clean
@SeppPenner
Copy link
Author

@oholimoli Which error do you get?

@oholimoli
Copy link

oholimoli commented Oct 6, 2019

I have this problem here: https://raspberrypi.stackexchange.com/questions/99621/python3-7-with-numpy-on-raspbian

How can I update glibc to 2.27?

@SeppPenner
Copy link
Author

@oholimoli. I've seen that you found a solution. I guess, doing a sudo apt-get install glibc should help as well?

@darkarun
Copy link

When I input python3.7 yourscript.py, I get "python3.7 can't open file 'yourscript.py': [Errno 2] No such file or directory

@imaginaryeast change the name yourscript.py to match the name of your script you are trying to run.

Sorry i don't get it... what do you mean with the match script name to be run?

@SeppPenner
Copy link
Author

SeppPenner commented Oct 10, 2019

@darkarun What I was trying to say: yourscript.py is just a placeholder for any Python script you have and want to run. I have updated the gist to make it clear from the first view.

@oholimoli
Copy link

@oholimoli. I've seen that you found a solution. I guess, doing a sudo apt-get install glibc should help as well?

glibc 2.27 seems to be not available. I use raspbian stretch lite.

@SeppPenner
Copy link
Author

The problem is that I can't really test this. I'm on Raspian Buster already...

@cjcbusatto
Copy link

Thanks for the gist! It worked without problems!

@firschau
Copy link

Thank you very much, was really helpful!

@josemi234
Copy link

josemi234 commented Mar 25, 2022

Hi,
Follow the entire procedure but when its ready seems that it always look for the directory where I put the files after download them.

xxx@raspberrypi:~ $ python 3.7 --version
python: can't open file '/home/xxx/3.7': [Errno 2] No such file or directory

It has created the folder in /usr/local/lib/python3.7 after installation

Thanks a lot!

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