Ubuntu16.04でpyenv3.7系の環境構築にひたすら躓いた話

pyenvを用いた、Python3.7.2の環境構築にひたすら躓いたので、解決手順をまとめていきます。

環境はUbuntu16.04、Google Cloud Computing Engineを利用しています。

## 1. no acceptable C compiler found
```
$ pyenv install 3.7.2
Downloading Python-3.7.2.tar.xz...
-> https://www.python.org/ftp/python/3.7.2/Python-3.7.2.tar.xz
Installing Python-3.7.2...

BUILD FAILED (Ubuntu 16.04 using python-build 1.2.12-2-geb68ec9)

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

Last 10 log lines:
checking for --enable-universalsdk... no
checking for --with-universal-archs... no
checking MACHDEP... checking for --without-gcc... no
checking for --with-icc... no
checking for gcc... no
checking for cc... no
checking for cl.exe... no
configure: error: in `/tmp/python-build.20190621225531.4895/Python-3.7.2':
configure: error: no acceptable C compiler found in $PATH
See `config.log' for more details
```
gccをインストールすることで解決。

```
$ sudo apt-get install build-essential
```

## 2. ZipImportError

```
$ pyenv install 3.7.2
Downloading Python-3.7.2.tar.xz...
-> https://www.python.org/ftp/python/3.7.2/Python-3.7.2.tar.xz
Installing Python-3.7.2...

BUILD FAILED (Ubuntu 16.04 using python-build 1.2.12-2-geb68ec9)

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

Last 10 log lines:
    sys.exit(ensurepip._main())
  File "/tmp/python-build.20190621230110.10841/Python-3.7.2/Lib/ensurepip/__init__.py", line 204, in _main
    default_pip=args.default_pip,
  File "/tmp/python-build.20190621230110.10841/Python-3.7.2/Lib/ensurepip/__init__.py", line 117, in _bootstrap
    return _run_pip(args + [p[0] for p in _PROJECTS], additional_paths)
  File "/tmp/python-build.20190621230110.10841/Python-3.7.2/Lib/ensurepip/__init__.py", line 27, in _run_pip
    import pip._internal
zipimport.ZipImportError: can't decompress data; zlib not available
Makefile:1130: recipe for target 'install' failed
make: *** [install] Error 1
```

Zlibが無いということでインストールして解決。

```
$ sudo apt-get install zlib1g-dev
```

## 3. ModuleNotFoundError: No module named '_ctypes'

```
$ pyenv install 3.7.2
Downloading Python-3.7.2.tar.xz...
-> https://www.python.org/ftp/python/3.7.2/Python-3.7.2.tar.xz
Installing Python-3.7.2...

BUILD FAILED (Ubuntu 16.04 using python-build 1.2.12-2-geb68ec9)

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

Last 10 log lines:
  File "/tmp/tmp9dawx9je/pip-18.1-py2.py3-none-any.whl/pip/_internal/commands/__init__.py", line 6, in <module>
  File "/tmp/tmp9dawx9je/pip-18.1-py2.py3-none-any.whl/pip/_internal/commands/completion.py", line 6, in <module>
  File "/tmp/tmp9dawx9je/pip-18.1-py2.py3-none-any.whl/pip/_internal/cli/base_command.py", line 18, in <module>
  File "/tmp/tmp9dawx9je/pip-18.1-py2.py3-none-any.whl/pip/_internal/download.py", line 38, in <module>
  File "/tmp/tmp9dawx9je/pip-18.1-py2.py3-none-any.whl/pip/_internal/utils/glibc.py", line 3, in <module>
  File "/tmp/python-build.20190621230630.24266/Python-3.7.2/Lib/ctypes/__init__.py", line 7, in <module>
    from _ctypes import Union, Structure, Array
ModuleNotFoundError: No module named '_ctypes'
Makefile:1130: recipe for target 'install' failed
make: *** [install] Error 1
```

libffi-devをインストールすることで解決。

```
$ sudo apt-get install libffi-dev
```

## 4. Missing the bzip2 lib? Missing the bzip2 lib? OpenSSL lib?

```
$ pyenv install 3.7.2
Downloading Python-3.7.2.tar.xz...
-> https://www.python.org/ftp/python/3.7.2/Python-3.7.2.tar.xz
Installing Python-3.7.2...
WARNING: The Python bz2 extension was not compiled. Missing the bzip2 lib?
WARNING: The Python readline extension was not compiled. Missing the GNU readline lib?
ERROR: The Python ssl extension was not compiled. Missing the OpenSSL lib?

Please consult to the Wiki page to fix the problem.
https://github.com/pyenv/pyenv/wiki/Common-build-problems


BUILD FAILED (Ubuntu 16.04 using python-build 1.2.12-2-geb68ec9)

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

Last 10 log lines:
		install|*) ensurepip="" ;; \
	esac; \
	 ./python -E -m ensurepip \
		$ensurepip --root=/ ; \
fi
Looking in links: /tmp/tmpid_vpfsb
Collecting setuptools
Collecting pip
Installing collected packages: setuptools, pip
Successfully installed pip-18.1 setuptools-40.6.2
```

`https://github.com/pyenv/pyenv/wiki/Common-build-problems` を見ろとのことなので、こちらに記載のある以下のコマンドにて解決。

```
$ sudo apt-get install -y make build-essential libssl-dev zlib1g-dev libbz2-dev \
libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev \
xz-utils tk-dev libffi-dev liblzma-dev python-openssl git
```