Linux CentOS 8에서 python39 설치하기

python post logo Linux

CentOS 8 버전 OS 운영 체제를 설치할 때 소프트웨어 선택을 “최소 선택“으로 설치했다면 python이 포함되지 않기 때문에 별도의 설치 없이는 사용할 수 없습니다. 이 글에서는 Linux 계열 OS에서 추가로 python 패키지를 설치하는 방법을 소개 합니다. 참고로 yum과 dnf는 같은 명령입니다. 이 글에서 사용한 dnf 명령 대신 yum명령을 사용할 수 있습니다.

작업 환경

이 글은 다음 환경에서 작업했습니다. OS 버전이 다르면 명령 또는 명령 실행 결과가 다소 다를 수 있습니다.

$ cat /etc/redhat-release
CentOS Linux release 8.4.2105
$ uname -a
Linux centossrv01 4.18.0-305.3.1.el8.x86_64 #1 SMP Tue Jun 1 16:14:33 UTC 2021 x86_64 x86_64 x86_64 GNU/Linux

python 설치하기

다음과 같이 python또는 py명령이 실행 가능한지 확인합니다.

$ python
-bash: python: command not found
$ py
-bash: py: command not found

다음과 같이 python을 설치하려고 하면 python2, python36, python38, python39 버전에서 선택해야 한다는 메시지를 표시합니다.

$ sudo dnf install python
Last metadata expiration check: 1 day, 9:30:55 ago on Sun Aug  8 14:41:33 2021.
No match for argument: python
There are following alternatives for "python": python2, python36, python38, python39
Error: Unable to find a match: python

다음과 같이 최신 버전으로 선택하여 설치합니다. 설치 결과는 여기를 참조 하십시오.

$ sudo dnf install python39
... 중간 생략 ...
Installed:
  python39-3.9.2-1.module_el8.4.0+720+bfbc1bcb.x86_64
  python39-libs-3.9.2-1.module_el8.4.0+720+bfbc1bcb.x86_64
  python39-pip-20.2.4-3.module_el8.4.0+680+7b309a77.noarch
  python39-pip-wheel-20.2.4-3.module_el8.4.0+680+7b309a77.noarch
  python39-setuptools-50.3.2-3.module_el8.4.0+680+7b309a77.noarch
  python39-setuptools-wheel-50.3.2-3.module_el8.4.0+680+7b309a77.noarch

Complete!
$

설치 결과 확인하기

다음과 같이 python3.9가 설치되었음을 확인할 수 있습니다.

$ sudo ls -l /usr/bin/* | grep python
lrwxrwxrwx. 1 root root          25 Aug 10 00:27 /usr/bin/python3 -> /etc/alternatives/python3
-rwxr-xr-x. 1 root root        7728 Mar 11 02:32 /usr/bin/python3.9
lrwxrwxrwx. 1 root root          24 Jul 10 17:28 /usr/bin/unversioned-python -> /etc/alternatives/python

그렇다고 바로 python명령을 사용할 수 있는 것은 아닙니다. 다음 명령으로 python3.9를 기본으로 사용할 수 있도록 python명령으로 연결합니다.

$ sudo update-alternatives --config python

There are 3 programs which provide 'python'.

  Selection    Command
-----------------------------------------------
*+ 1           /usr/libexec/no-python
   2           /usr/bin/python3
   3           /usr/bin/python3.9

Enter to keep the current selection[+], or type selection number: 3
$ python

Python 3.9.2 (default, Mar 10 2021, 17:29:56)
[GCC 8.4.1 20200928 (Red Hat 8.4.1-1)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> quit()
$

Hello World 출력 샘플

다음과 같이 print (“Hello World”) 내용으로 test.py 파일을 작성 python test.py 명령으로 실행합니다.

$ cat test.py
print ("Hello World")

$ python test.py
Hello World

이제 python프로그램을 작성할 수 있습니다.

제목과 URL을 복사했습니다