As of Jan 2019, Raspberry Pi comes with Python 3 but it’s an older version of Python 3 (<3.6). The latest Python version is 3.7.2 at the time of this post. If we want to use the latest version on Raspberry Pi, we need to compile it from the source. Please make sure to note the use of ‘sudo’ in the command lines because at certain lines you don’t want to have sudo because it’ll change the location of the configuration and installation and you might not end up with a working Python 3.7.2
First make sure you update your Raspbian to the latest update. If any prompt comes up just answer ‘yes’. There might be some information page that you have to scroll through by holding down the space key to the end. After that press q to continue with the installation.
$ sudo apt-get update
$ sudo apt-get -y dist-upgrade
Your Raspberry Pi might require a reboot. If you end up with a black screen and no response at some point then you can unplug and plug in the power to re-start your Pi.
Now you are ready to upgrade to Python 3.7.2. One thing to note is that your GPIO libraries that’s specific to the hardware might not work. You can always still access your original system Python 3 using /usr/bin/python 3 to bring up the original system Python3
Before downloading and compiling the new Python 3, install all the required packages to do this:
$ sudo apt-get install libffi-dev 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 git
You might have to force install libssl-dev 1.0.2 in order to get pip to work with Python 3.7.2 since apparently you have to have OpenSSL 1.0.2 or 1.1 compatible libssl.
You can check your version of libssl-dev using this command:
$ sudo apt-cache policy libssl-dev
I found a post discussing how he solved this issue by running this install first before compiling Python 3.7.2
$ sudo apt install -t jessie-backports libssl-dev
Download Python 3.7.2 and extract it.
$ wget https://www.python.org/ftp/python/3.7.2/Python-3.7.2.tar.xz
$ tar xf Python-3.7.2.tar.xz
$ cd Python-3.7.2
Now configure the directory. It’s important to have the –with-ssl option or openssl wouldn’t be included. This will take a couple of minutes:
$ ./configure --prefix=$HOME/.local --with-ssl --enable-optimizations
Build and install Python 3.7.2. This step takes a long time. On my Raspberry Pi B (not B+) it takes about 30 minutes. DO NOT USE SUDO! Make sure to have the -l 4 option there to keep the error build limit or you’ll encounter problems.
$ make -j -l 4
$ make install
After the installation is done, update ~/.bashrc so that when you type Python3 as a command it will choose Python 3.7.2 over the pre-installed Python 3.4.
$ nano ~/.bashrc
Add the following line at the end of the file. Save and the exit nano.
You can now close this terminal and open a new one. Launch python3 using the following command and check what version you have. Use exit() to quit Python.
You can also check what version of pip3 you have by typing in the command:
$ pip3 --version
Remember to have the 3 at the end of these commands otherwise you’ll launch the default Python 2.7.
If there you have limited space on your Raspberry Pi, you might want to remove all the compile tools and the source code to free up space. Use the following commands. Make sure you are not in the Python-3.7.2 directory where we were doing the compiling.
$ cd $home
$ sudo rm -r Python-3.7.2
$ sudo rm Python-3.7.2.tar.xz
$ sudo apt-get --purge remove libffi-dev 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 git -y
$ sudo apt-get autoremove -y
$ sudo apt-get clean
I want to give credit to the following pages for the original content on how to install Python 3.7.x on Raspbian:
Enjoy Python 3.7.2 on Raspberry Pi!