Skip to content

Instantly share code, notes, and snippets.

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

Installing Python 3.6 on Raspbian

As of January 2018, Raspbian does not yet include the latest Python release, Python 3.6. This means we will have to build it ourselves, and here is how to do it. There is also an ansible role attached that automates it all for you.

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

    $ sudo apt-get update
    $ 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

    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.6. When downloading the source code, select the most recent release of Python 3.6, available on the official site. Adjust the file names accordingly.

    $ wget https://www.python.org/ftp/python/3.6.5/Python-3.6.5.tar.xz
    $ tar xf Python-3.6.5.tar.xz
    $ cd Python-3.6.5
    $ ./configure
    $ make
    $ 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.6.5
    $ rm Python-3.6.5.tar.xz
    $ sudo apt-get --purge remove build-essential tk-dev
    $ sudo apt-get --purge remove libncurses5-dev libncursesw5-dev libreadline6-dev
    $ sudo apt-get --purge remove libdb5.3-dev libgdbm-dev libsqlite3-dev libssl-dev
    $ sudo apt-get --purge remove libbz2-dev libexpat1-dev liblzma-dev zlib1g-dev
    $ sudo apt-get autoremove
    $ sudo apt-get clean

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

# An ansible role to configure python3.6 on a Raspberry Pi.
# to get started, ensure you have ansible installed:
# $ sudo apt install ansible
# Then run this playbook:
# $ ansible-playbook -i localhost, python3.6.yml
# Then you can get started:
# $ python3.6
#
---
- hosts: all
vars:
version: 3.6.5
threads: 4
tasks:
- name: "python{{version}} runtime&build dependencies"
become: yes
apt: name={{item}}
with_items:
- 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
- name: "Download python{{version}}"
get_url:
url="https://www.python.org/ftp/python/{{version}}/Python-{{version}}.tar.xz"
dest="/tmp/Python-{{version}}.tar.xz"
- name: "Unarchive python{{version}}"
unarchive:
src="/tmp/Python-{{version}}.tar.xz"
dest="/tmp/"
copy=no
creates="/tmp/Python-{{version}}"
- name: "configure python{{version}} build"
command: ./configure
args:
chdir: "/tmp/Python-{{version}}"
creates: "/tmp/Python-{{version}}/Makefile"
- name: "build python{{version}}"
# not using make module to be able to use -j and creates option to fully skip step
command: make -j{{threads}}
args:
chdir: "/tmp/Python-{{version}}"
creates: "/tmp/Python-{{version}}/python"
- name: "install python{{version}}"
become: yes
make:
chdir: "/tmp/Python-{{version}}"
target: altinstall
@ghost

This comment has been minimized.

Copy link

ghost commented Mar 25, 2017

make: *** No rule to make target 'Programs/python.c', needed by 'Programs/python.o'. Stop.

@w33zel

This comment has been minimized.

Copy link

w33zel commented Apr 17, 2017

Hi,
I installed python 3.6.1 like in this quide, but how can I use it? When I type "python" I get python 2.7 or "python3" for python 3.4 but I have no python36 and I have no idea where it is intalled.

Is there a way to make python 3.6 the standart when I type "python"?

@nueverest

This comment has been minimized.

Copy link

nueverest commented Apr 23, 2017

@w33zel Use the command

python3.6

see this for more details as to why.

A quote to highlight from the link.

This is a point of interest. I would not mess with the symbolic link [to the python command]. Ubuntu runs python for many internal maintenance scripts and those scripts are expecting the python version that shipped with ubuntu.

@dadatuputi

This comment has been minimized.

Copy link

dadatuputi commented Jun 7, 2017

Just a heads up, your bullet 4 should be bullet 3. I forked this for 3.5 with altinstall, make -j 4, and ./configure --enable-optimizations: https://gist.github.com/elBradford/5e0c999b6d2ab8fcf5331c5ac177a486

@TomaasHenriques

This comment has been minimized.

Copy link

TomaasHenriques commented Jun 26, 2017

How can i install modules in that python?

@orcsriver

This comment has been minimized.

Copy link

