Skip to content

Instantly share code, notes, and snippets.

@dschep
Forked from BMeu/raspbian-python3.5.rst
Last active September 2, 2024 16:58
Show Gist options
  • Save dschep/24aa61672a2092246eaca2824400d37f to your computer and use it in GitHub Desktop.
Save dschep/24aa61672a2092246eaca2824400d37f to your computer and use it in GitHub Desktop.
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
@jmsaltzman
Copy link

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
Copy link

@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
Copy link

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
Copy link

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

@user790235692
Copy link

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

@dschep
Copy link
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
Copy link

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

@appeacock
Copy link

appeacock commented Mar 7, 2020

Install pip3 using the python3 version you want:

$ wget https://bootstrap.pypa.io/get-pip.py
$ python3.6.x ./get-pip.py
$ pip3 --version
pip <version> from /home/pi/.local/lib/<python_version>/site-packages/pip (<python_version>)

@sarusukh1
Copy link

@aplawson I tried following your steps but got bash error:
$ wget https://bootstrap.pypa.io/get-pip.py
$ python3.6.x ./get-pip.py

bash: python3.6.5: command not found

@Zacharyprime
Copy link

Just a formatting comment, it would be nice if the dollar signs were removed from the instructions so people could copy and paste the block.

I can't tell if the raw just isn't showing me this, but if you do:
``` (3 of this ` character) and "shell" then another 3 of that character to close the block, it will format like at terminal.

#```shell
#code here
#```
echo like this
#comment

@gopinath-vijayakumaar
Copy link

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"?

https://raspberry-valley.azurewebsites.net/Python-Default-Version/ refer

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