리눅스 Python 2.7 컴파일 설치

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

1 설치된 버전 확인

[root@zetawiki ~]# python -V
Python 2.4.3
[root@zetawiki ~]# 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@zetawiki ~]# NEW_VER=2.7.9
[root@zetawiki ~]# cd /usr/local/src
[root@zetawiki src]# wget --no-check-certificate -N http://www.python.org/ftp/python/$NEW_VER/Python-$NEW_VER.tgz
... (생략)
100%[===================================================>] 16,657,930  10.3M/s   in 1.5s    

2015-01-21 20:24:50 (10.3 MB/s) - “Python-2.7.9.tgz” saved [16657930/16657930]
[root@zetawiki src]# tar xzf Python-$NEW_VER.tgz
[root@zetawiki src]# cd Python-$NEW_VER
[root@zetawiki Python-2.7.9]# ./configure
... (생략)
creating Modules/Setup
creating Modules/Setup.local
creating Makefile
[root@zetawiki Python-2.7.9]# 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@zetawiki Python-2.7.9]# make altinstall
... (생략)
Writing /usr/local/lib/python2.7/lib-dynload/Python-2.7.9-py2.7.egg-info
rm /usr/local/lib/python2.7/lib-dynload/_sysconfigdata.py*
/usr/bin/install -c -m 644 ./Misc/python.man \
		/usr/local/share/man/man1/python2.7.1
if test "xno" != "xno"  ; then \
		case no in \
			upgrade) ensurepip="--altinstall --upgrade --no-default-pip" ;; \
			install|*) ensurepip="--altinstall --no-default-pip" ;; \
		esac; \
		 ./python -E -m ensurepip \
			$ensurepip --root=/ ; \
	fi

4 바꿔치기

[root@zetawiki Python-2.7.9]# cd
[root@zetawiki ~]# ls -l /usr/bin/python*
-rwx------. 2 root root 9032 Jan 22  2014 /usr/bin/python
lrwxrwxrwx. 1 root root    6 May 26  2014 /usr/bin/python2 -> python
-rwx------. 2 root root 9032 Jan 22  2014 /usr/bin/python2.6
-rwxr-xr-x  1 root root 1418 Jan 22  2014 /usr/bin/python2.6-config
lrwxrwxrwx  1 root root   16 Sep  4 02:35 /usr/bin/python-config -> python2.6-config
lrwxrwxrwx  1 root root    3 Sep  4 02:23 /usr/bin/python-pip -> pip
[root@zetawiki ~]# python -V
Python 2.6.6
[root@zetawiki ~]# /usr/bin/python -V
Python 2.6.6
[root@zetawiki ~]# /usr/local/bin/python2.7 -V
Python 2.7.9

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

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

5 (필요시) yum 오류 고치기

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

[root@zetawiki ~]# 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.9 (default, Jan 21 2015, 20:29:09) 
[GCC 4.4.7 20120313 (Red Hat 4.4.7-11)]

If you cannot solve this problem yourself, please go to 
the yum faq at:
  http://yum.baseurl.org/wiki/Faq

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

기존 버전이 2.4이였다면 #!/usr/bin/python#!/usr/bin/python2.4
기존 버전이 2.6이였다면 #!/usr/bin/python#!/usr/bin/python2.6
[root@zetawiki ~]# cp /usr/bin/yum /usr/bin/yum_old
[root@zetawiki ~]# sed -i 's/\/usr\/bin\/python/\/usr\/bin\/python2.6/g' /usr/bin/yum
[root@zetawiki ~]# diff /usr/bin/yum_old /usr/bin/yum
1c1
< #!/usr/bin/python
---
> #!/usr/bin/python2.6
[root@zetawiki ~]# yum
... (생략)
    --sec-severity=SEVERITY
                        Include security relevant packages, of this severity
    --advisory=ADVISORY
                        Include packages needed to fix the given advisory
→ 이제 잘 된다.

6 (optional) 원상복구

  • 예전 버전으로 되돌리고 싶다면 yum에 있는 python으로 재설치하자.
  • 사실 원상복구는 아니다...
기존과 완전히 동일한 버전은 아니지만 yum이 의존성을 잘 맞춰줘서 별 문제 없을 것이다.
[root@zetawiki ~]# 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@zetawiki ~]# python -V
Python 2.6.6

7 같이 보기

8 주석

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

9 참고

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}