orcsriver commented Jun 29, 2017

Thanks dschep for this guide.

But, one correction for the code to delete the downloaded source:

Replace:
$ rm Python-3.6.0.tgz

With:
$ rm Python-3.6.0.tar.xz

Regards

@kwood

This comment has been minimized.

Copy link

kwood commented Jul 7, 2017

If you're doing this on a Rpi 2 or 3 use make -j 4 to compile a lot faster... For whatever reason it only used one job by default on my pi.

You could also add export MAKEFLAGS="-j 4" to ~/.bash_profile for a longer lasting solution.

@TheMightyMat

This comment has been minimized.

Copy link

TheMightyMat commented Jul 12, 2017

@Rxon7 Same issue here - make: *** No rule to make target 'Programs/python.c', needed by 'Programs/python.o'. Stop. . Anyone got any solutions to this?

@Nick-Archi

This comment has been minimized.

Copy link

Nick-Archi commented Sep 18, 2017

Any idea on how to open the Python 3.6 idle? I cannot seem to find any documentation on this

@grabear

This comment has been minimized.

Copy link

grabear commented Sep 28, 2017

Thanks for the inspiration! I used it in my repository grabear/Raspbian-ShinyServerAndPython36.

@Michael-Huang-9001

This comment has been minimized.

Copy link

Michael-Huang-9001 commented Oct 9, 2017

Any idea on how to install modules?
I can't seem to install them and always get a "no matching distribution found".

@david-loran

This comment has been minimized.

Copy link

david-loran commented Nov 23, 2017

The ./configure step locks up my Raspberry Pi 2 model B. Only, "checking build system type..." is emitted on the console. Has anyone else seen this?
It's apparently getting hung up in config.guess. Probably I can just tell it which build system. Will keep working on it.
So, my problem was a bad SD card. The bad one had been working fine and only seemed to fail while running somewhere in config.guess.

@ekohilas

This comment has been minimized.

Copy link

ekohilas commented Dec 8, 2017

@Michael-Huang-9001, @TomaasHenriques
You can use
pip3.6 ...
or
python3.6 -m pip ...

@xslendix

This comment has been minimized.

Copy link

xslendix commented Feb 10, 2018

I crated a gist that automatically installs Python 3.6
It can be found here: https://gist.github.com/xslendix/fcb55ae06b49be557e3418cfa8af4534

@dschep

This comment has been minimized.

Copy link
Owner Author

dschep commented Mar 18, 2018

Whoa. sorry folks, github doesn't show notifications for comments on gists. I haven't used this in a while but have just gotten the need to do so again. pip3.6 is indeed probably the best way to install modules. tho virtualenvs are a good idea too:

$ python3.6 -m venv venv
$ . ./venv/bin/activate
$ pip install some-module

ALso, i'm configuring my pi with ansible, so I'll add an ansible role to do the setup once i've gotten that working

@dschep

This comment has been minimized.

Copy link
Owner Author

dschep commented Mar 18, 2018

And re: getting it as python this guide intentionally uses make altinstall to only install a python3.6 binary to avoid conflicting with any system(apt) provided python installations. I suggest using a virtual env as i mentioned above, when it's activated your python will be python 3.6 that you created the virtualenv with.

@SyntheticDream

This comment has been minimized.

Copy link

SyntheticDream commented Mar 26, 2018

Distributor ID:	Raspbian
Description:	Raspbian GNU/Linux 9.3 (stretch)
Release:	9.3
Codename:	stretch

Ran into python3.6: error while loading shared libraries: libpython3.6m.so.1.0: cannot open shared object file: No such file or directory while building python3.6.4 with --enable-shared. Fixed it as suggested here, by adding /usr/local/lib to /etc/ld.so.conf and running ldconfig.

Another possible fix, as suggested here, would be to modify configure task:

command: ./configure --enable-shared --prefix=/usr/local LDFLAGS="-Wl,--rpath=/usr/local/lib"

BTW, thanks a lot for ansible script, good job.

@ghost

This comment has been minimized.

Copy link

ghost commented Apr 3, 2018

