Wednesday, October 14, 2015

Building Python 3.5 on Raspberry Pi 2 (Raspbian)

Building Python 3.5 on Raspberry Pi 2 (Raspbian)


1. Problem



Problem: we want use the latest version of Python to run Flask and other fun stuff on our RPi 2 (mod. B). But only old version of Python (3.2) supported by apt-get.

We need to build and install our own build of latest Python release! So, our target will be Python 3.5.

Also, you may want to know What’s New In Python 3.5

UPD! You can follow procedures for Python 3.6 installation by following Building Python 3.6 on Raspberry Pi 3 from sources (clean Debian)


2. Solution

2.1. Preparation

2.1.1. SD card backup

You need make backup of whole SD card

On Windows you can use Win32DiskImager. This is GUI application and it's must be easy in use.

On Linux you can use dd:

$ sudo dd bs=4M if=/dev/sdb | gzip > /home/your_username/image`date +%d%m%y`.gz

This command will also compress your backup image with gzip. When you need to restore backed up image use this command:

$ gzip -dc /home/your_username/image.gz | sudo dd bs=4M of=/dev/sdb

2.1.2. Updating System

We need latest versions of everything on our system. So, let's update everything:

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

Now we need install components which will be necessary for our Python build:

$ sudo apt-get install build-essential libncursesw5-dev libgdbm-dev libc6-dev
$ sudo apt-get install zlib1g-dev libsqlite3-dev tk-dev
$ sudo apt-get install libssl-dev openssl

2.2. Building Python

Now let's go home then download, build and install our new Python.
Going home:

$ cd ~

Retrieving source codes of Python 3.5 (downloading time depends on your connection width):

$ wget https://www.python.org/ftp/python/3.5.0/Python-3.5.0.tgz

Unpacking sources archive (it can take approx half a minute):

$ tar -zxvf Python-3.5.0.tgz

Going de-e-e-eper:

$ cd Python-3.5.0

Configuring (takes few minutes):

$ ./configure

Building (takes approx. 30 minutes):

$ make

Installing our own-built-python-release (few minutes for this operation):

$ sudo make install

2.3. Testing installation

If everything is OK you will be able to find python3.5 command by pressing TAB after printing python to your shell. Also, you can test installation by running command:

$ python3.5 --version

If everything is OK you will get output like this:

Python 3.5.0

Congratulations, now you have installed Python 3.5.

3. Installing pip

3.1. Installing pip


If you want to use pip for you Python 3.5 installation let's install it.

Going home:

$ cd ~

Downloading needful script:

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

Running this script using our Python 3.5:

$ sudo python3.5 get-pip.py

3.2. Testing installed pip

Let's test just installed pip:

$ pip3.5 --version

We will get something like this:

pip 7.1.2 from /usr/local/lib/python3.5/site-packages (python 3.5)

Enjoy!

13 comments:

  1. Thanks! BTW on Rapberry Pi 3 it took 15 min to compile it

    ReplyDelete
    Replies
    1. I'm happy that it works on your RPi. Very fast compilation!

      Delete
  2. Thanks a lot, it worked perfectly! Took approx. one and a half hour on raspberry 1 model b, so it took some patience but worked out in the end.

    ReplyDelete
  3. thanks alot man helped me alot for python good tutorial!

    ReplyDelete
  4. Hello, when using : 'sudo python3.5 get-pip.py' i'm getting an error saying it can't fetch the url because I don't have required TLS/SSL. Any help?

    ReplyDelete
    Replies
    1. As I can see, newer version of Python > 3.5.0 requires different procedure. I will fix it in newer blog-post. Newer Python version will be used, also will be fixed problem with TLS/SSL support. I will write it in few days. Thank you.

      Delete
    2. You can follow this instructions:
      http://bohdan-danishevsky.blogspot.com/2017/01/building-python-360-on-raspberry-pi-3.html

      Delete
  5. Not working for me.

    sudo python3.5 get-pip.py

    Results in an error:
    pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.
    Collecting pip
    Could not fetch URL https://pypi.python.org/simple/pip/: There was a problem confirming the ssl certificate: Can't connect to HTTPS URL because the SSL module is not available. - skipping
    Could not find a version that satisfies the requirement pip (from versions: )
    No matching distribution found for pip

    Using the latest Raspbian PIXEL dated Nov 25, updated, upgraded, and dist-upgraded.

    ReplyDelete
    Replies
    1. As I can see, newer version of Python > 3.5.0 requires different procedure. I will fix it in newer blog-post. Newer Python version will be used, also will be fixed problem with TLS/SSL support. I will write it in few days. Thank you.

      Delete
    2. You can follow this instructions:
      http://bohdan-danishevsky.blogspot.com/2017/01/building-python-360-on-raspberry-pi-3.html

      Delete