CentOS는 기본적으로 Python3를 제공하지 않고 있음 (CentOS 7 이하)
centos-release-scl 패키지를 설치하면 rh-python36을 설치할 수 있음
SCL은 Software Collections로서 CentOS에 신규 python 버전을 사용하게 해줌
sudo yum makecache fast
sudo yum install centos-release-scl
sudo yum install rh-python36
/opt/rh/rh-python36/root/ 디렉토리 하위에 설치됨
이렇게 설치된 python을 사용하려면 PATH 환경변수에 위 설치경로를 추가해줘야 함
export PATH=$PATH:/opt/rh/rh-python36/root/usr/bin
빌드 중 _ctypes 모듈을 못 찾아서 발생하는 오류를 방지하기 위해 libffi 라이브러리와 헤더를 설치해야 함
빌드 중 zlib 라이브러리를 못 찾아서 발생하는 오류를 방지하기 위해 zlib 라이브러리와 헤더를 설치해야 함
sudo yum install wget gcc make
sudo yum install libffi-devel zlib-devel
pip로 외부 모듈을 설치하려면 https 통신이 가능해야 함. ssl 모듈이 활성화되지 않으면 다음과 같은 오류가 발생함
pip search bs4
Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError("Can't connect to HTTPS URL because the SSL module is not available.")': /pypi
python
>>>> import ssl
Traceback (most recent call last):
File "", line 1, in
File "python3-7.2/lib/python3.7/ssl.py", line 98, in
import _ssl # if we can't import it, let the error propagate
ModuleNotFoundError: No module named '_ssl'
openssl-devel 패키지를 먼저 설치하고 Python을 빌드해야 함
sudo yum install openssl-devel
wget https://www.python.org/ftp/python/3.7.2/Python-3.7.2.tgz
tar xvfz Python-3.7.2.tgz
cd Python-3.7.2
./configure --prefix=설치경로 --with-ssl-default-suites=python && make all && make install
같은 CentOS라고 하더라도 위와 같은 방법으로 ssl이 활성화되지 않을 수 있음 (특히, /usr/lib64에 openssl 라이브러리 파일이 설치되는 경우)
이럴 때는 openssl을 소스 빌드하여 /usr/local/ssl 하위에 설치할 필요가 있음
wget https://www.openssl.org/source/openssl-1.1.1b.tar.gz
tar xvfz openssl-1.1.1b.tar.gz
cd openssl-1.1.1b
./config --prefix=/usr/local/ssl && make all && sudo make install
export LD_LIBRARY_PATH=$LD_LIBRARY_PATH:/usr/local/ssl/lib
LD_LIBRARY_PATH 환경변수 설정은 반드시 .bashrc에 추가해야 python이나 pip 바이너리 실행 시 정상 동작함
wget https://www.python.org/ftp/python/3.7.2/Python-3.7.2.tgz
tar xvfz Python-3.7.2.tgz
cd Python-3.7.2
Modules/Setup.dist 파일 편집
# Socket module helper for socket(2)
_socket socketmodule.c # <---- 여기 주석을 제거해야 함
# Socket module helper for SSL support; you must comment out the other
# socket line above, and possibly edit the SSL variable:
SSL=/usr/local/ssl # <---- 여기 확인하고 4줄의 주석을 제거해야 함
_ssl _ssl.c \\
-DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \\
-L$(SSL)/lib -lssl -lcrypto
./configure --prefix=설치경로 && make all install
--prefix 옵션으로 지정한 설치경로 하위에 설치됨. 마찬가지로 PATH 환경변수에 설치경로가 추가되어야 함
export PATH=$PATH:설치경로/bin
python -m ssl