리눅스 Python 2.7 컴파일 설치

upgrading python to 2.7
파이썬 2.7 업그레이드
파이썬 2.7로 업그레이드
Python 컴파일 설치

1 설치된 버전 확인

[root@jmnote ~]# python -V
Python 2.4.3
[root@jmnote ~]# python -V
Python 2.6.6

2 설치할 버전 확인

이 문서에서는 2.7.9로 실습

3 다운로드 및 설치

명령어
NEW_VER=2.7.9
cd /usr/local/src
wget --no-check-certificate -N http://www.python.org/ftp/python/$NEW_VER/Python-$NEW_VER.tgz 
tar xzf Python-$NEW_VER.tgz
cd Python-$NEW_VER
./configure
make
make altinstall
실행예시
[root@jmnote ~]# cd /usr/local/src
[root@jmnote src]# wget -N http://www.python.org/ftp/python/2.7.8/Python-2.7.8.tgz
... (생략)
100%[============================>] 14,846,119  1.79M/s   in 12s     

2014-09-04 01:57:31 (1.15 MB/s) - `Python-2.7.8.tgz' saved [14846119/14846119]
[root@jmnote src]# tar xzf Python-2.7.8.tgz
[root@jmnote src]# cd Python-2.7.8
[root@jmnote Python-2.7.8]# ./configure
... (생략)
creating Modules/Setup
creating Modules/Setup.local
creating Makefile
[root@jmnote Python-2.7.8]# make
... (생략)
changing mode of build/scripts-2.7/2to3 from 644 to 755
changing mode of build/scripts-2.7/smtpd.py from 644 to 755
/usr/bin/install -c -m 644 ./Tools/gdb/libpython.py python-gdb.py
[root@jmnote Python-2.7.8]# make altinstall
... (생략)
changing mode of /usr/local/bin/pydoc to 755
running install_egg_info
Writing /usr/local/lib/python2.7/lib-dynload/Python-2.7.8-py2.7.egg-info
rm /usr/local/lib/python2.7/lib-dynload/_sysconfigdata.py*

4 바꿔치기

[root@jmnote ~]# ll /usr/bin/python*
-rwxr-xr-x 1 root root    8304 Sep 22  2011 /usr/bin/python
lrwxrwxrwx 1 root root       6 Nov  1  2011 /usr/bin/python2 -> python
-rwxr-xr-x 1 root root    8304 Sep 22  2011 /usr/bin/python2.4
[root@jmnote Python-2.7.8]# cd
[root@jmnote ~]# /usr/bin/python -V
Python 2.4.3
[root@jmnote ~]# /usr/local/bin/python2.7 -V
Python 2.7.8

구버전은 python_old로 이름을 바꾸고 신버전을 python이라는 이름으로 복사한다.

[root@jmnote ~]# mv /usr/bin/python /usr/bin/python_old
[root@jmnote ~]# cp /usr/local/bin/python2.7 /usr/bin/python
[root@jmnote ~]# python -V
Python 2.7.8

5 (옵션) yum 오류 고치기

yum이 제대로 작동하지 않는다…[1]

[root@jmnote ~]# yum
There was a problem importing one of the Python modules
required to run yum. The error leading to this problem was:

   No module named yum

Please install a package which provides this module, or
verify that the module is installed correctly.

It's possible that the above module doesn't match the
current version of Python, which is:
2.7.8 (default, Sep  4 2014, 02:00:48) 
[GCC 4.1.2 20080704 (Red Hat 4.1.2-54)]

If you cannot solve this problem yourself, please go to 
the yum faq at:
  http://wiki.linux.duke.edu/YumFaq

다음과 같이 /usr/bin/yum 파일을 수정하자.

[root@jmnote ~]# cp /usr/bin/yum /usr/bin/yum_old
[root@jmnote ~]# sed -i 's/\/usr\/bin\/python/\/usr\/bin\/python2.4/g' /usr/bin/yum
[root@jmnote ~]# diff /usr/bin/yum_old /usr/bin/yum
1c1
< #!/usr/bin/python
---
> #!/usr/bin/python2.4
→ 파일의 1행에 있는 #!/usr/bin/python#!/usr/bin/python2.4로 수정하였다...
[root@jmnote ~]# yum
... (생략)
  --samearch-priorities
                        Priority-exclude packages based on name + arch

  Plugin Options:
→ 이제 잘 된다.

6 (원상복구)

  • 예전 버전으로 되돌리고 싶다면 yum에 있는 python으로 재설치하자.
  • 사실 원상복구는 아니다...
기존과 완전히 동일한 버전은 아니지만 yum이 의존성을 잘 맞춰줘서 별 문제 없을 것이다.
[root@jmnote ~]# yum reinstall python
... (생략)
======================================================================================================
 Package               Arch                  Version                     Repository              Size
======================================================================================================
Reinstalling:
 python                x86_64                2.6.6-52.el6                updates                 74 k

Transaction Summary
======================================================================================================
Reinstall     1 Package(s)

Total download size: 74 k
Installed size: 78 k
Is this ok [y/N]: y
... (생략)
Installed:
  python.x86_64 0:2.6.6-52.el6                                                                        

Complete!
[root@jmnote ~]# python -V
Python 2.6.6

7 같이 보기

8 주석

  1. /usr/bin/yum도 파이썬 스크립트임

9 참고 자료