Install pyenv on Raspberry Pi and version control Python

Python of 3.5.3 is installed by default on Raspberry Pi, I wanted to use Python with 3.6 or higher, so I'll use pyenv to switch versions.


$ uname -a
# Linux raspberrypi 4.19.66-v7+ #1253 SMP Thu Aug 15 11:49:46 BST 2019 armv7l GNU/Linux
$ lsb_release -a                 
# No LSB modules are available.
# Distributor ID: Raspbian
# Description:    Raspbian GNU/Linux 9.11 (stretch)
# Release:        9.11
# Codename:       stretch

Install pyenv

First, install the required packages.

$ sudo apt update
$ sudo apt upgrade
$ sudo apt install -y git openssl libssl-dev libbz2-dev libreadline-dev libsqlite3-dev

Clone pyenv from github.

$ git clone ~/.pyenv

Add the following to .bash_profile so that you can hit the command.

$ sudo vi ~/.bash_profile


export PYENV_ROOT="$HOME/.pyenv"
export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init -)"

Reload .bash_profile.

$ source ~/.bash_profile

Installation is complete when the version is displayed with the following command.

$ pyenv --version
#pyenv 1.2.16-5-g7097f820

Version change with pyenv

Let's display a list of versions that can be installed.

$ pyenv install --list
#Available versions:
#  2.1.3
#  2.2.3
#  ...
#  3.6.0
#  3.6-dev
#  ...
#  stackless-3.4.7
#  stackless-3.5.4

This time I will install 3.6.0.

$ pyenv install 3.6.0

Let's actually change the version after the installation is completed.

First, check the version before the change.

$ python --version
#Python 2.7.13
$ python3 --version
#Python 3.5.3

Change the version.

$ pyenv global 3.6.0

The version has changed as below!

$ python --version
#Python 3.6.0
$ python3 --version
#Python 3.6.0

About pyenv settings

Set pyenv with the following command.

$ pyenv global x.x.x

By the way, you can set the scope by changing this global part.

Scope of application Use applications
shell Applies only to the shell you are currently using Temporary use
local Apply to current directory I want to set for each folder
global Applies to the whole I want to change the overall default environment

You can restore it by changing the x.x.x part to system.


$ pyenv local 3.6.4
$ pyenv shell 2.7.6
$ pyenv global system

You can also see the list of installed versions with the following command.

$ pyenv versions


When I tried to install 3.7 series, I could not install it with the following error.

$ pyenv install 3.7.0
Downloading Python-3.7.0.tar.xz...
Installing Python-3.7.0...

BUILD FAILED (Raspbian 9.11 using python-build 1.2.16-5-g7097f820)

Inspect or clean up the working tree at /tmp/python-build.20200213174102.2652
Results logged to /tmp/python-build.20200213174102.2652.log

Last 10 log lines:
  File "/tmp/tmp01sfxi5t/pip-10.0.1-py2.py3-none-any.whl/pip/_internal/", line 42, in <module>
  File "/tmp/tmp01sfxi5t/pip-10.0.1-py2.py3-none-any.whl/pip/_internal/", line 16, in <module>
  File "/tmp/tmp01sfxi5t/pip-10.0.1-py2.py3-none-any.whl/pip/_internal/", line 25, in <module>
  File "/tmp/tmp01sfxi5t/pip-10.0.1-py2.py3-none-any.whl/pip/_internal/", line 39, in <module>
  File "/tmp/tmp01sfxi5t/pip-10.0.1-py2.py3-none-any.whl/pip/_internal/utils/", line 3, in <module>
  File "/tmp/python-build.20200213174102.2652/Python-3.7.0/Lib/ctypes/", line 7, in <module>
    from _ctypes import Union, Structure, Array
ModuleNotFoundError: No module named '_ctypes'
Makefile:1122: recipe for target 'install' failed
make: *** [install] Error 1

By installing libffi-dev, I was able to install 3.7 series as well.

$ sudo apt install libffi-dev
$ pyenv install 3.7.0

Today is up to here!


-Building an environment with pyenv and pyenv-virtualenv

Recommended Posts

Install pyenv on Raspberry Pi and version control Python
Install pyenv and Python 3.6.8 on Ubuntu 18.04 LTS
Install ZIP version Python and pip on Windows 10
Introduced pyenv on Raspberry Pi
Install OpenCV4 on Raspberry Pi 3
Install TensorFlow 1.15.0 on Raspberry Pi
Ubuntu 20.04 on raspberry pi 4 with OpenCV and use with python
MQTT on Raspberry Pi and Mac
raspberry pi 4 centos7 install on docker
Install xgboost (python version) on Windows
Install ghoto2 on Raspberry Pi (memo)
Install Python on CentOS using Pyenv
Install OpenMedia Vault 5 on Raspberry Pi 4
Install Python on CentOS using pyenv
Install pyenv on MacBook Air and switch python to use
Install python and Visual Studio Code on windows10 (April 2020 version)
Install PyCall on Raspberry PI and try using GPIO's library for Python from Ruby
Control the motor with a motor driver using python on Raspberry Pi 3!
Detect "brightness" using python on Raspberry Pi 3!
Adafruit Python BluefruitLE works on Raspberry Pi.
Install 64-bit OS (bate) on Raspberry Pi
Notes on building Python and pyenv on Mac
Install docker-compose on 64-bit Raspberry Pi OS
Run servomotor on Raspberry Pi 3 using python
Install Python Control
Detect temperature using python on Raspberry Pi 3!
How to install NumPy on Raspberry Pi
Install pyenv and rbenv on CentOS system-wide
Working with GPS on Raspberry Pi 3 Python
Use python on Raspberry Pi 3 to light the LED with switch control!
Install and run Python3.5 + NumPy + SciPy on Windows 10
Detect slide switches using python on Raspberry Pi 3!
Install Python 3.8.6 on macOS Big Sur using pyenv
Install OpenCV 4.0 and Python 3.7 on Windows 10 with Anaconda
Detect magnet switches using python on Raspberry Pi 3!
Make DHT11 available on Raspberry Pi + python (memo)
Sound the buzzer using python on Raspberry Pi 3!
Install MongoDB on Ubuntu 16.04 and operate via python
Connect to MySQL with Python on Raspberry Pi
Build a Python development environment on Raspberry Pi
Install Python and libraries for Python on MacOS Catalina
Install python on WSL
Install pyenv and pyenv-virtualenv
Install Python on Pidora.
pigpio on Raspberry pi
Install Python on Mac
Install Python 3 on Mac
Install Python3.4 on CentOS 6.6
Install python (pyenv, pyenv-virtualenv)
Install python on windows
Install Python 2.7.3 on CentOS 5.4
Install python with pyenv
Install Python 3.3 on Ubuntu 12.04
Install Python 3.4 on Mac
Python version switching (pyenv)
Install pyenv on OSX
Cython on Raspberry Pi
Install Python 3.6 on Docker
Creating a temperature control system with Raspberry Pi and ESP32 (3) Recipient Python file
Use python on Raspberry Pi 3 and turn on the LED when it gets dark!
Record temperature and humidity with systemd on Raspberry Pi