Can we bump to update this to reflect the release of 3.6.5?

@dschep

This comment has been minimized.

Copy link
Owner Author

dschep commented Apr 4, 2018

@neko404notfound, done. But if using ansible, you can specify the specific version by adding -e version=3.6.5 when calling ansible-playbook

@iCrazyBlaze

This comment has been minimized.

Copy link

iCrazyBlaze commented May 9, 2018

Can you create a shell or Python script that does this instead of an ansible role?

@y-spreen

This comment has been minimized.

Copy link

y-spreen commented May 12, 2018

I checked the latest raspbian for packages that were already installed before I ran the python installation. It was a clean raspbian stretch, literally out of the box. I adjusted the deinstallation lines accordingly:

sudo apt-get --purge remove tk-dev
sudo apt-get --purge remove libncurses5-dev libncursesw5-dev
sudo apt-get --purge remove libdb5.3-dev libgdbm-dev libsqlite3-dev libssl-dev
sudo apt-get --purge remove libbz2-dev liblzma-dev
@MartenKL

This comment has been minimized.

Copy link

MartenKL commented May 26, 2018

@Nick-Archi
How to add Idle3.6.5 to Stretch
cd /usr/share/raspi-ui-overrides/applications/
sudo cp idle3.desktop idle3.6.desktop
sudo nano idle3.6.desktop
change contents to:
[Desktop Entry]
Name=Python 3.6 (IDLE)
Comment=Integrated development environment for Python 3.6.5
TryExec=/usr/local/bin/idle3.6
Exec=/usr/local/bin/idle3.6
Icon=python
Terminal=false
Type=Application
Categories=Application;Development;
StartupNotify=true
ctrl-x
y
[ENTER]

@DennisNickname

This comment has been minimized.

Copy link

DennisNickname commented Jun 12, 2018

Hi,

I am using an RPi3 with fresh Raspbian Stretch 04/2018. After updating and upgrading I installed Ansible 2.2.1.0.
When i role out the ansible playbook like described i get the following:

ERROR! Syntax Error while loading YAML.

The error appears to have been in '/home/pi/Python3.6.yml': line 57, column 1,but may be elsewhere in the file depending on the exact syntax problem.

The offending line appears to be:

chdir: "/tmp/Python-{{version}}"
target: altinstall
^here

Anyone an idea what I am doing wrong?

Thanks a lot
Dennis

@rhyministermc

This comment has been minimized.

Copy link

rhyministermc commented Jun 14, 2018

Got it installed. Thanks a million!

@slinden2

This comment has been minimized.

Copy link

slinden2 commented Jun 26, 2018

Thanks! Seems to be working on my RPi 3 B+ (Raspbian GNU/Linux 9 (stretch)). I just installed Python 3.6.5.

@SeppPenner

This comment has been minimized.

Copy link

SeppPenner commented Jul 5, 2018

I have created a version for 3.6.0, 3.6.4 and 3.7.0

@G1psyDanger

This comment has been minimized.

Copy link

G1psyDanger commented Sep 3, 2018

This works extremely well. I wanted to know why the command line asks if I want stable optimizations though? I went ahead and ran the suggested command "./configure --enable-optimizations" what exactly does that do?

@PJaros

This comment has been minimized.

Copy link

PJaros commented Sep 12, 2018

I needed to add "libffi-dev" as a dependency of raspbian-python3.6.yml
I had to add it to be able to use "pip3.6 install ansible"

@061375

This comment has been minimized.

Copy link

061375 commented Oct 2, 2018

Thanks! Also, RPi 3 B+ (Raspbian GNU/Linux 9 (stretch))

@quique123

This comment has been minimized.

Copy link

quique123 commented Oct 27, 2018

Im getting a bunch of these on running sudo make:

gcc -pthread -Wno-unused-result -Wsign-compare -DNDEBUG -g -fwrapv -O3 -Wall -Wstrict-prototypes -std=c99 -Wextra -Wno-unused-result -Wno-unused-parameter -Wno-missing-field-initializers -I. -I./Include -DPy_BUILD_CORE -c ./Modules/itertoolsmodule.c -o Modules/itertoolsmodule.o

