Wednesday, January 25, 2017

Building Python 3.6 on Raspberry Pi 3 from sources (clean Debian)

Building Python 3.6 on Raspberry Pi 3 from sources (clean Debian)

 

1. Problem


By default clean Debian installation contains Python version 2.7.9 on my Debian (Raspbian GNU/Linux 8 (jessie)"). But we want more! We need Python 3.X, this tutorial shows you how to install Python version 3.6.0 .

Warning! Newer versions of Debian for Raspbery Pi have SSH disabled, so you need enable it by creating empty file with name ssh (Yes, just three letters. Yes, without extension) in /boot section of SD-card. This section also visible under Windows, so you will have no problems to do this.

Note: This is updated version of my previous tutorial Building Python 3.5 on Raspberry Pi 2 (Raspbian) which fixes issues with TLS/SSL support of compiled Python.



2. Info


You can check Python version in any moment using command
$ python --version
> Python 2.7.9
you can get you OS kernel version using command 
$ uname --all
> Linux raspberrypi 4.4.34-v7+ #930 SMP Wed Nov 23 15:20:41 GMT 2016 armv7l GNU/Linux

also, you can get your Debian version using:
$ cat /etc/os-release
> PRETTY_NAME="Raspbian GNU/Linux 8 (jessie)"
> NAME="Raspbian GNU/Linux"
> VERSION_ID="8"
> VERSION="8 (jessie)"
> ID=raspbian
> ID_LIKE=debian
> HOME_URL="http://www.raspbian.org/"
> SUPPORT_URL="http://www.raspbian.org/RaspbianForums"
> BUG_REPORT_URL="http://www.raspbian.org/RaspbianBugs"

you can get Debian version using lsb-release utility, to use it you need install it:
$ sudo apt-get install lsb-release

After this you can use it by:
$ lsb_release -a 
> No LSB modules are available.
> Distributor ID: Raspbian
> Description:    Raspbian GNU/Linux 8.0 (jessie)
> Release:        8.0
> Codename:       jessie


to get SD card free space you can run this:
$ df -H

this command will show you free space of your SD card in human readable format

3. Installation sequence

3.1 Updating system


$ sudo apt-get update
$ sudo apt-get -y upgrade
$ sudo apt-get dist-upgrade

Note: all these operations can take a lot of time. Please, be patient.

Also, you may expand your fs to all free space of SD card using raspi-config utility:
$ sudo raspi-config

Then in UI you need select Expand file system, then you will be notified that reboot required. When you will quit raspi-config you will be asked to perform reboot wright now.

If you want to reboot manually you can do it using:
$ sudo reboot

After these operations we will have all packages updated and can start installation sequence.

3.2 Installing Python manually

Installing dependencies (up to 3-5 minutes of execution):
$ sudo apt-get -y install libbz2-dev liblzma-dev libsqlite3-dev libncurses5-dev libgdbm-dev zlib1g-dev libreadline-dev libssl-dev tk-dev build-essential libncursesw5-dev libc6-dev openssl

Info: -y switch will disable all confirmation requests.
Going home:
$ cd ~

Downloading needful version (execution time depends of channel bandwidth):
$ wget https://www.python.org/ftp/python/3.6.0/Python-3.6.0.tgz

Unpacking archive:
$ tar -zxvf Python-3.6.0.tgz

Going to new directory:
$ cd Python-3.6.0

Configuring (takes few minutes: 3-4 minutes on RPi 3):
$ ./configure

Building (takes a lot of time, approx. 15 minutes on RPi 3):
$ make

Installing our own-built-python-release (few minutes for this operation, approx 3 minutes on RPi 3):
$ sudo make install

3.3 Verifying installation

Now we can check Python version using:
$ python3.6 --version

In case of success you will get something like this:
Python 3.6.0

Congratulations, now you have installed Python 3.6.

3.4 Installing pip

WARN! By default this Python installation already installs pip. So you can skip this chapter.

In case you want install it manually you need follow this steps.

Going home:
$ cd ~

Downloading needful script:
$ wget https://bootstrap.pypa.io/get-pip.py

Running this script using our new Python 3.6: 
$ sudo python3.6 get-pip.py

3.5 Testing installed pip

Let's test just installed pip:
$ pip3.6 --version

We will get something like this:
> pip 9.0.1 from /usr/local/lib/python3.6/site-packages (python 3.6

Also, you can download some package to test pip functionality:
$ sudo pip3.6 install python-dateutil

This will download to your RPi perfect library to manipulate datetime objects.

3.6 Cleaning up

You can remove Python sources and script for pip installation by these steps:

Removing Python sources:
$ rm -rf ~/Python-3.6.0

Removing get-pip.py:
$ rm ~/get-pip.py

4. Installing different versions of Python

You may note that this post contains few fixes for my previous post Building Python 3.5 on Raspberry Pi 2 (Raspbian) main differences is:
  1. other version of Python in link and folder names;
  2. different packages list when installing dependencies.
So, you can try (I can't guarantee anything) to install different versions of Python just using this list of dependencies and changing Python version in link and folder name

Hope you like it!

Enjoy!



22 comments:

  1. Hi,
    I tried this on raspi2 Jessie lite. I redid the ./configure a 2nd time with optimization. Now the make is going on for over 12 hours!!..It is doing the checks almost all the time. How do I abort grand gracefully?

    ReplyDelete
    Replies
    1. It's very long time. I think you can break it with Ctrl-C and just start again. Please, send me a note about results. Thank you.

      Delete
  2. Hi, I just install python3.6 on pi3, but there's a problem when I pip3 install only install at python3.6. Python3.4 pip didn't work. Virtualenv is not working well. mkvirtualenv --python==python2.7 will virtual python3.6

    ReplyDelete
  3. Please, show me your command line when you trying install using Python 3.4 pip. Thank you.

    ReplyDelete
  4. Thank you for very clear instructions. Worked first time! But I now have a problem
    with Pygame. I tried pip3.6 install Python-pygame but got the response that it couldn't find one. Can you point me in the right direction? My original distribution came with pygame for python3.4 but not 3.6. Apart from installing 3.6, I have made no other changes.

    ReplyDelete
    Replies
    1. I will try this issue on my RPi and will tell achieved results in 2 days.

      Delete
  5. Replies
    1. Hi.

      Sorry for long delay.

      When I installed Python 3.6 and then pip for Python 3.6 (following instructions below), I tried to install pygame using this command:
      $ sudo pip3.6 install pygame

      Next I'v got this message:
      >> WARNING: "sdl-config" failed!
      >> Unable to run "sdl-config". Please make sure a development version of SDL is installed.

      So I installed some more stuff using these commands:
      $ sudo apt-get install libsdl1.2-dev
      $ sudo dpkg -S sdl-config

      $ sudo apt-get install ibsdl-image1.2-dev libsdl-mixer1.2-dev libsdl-ttf2.0-dev libsdl1.2-dev libsmpeg-dev python3-numpy subversion libportmidi-dev libswscale-dev libavformat-dev libavcodec-dev

      After that I retried installation of PyGame using:
      $ sudo pip3.6 install pygame

      And everything was fine. So I tested installation using file:

      ---pgtest.py---
      #!/usr/bin/env python3

      from pygame.locals import *
      print("OK");
      ---------------

      Warn: notice that I accidentally installed python3-numpy which also installs Python3.4 minimal, you can remove

      Enjoy.

      Delete
    2. This is great! I'll try it out - very carefully. Thank you so much - especially for the precise instructions - would that everyone was as meticulous.

      Delete
    3. I have - finally - attempted to install pygame as instructed. I have a text file of the terminal messages. I'm afraid I don't know enough to interpret them - except I know it didn't work. I hope there's a simple explanation. Can I send you the file? I don't believe I'm allowed to attach it to this message.

      Delete
  6. @KittyHawk
    Thanks very much for your precise instructions; I've spent two days with only limited success (my builds took HOURS!). Before I embark on your method and reinstall, may I ask few questions?
    1. I already have 2.7 AND 3.4 installed by default, will 3.6 sit alongside 3.4? (my last attempt involved an altinstall)
    2. How will I point the *right* pip to do install? [python3.6 -m pip install numpy]? I need to install pylab, numpy, scipy, matplotlib & pygame
    3. my attempts with the above failed cos ("pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available"). Does your build solve this?
    Thanks again, you've give me the heart to face this again tomorrow!

    ReplyDelete
    Replies
    1. OK, a quick (limited!) progress report on how I got on:
      - initial install all fine
      -my times slightly better than Bohdan's EXCEPT I chose --optimisations which added HOURS to the make (not a problem as I need the 10% improvements that they're supposed to off)
      - couldn't install all the modules mentioned above
      -scipy failed ('no lapack/blas resources found')
      - pygame* failed ( after installing sdl found a whole load of dependencies missing; gave a little bit of reading hinted that pygame may not play nicely with 3.6)
      I don't understand enough yet to know how many of the problems I had were to do with trying to have >1 Python 3.x versions installed. Sometime I'll try another fresh install and remove P3.4 first!

      Thanks again, however, for this article - it's by far the best I've come across.

      *anyone had any joy with this? Do tell!

      Delete
  7. Thank you for taking the time to provide us with your valuable information.
    Freshers Jobs in Chennai

    ReplyDelete
  8. Hi! all. I did everything described here and went very well. I did change the 3.6.0 to 3.6.2. After that I wanted to remove version 2.7 as I don't plan to use it, using from internet:

    sudo apt-get remove python2.7
    sudo apt-get autoremove

    But now when I type "python --version" it print "bash: python: command not found" I'm Raspberry Pi3, Linux raspberry 4.9.41-v7+ , Raspbian GNU/Linux 9 (stretch). On the menu I don't have Preferences/config. Using VNC Viewer on Win7.
    Could you tell me what's wrong and how to repair this. Thanks

    ReplyDelete
    Replies
    1. When you calling `python --version` you calling for Python2 which was removes by your previous commands. If you want execute Python 3 you should call `python3 --version`

      Delete
  9. Thanks for the tutorial. The Pi community appreciates posts like this that show how to do more complex tasks like installing from source.

    ReplyDelete
  10. the $make step just took me 1.5 hours on a RPi2 FYI. but totally worth it!

    ReplyDelete
  11. I installed python 3.6 but couldn't get gi repo working.What is the best solution for this?

    ReplyDelete