Skip to content

Instantly share code, notes, and snippets.

@SeppPenner
Last active January 8, 2024 12:33
Star You must be signed in to star a gist
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
@Minecraftschurli
Copy link

Minecraftschurli commented Jun 4, 2019

this doesn't work for me
Using: Raspberry Pi 3B+ and freshly installed raspbian stretch
checking build system type... armv7l-unknown-linux-gnueabihf
checking host system type... armv7l-unknown-linux-gnueabihf
checking for python3.7... no
checking for python3... python3
checking for --enable-universalsdk... no
checking for --with-universal-archs... no
checking MACHDEP... checking for --without-gcc... no
checking for --with-icc... no
checking for gcc... gcc
checking whether the C compiler works... yes
checking for C compiler default output file name... a.out
checking for suffix of executables...
checking whether we are cross compiling... configure: error: in '/home/pi/Python-3.7.3':
configure: error: cannot run C compiled programs.
If you meant to cross compile, use '--host'.
See 'config.log' for more details

@SeppPenner
Copy link
Author

configure: error: cannot run C compiled programs.

Sorry, I have no idea here...

@Bluscream
Copy link

Bluscream commented Jun 5, 2019

blu@minopia:~/bots/discord/Discord-MessageLinkToQuoteBot$ python3.7 --version
Python 3.7.0
blu@minopia:~/bots/discord/Discord-MessageLinkToQuoteBot$ python3.7 -m pip
/usr/local/bin/python3.7: No module named pip
blu@minopia:/tmp$ python3.7 get-pip.py
Traceback (most recent call last):
  File "get-pip.py", line 21492, in <module>
    main()
  File "get-pip.py", line 197, in main
    bootstrap(tmpdir=tmpdir)
  File "get-pip.py", line 82, in bootstrap
    import pip._internal
  File "/tmp/tmpjg28ooc4/pip.zip/pip/_internal/__init__.py", line 40, in <module>
  File "/tmp/tmpjg28ooc4/pip.zip/pip/_internal/cli/autocompletion.py", line 8, in <module>
  File "/tmp/tmpjg28ooc4/pip.zip/pip/_internal/cli/main_parser.py", line 12, in <module>
  File "/tmp/tmpjg28ooc4/pip.zip/pip/_internal/commands/__init__.py", line 6, in <module>
  File "/tmp/tmpjg28ooc4/pip.zip/pip/_internal/commands/completion.py", line 6, in <module>
  File "/tmp/tmpjg28ooc4/pip.zip/pip/_internal/cli/base_command.py", line 20, in <module>
  File "/tmp/tmpjg28ooc4/pip.zip/pip/_internal/download.py", line 37, in <module>
  File "/tmp/tmpjg28ooc4/pip.zip/pip/_internal/utils/glibc.py", line 3, in <module>
  File "/usr/local/lib/python3.7/ctypes/__init__.py", line 7, in <module>
    from _ctypes import Union, Structure, Array
ModuleNotFoundError: No module named '_ctypes'

@imaginaryeast
Copy link

Looks like raspbian no longer has a libreadline6-dev, it automatically defaults to some generic libreadline-dev. I assume it's fine as it installs, but perhaps the documentation should be fixed?

@imaginaryeast
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

@developer-x1
Copy link

developer-x1 commented Jun 5, 2019

@PhillipNewman

How do you install the Python 3.7 IDLE and it's shortcut to the menu>Programming where I currently have:
Python 2 (IDLE)
Python 3 (IDLE)
Python 3.7 (IDLE) <-- this is what I want.

use alacarte to change idle's command. go to programming, find the checked idle3 and in properties change the command.
change it from /usr/bin/idle3 to /usr/local/bin/idle3.7

@Paitch
Copy link

Paitch commented Jun 11, 2019

This script works on Raspberry Pi Zero W, with about 1h30 installation time.
No problem at all.
Thanks for sharing!

@SeppPenner
Copy link
Author

This script works on Raspberry Pi Zero W, with about 1h30 installation time.

The installation time is really bad, I know...

@Paitch
Copy link

Paitch commented Jun 12, 2019

This script works on Raspberry Pi Zero W, with about 1h30 installation time.

The installation time is really bad, I know...

Don´t worry about that.. @SeppPenner! It´s not your fault with they didn´t put 2GB ram on Pi Zero..
At least your script works and make things really easy! Kudos!

@im-alexandre
Copy link

Thanks! Succeed for installing Python 3.7.3 on Raspberry Pi 3 Model B+.

For future readers, after that just use python3.7 and pip3.7, for example

python3.7 --verison
pip3.7 --verison

Man, you've just saved my life!!!!