Is this normal?

@placidchat

This comment has been minimized.

Copy link

placidchat commented Nov 14, 2018

Are there tutorials for cross compiling python 3.6 to the raspberry pi arm from x86_64 ?

@NikhilKanda

This comment has been minimized.

Copy link

NikhilKanda commented Dec 26, 2018

I'm wondering to know if there's a way to cross compile Pytorch on Allwinner platform (AArch64 processor). Leads are greatly appreciated.

@sjkim04

This comment has been minimized.

Copy link

sjkim04 commented Jan 6, 2019

I have the error 'invalid desktop entry' on Raspbian Stretch(RPi 3B+), why?

@hhv

This comment has been minimized.

Copy link

hhv commented Feb 1, 2019

Hi,

the 'sudo make altinstall' failed on my Pi2 because libffi-dev was missing. After running the 'apt-get install' command with libffi-dev added, 'sudo make altinstall' completed OK. See: https://bugs.python.org/issue31652

@jmsaltzman

This comment has been minimized.

Copy link

jmsaltzman commented Mar 11, 2019

This worked for me-- I'm able to get 3.6.4 (my preferred version) by launching python3.6-- but I wonder if these have been discussed elsewhere:

  1. Is it safe to remake the links in /usr/bin so the various python3's run python3.6?
  2. I'm able to make a virtualenv to install packages for 3.6, but how can I get the RPi library? apt-get installs it for 3.5 into /usr/lib/python3, but python3.6 installs libraries into /usr/local/lib/python3.6. I don't suppose I could [gulp] copy files from 3.5 to 3.6?
@bastisawesome

This comment has been minimized.

Copy link

bastisawesome commented Jun 18, 2019

@jmsaltzman

  1. It's heavily discouraged as many under-the-hood scripts and applications rely on the specific version of Python that comes with Ubuntu. However, I have replaced the symlinks and removed older versions of Python in the past without any problems, so I'd suggest make a backup before doing anything, that way if something goes wrong you can revert.

  2. I haven't ever tried this before so I can't help with that one. But I don't think you'd risk breaking anything if you just copied the files over. Worst case scenario you get a Python crash.

@talderson1

This comment has been minimized.

Copy link

talderson1 commented Jun 19, 2019

I have seen a few guides for installing before. Mostly they all seem good, so thanks for taking the time to make these. They're very helpful to Linux noobs like me.
My question is about the existing installations of python. I have very limited space on the raspberry pi. How do I delete the old versions of Python? I have 2.7, 3.5, 3.7.3 I will probably keep 2.7 and 3.7 but how do I get rid of the 3.5? Also, is there a way to make 3.7 the default python on my system? Right now I think it's 2.7
Thanks so much.

@jmsaltzman

This comment has been minimized.

Copy link

jmsaltzman commented Jun 19, 2019

@bastisawesome thanks for the reply. I've since decided it doesn't matter for me-- happy to leave 2.7 where it is and to make sure my scripts run the Python I want them to. Using virtualenvs solves most of the issues I was having.

@talderson1, I wouldn't want to actually remove Python 2. Keeping the system small isn't a priority for me (need GB for images and other data) and anyway the sweet spot for SD card pricing seems to be going up and up: fast 16GB cards are <$10 these days.

@Twistere

This comment has been minimized.

Copy link

Twistere commented Jul 4, 2019

Hi i followed your tutorial but when i make python3 my raspbian says 3.5.3

@dschep

This comment has been minimized.

Copy link
Owner Author

dschep commented Jul 23, 2019

@Twistere does running python3.6 get you anything different? My guess is your pi already has python3 in some form, and this doesn't supercede that (bc of the use of make altinstall) but rather uses more specific, but non-conflicting names for the binaries.

@runninbear5

This comment has been minimized.

Copy link

runninbear5 commented Jul 28, 2019

@dschep I have followed your installation instructions and got python 3.7 installed successfully. I don't seem to be able to use pip3.7 though. It returns pip3.7: command not found. Is there something special that needs to be done to allow pip to use the latest version?

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.