Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Installing Python 3.7.0 on Raspbian

Installing Python 3.7.0 on Raspbian

As of July 2018, Raspbian does not yet include the latest Python release, Python 3.7.0 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.0 When downloading the source code, select the most recent release of Python 3.7.0, available on the official site. Adjust the file names accordingly.

    wget https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tar.xz
    tar xf Python-3.7.0.tar.xz
    cd Python-3.7.0
    ./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.0
    rm Python-3.7.0.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, use the python setup by calling 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.0/Python-3.7.0.tar.xz
tar xf Python-3.7.0.tar.xz
cd Python-3.7.0
./configure
make -j 4
sudo make altinstall
cd ..
sudo rm -r Python-3.7.0
rm Python-3.7.0.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
@mpassosbr

This comment has been minimized.

Copy link

mpassosbr commented Jul 31, 2018

Thanks a lot!

@heyalexchoi

This comment has been minimized.

Copy link

heyalexchoi commented Aug 9, 2018

@SeppPenner are you able to use python 3.7's pip?

pi@raspberrypi:~/homebot $ python3.7 -m pip
/usr/local/bin/python3.7: No module named pip
pi@raspberrypi:~/homebot $ pip3.7
-bash: pip3.7: command not found
pi@raspberrypi:~/homebot $ pip3 --version
pip 9.0.1 from /usr/lib/python3/dist-packages (python 3.5)
@PhillipNewman

This comment has been minimized.

Copy link

PhillipNewman commented Aug 14, 2018

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.

@dalymople

This comment has been minimized.

Copy link

dalymople commented Sep 18, 2018

I am having an error with this script and SSL support.
The end result is that the _sll module is not built and pip fails to download anything because of this...

After a bit of digging I have found that after calling ./configure;

checking whether compiling and linking against OpenSSL works... yes
checking for X509_VERIFY_PARAM_set1_host in libssl... no
checking for --with-ssl-default-suites... python

This leads to the following build error;

Could not build the ssl module!
Python requires an OpenSSL 1.0.2 or 1.1 compatible libssl with X509_VERIFY_PARAM_set1_host().
LibreSSL 2.6.4 and earlier do not provide the necessary APIs, https://github.com/libressl-portable/portable/issues/381

So it looks like the libssl-dev dependency may not be correct?

@MEHUL95

This comment has been minimized.

Copy link

MEHUL95 commented Oct 6, 2018

I am getting error while executing the command:
pi@raspberrypi:/etc/apt/sources.list.d/Python-3.7.0 $ make -j 4

error:
gcc -pthread -c -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -std=c99 -Wextra -Wno-unused-result -Wno-unused-parameter -Wno-missing-field-initializers -Werror=implicit-function-declaration -I. -I./Include -DPy_BUILD_CORE -o Python/Python-ast.o Python/Python-ast.c
gcc -pthread -c -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -std=c99 -Wextra -Wno-unused-result -Wno-unused-parameter -Wno-missing-field-initializers -Werror=implicit-function-declaration -I. -I./Include -DPy_BUILD_CORE -o Python/asdl.o Python/asdl.c
gcc -pthread -c -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -std=c99 -Wextra -Wno-unused-result -Wno-unused-parameter -Wno-missing-field-initializers -Werror=implicit-function-declaration -I. -I./Include -DPy_BUILD_CORE -o Python/ast.o Python/ast.c
gcc -pthread -c -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -std=c99 -Wextra -Wno-unused-result -Wno-unused-parameter -Wno-missing-field-initializers -Werror=implicit-function-declaration -I. -I./Include -DPy_BUILD_CORE -o Python/ast_opt.o Python/ast_opt.c
Assembler messages:
Fatal error: can't create Python/asdl.o: Permission denied
Makefile:1623: recipe for target 'Python/asdl.o' failed
make: *** [Python/asdl.o] Error 1
make: *** Waiting for unfinished jobs....
Assembler messages:
Fatal error: can't create Python/ast_opt.o: Permission denied
Makefile:1623: recipe for target 'Python/ast_opt.o' failed
make: *** [Python/ast_opt.o] Error 1
Assembler messages:
Fatal error: can't create Python/ast.o: Permission denied
Makefile:1623: recipe for target 'Python/ast.o' failed
make: *** [Python/ast.o] Error 1
Assembler messages:
Fatal error: can't create Python/Python-ast.o: Permission denied
Makefile:1623: recipe for target 'Python/Python-ast.o' failed
make: *** [Python/Python-ast.o] Error 1