@SeppPenner
Copy link
Author

Thanks! Succeed for installing Python 3.7.3 on Raspberry Pi 3 Model B+.
For future readers, after that just use python3.7 and pip3.7, for example

python3.7 --verison
pip3.7 --verison

Man, you've just saved my life!!!!

Just make sure to use --version, not --verison.

@guti7
Copy link

guti7 commented Jul 14, 2019

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.

@guti7
Copy link

guti7 commented Jul 14, 2019

@PhillipNewman

How do you install the Python 3.7 IDLE and it's shortcut to the menu>Programming where I currently have:
Python 2 (IDLE)
Python 3 (IDLE)
Python 3.7 (IDLE) <-- this is what I want.

use alacarte to change idle's command. go to programming, find the checked idle3 and in properties change the command.
change it from /usr/bin/idle3 to /usr/local/bin/idle3.7

@Piprogramer5 Thanks!

@runninbear5
Copy link

@SeppPenner I followed your link and the installation seemed to work great. However pip3.7 does not work for me, it is an unrecognized command. I downloaded the latest 3.7 version which is 3.7.4 from the python website. Any ideas why pip would not be working?

@SeppPenner
Copy link
Author

@runninbear5: Did you check your path variable? echo $PATH. Check if the pip command is in there, please. Maybe https://askubuntu.com/questions/1082136/how-can-i-use-pip3 or https://superuser.com/questions/769920/python3-pip-installed-but-pip3-command-not-found will help you there...

@MrMEScott
Copy link

I am getting the following curious dependency error when attempting to install python3-ws4py. It seems to be saying that it can't install ws4py because needs to be at a level no higher than the one it's trying to install. Which makes no sense to me. Can anyone help or suggest a solution to this?

FWIW this is on a Raspberry Pi 3B+ running Raspbian buster, fully up-to-date. Python3 is at 3.7.3. (I had installed aptitude in the hope it would do better at resolving the dependency than apt, but it identifies the same issue.)

pi@birdbox:~ $ sudo aptitude install python3-ws4py
The following NEW packages will be installed:
  python3-ws4py 
0 packages upgraded, 1 newly installed, 0 to remove and 92 not upgraded.
Need to get 39.7 kB of archives. After unpacking 170 kB will be used.
The following packages have unmet dependencies:
 python3.7 : Breaks: python3-ws4py (<= 0.4.2+dfsg1-2) but 0.4.2+dfsg1-2 is to be installed
The following actions will resolve these dependencies:

     Keep the following packages at their current version:
1)     python3-ws4py [Not Installed]                      

Accept this solution? [Y/n/q/?] q
Abandoning all efforts to resolve these dependencies.
Abort.

@SeppPenner
Copy link
Author

FWIW this is on a Raspberry Pi 3B+ running Raspbian buster, fully up-to-date.

I haven't tried to update this yet, is on my list. Strange thing though...

@MrMEScott
Copy link

I am getting the following curious dependency error when attempting to install python3-ws4py.

Still digging on this. There are definitely no held packages, but could the issue possibly be connected to the following?

pi@birdbox:~ $ sudo apt-get update
Get:1 http://raspbian.raspberrypi.org/raspbian buster InRelease [15.0 kB]
Hit:2 http://archive.raspberrypi.org/debian buster InRelease
Reading package lists... Done                            
E: Repository 'http://raspbian.raspberrypi.org/raspbian buster InRelease' changed its 'Suite' value from 'testing' to 'stable'
N: This must be accepted explicitly before updates for this repository can be applied. See apt-secure(8) manpage for details.

@MrMEScott
Copy link

could the issue possibly be connected to the following?

Nope. I ran "sudo apt-get update --allow-releaseinfo-change" and retried the install of python3-ws4py - still no dice. Exact same error from aptitude, and apt-get complains thus:

pi@birdbox:~ $ sudo apt-get install python3-ws4py
Reading package lists... Done
Building dependency tree       
Reading state information... Done
Some packages could not be installed. This may mean that you have
requested an impossible situation or if you are using the unstable
distribution that some required packages have not yet been created
or been moved out of Incoming.
The following information may help to resolve the situation:

The following packages have unmet dependencies:
 python3-ws4py : Depends: python3:any (>= 3.3.2-2~)
E: Unable to correct problems, you have held broken packages.

Now wondering if there's an error in the python3-ws4py package on the Raspbian repository. FWIW, I was able to install the package on a separate (Ubuntu) system, but that probably doesn't prove anything.

@oholimoli
Copy link

Thank you, I successfully installed python 3.7.4 :)

Now I try to install numpy with no luck.. :/
Was somebody luckier than me?

@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