I am novice to raspbian platform please help.

@BombshellBill

This comment has been minimized.

Copy link

BombshellBill commented Oct 11, 2018

just writing to say thanks for putting this together

@drinu276

This comment has been minimized.

Copy link

drinu276 commented Oct 26, 2018

Thanks for this, running the setup.sh script worked great! No issues.

@skyice78

This comment has been minimized.

Copy link

skyice78 commented Dec 3, 2018

Why use altinstall?

@peerv

This comment has been minimized.

Copy link

peerv commented Dec 20, 2018

Thanks, did run the script and after 30 minutes all was up and running python 3.7

@yackx

This comment has been minimized.

Copy link

yackx commented Jan 4, 2019

@skyice78 see here about altinstall

@ianschafer

This comment has been minimized.

Copy link

ianschafer commented Jan 8, 2019

Installing Python-3.7.2 worked for me on RaspberryPi 3B+ running Raspbian.
I ran the ./configure a second time for stable optimizations (It gives you the choice).
The line: make -j 4 took very long at 2hr56min.
The line: sudo make altinstall took just a few minutes.
In the end ... success! Thanks!

@jagrit007

This comment has been minimized.

Copy link

jagrit007 commented Jan 19, 2019

Ok so Everything ran just fine. altinstall worked just fine but after all that when I checked my python version ("python3 --version") , It was still the older one i.e 3.5.2
So my question here is, Do I have to remove previous python installation that came by default?
Or it there something else I can do?

@marked23

This comment has been minimized.

Copy link

marked23 commented Jan 20, 2019

@ianschafer I looked up -j 4 and also -l 4 (that's a lower case L). GNU make
-j (aka. --jobs) takes an integer to specify how many recipes to run in parallel. (or empty to mean, max-it-out.)
-l (aka. --max-load) takes a float to specify the load level where it should hold-back adding more parallel recipes.
Used together, as in "make --jobs --max-load 4", seems like it should dynamically regulate parallel make jobs to keep the average load around X. (I'm not sure what "load" means.)

That being said... your 2hr 56min beat my time of 4+ hours. So I probably need to experiment with the "4" value that I used yesterday.

@marked23

This comment has been minimized.

Copy link

marked23 commented Jan 21, 2019

@jagrit007 I have a similar question, but I've discovered most of the answer.

For this I'm reasonably confident:
Raspbian depends on 2.7 and 3.5, as part of the OS dependencies. You don't want to change them, or re-purpose the symbolic links that reference them. Installing 3.7 means that you will have a third version of Python on your machine. This is the best-practice.

Can someone validate what I'm saying here?
I want to install Python 3.7, for use by any/all users, without making them customize their .bashrc, or install to /home/<user>/.local/.

I'm using this configure:: ./configure --enable-optimizations --prefix=/usr/local/

I want it to work in such a way that any user can shebang python 3.7: #!/usr/bin/env python3.7
and confirm their python version: $ /usr/bin/env python3.7 -V
and run python 3.7 interactively without specifying the full path to env: $ python3.7 -V

I think merely setting that --prefix=/usr/local/ does the deed. Am I missing some other necessity?

@floflo2607

This comment has been minimized.

Copy link

floflo2607 commented Jan 27, 2019

1 line
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.0/Python-3.7.0.tar.xz && tar xf Python-3.7.0.tar.xz && cd Python-3.7.0 && ./configure && make -j 4 && sudo make altinstall && cd .. && sudo rm -r Python-3.7.0 && rm Python-3.7.0.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

@kidig

This comment has been minimized.

Copy link

kidig commented Feb 25, 2019

thank you!

@vinceskahan

This comment has been minimized.

Copy link

vinceskahan commented Mar 10, 2019

yes, @floflo2607's instructions worked fine on a pi3b+ (I skipped the cleanup half after 'make altinstall'). Thanks here too !

@wordlesswind

This comment has been minimized.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.