또이리의 Server Engineer

우분투 파이썬 버전 변경 - alternatives 본문

Linux Engineer

우분투 파이썬 버전 변경 - alternatives

또이리 2020. 11. 15. 12:40

우분투 파이썬 버전 변경 - alternatives

ubuntu 18.04 Python, gcc, java alternatives

이번에는 지난 스토리에서 설치했던 파이썬, gcc와 같은 컴파일러나 애플리케이션의 버전을 관리하는 데 사용하는 update-alternatives라는 명령어를 알아보겠습니다.

 

많은 사용자들이 우분투, 센트 OS 등 리눅스 OS를 사용하면서 애플리케이션의 버전이 맞지 않아서, 애를 먹는 경우가 종종 발생합니다.

 

이런 경우에 다양한 버전을 설치해서 사용할 때 필요한 버전으로 우선순위를 변경 및 삭제하는 방법을 설명드리겠습니다.

 

Alternatives

alternatives는 기본적으로 심볼릭 링크를 관리해주는 명령어입니다. 심볼릭 링크란 A라는 파일에 123이라는 파이을 링크 해놓으면 A를 실행하게 되면 123을 실행하는 것과 같은 것이죠.

 

A라는 디렉터리나 파일에 링크를 걸어주는 것입니다. 파이썬을 예로 알아보겠습니다.

 

지난 스토리에서 우분투 18.04.4에 기본으로 설치되어있는 파이썬 3.6.9 버전에 추가로 파이썬 3.8.0 버전을 설치했습니다. alternatives에 등록하기 위해서는 python이 어느 경로에 설치되어있는지 확인해야 합니다.

 

지난 스토리에는 whereis를 사용했지만 이번에는 which 명령어를 사용하겠습니다.

 

간단하게 whereis + 명령어를 사용하게 되면, 실행 파일 위치, 소스 위치, 라이브러리 위치, 매뉴얼 위치 등의 경로가 검색되지만 which + 명령어를 사용하게 되면 명령어의 위치만 딱 검색됩니다.

 

현재 이 시스템에서 python을 실행했을 때, python3.8.0이 실행되게 하고 싶습니다.

하지만 지금 현재는 python 명령어를 실행했을 때 아무것도 되지 않습니다. 1차적으로는 python이 설치되어 있지 않습니다. 또는 심볼릭 링크가 걸려있지 않습니다.

 

보통 파이썬 2 버전이 설치되어있으면 파이썬 2 버전이 심볼릭 링크가 걸려있어 python이라고 명령어를 입력했을 때 파이썬 2 버전이 실행되는 게 일반적입니다.

 

하지만 현재 시스템에는 파이썬 2가 설치되어있지 않은 것이 분명합니다.

그래서 python3을 실행해 보았습니다. python3.6.9가 실행되었습니다. 저는 python 3.8을 사용해야 됩니다.

 

그래서 지난 스토리에서 파이썬 3.8을 설치했습니다. python3.8이라고 실행하면 python3.8을 사용할 수 있지만, 저는 python에 python3.8을 심볼릭 링크로 연결해서 지금 현재 우분투 18.04.4의 기본 파이썬이 3.8로 작동하기를 원합니다.

 

궁극적으로 python이라고 실행했을 때 python3.8.0이 실행되어야 합니다.

 

또 서론이 길었습니다. 그만큼 방법은 간단하니 설명이 길어진다고 보시면 됩니다.

python3.8의 경로를 확인하겠습니다.

root@localhost:~# whereis python3.8
python3: /usr/bin/python3 /usr/bin/python3.8 /usr/bin/python3.6m /usr/bin/python3.6 /usr/lib/python3.7 /usr/lib/python3 /usr/lib/python3.8 /usr/lib/python3.6 /etc/python3 /etc/python3.8 /etc/python3.6 /usr/local/lib/python3.8 /usr/local/lib/python3.6 /usr/share/python3 /usr/share/man/man1/python3.1.gz
root@localhost:~# which python3.8
/usr/bin/python3.8
root@localhost:~#

whereis 와 which의 차이를 확인할 수 있습니다.

 

파이썬 3.8은 /usr/bin/python3.8 디렉터리에 있습니다.

root@localhost:~# python

Command 'python' not found, but can be installed with:

apt install python3
apt install python
apt install python-minimal

You also have python3 installed, you can run 'python3' instead.

root@localhost:~# python3
Python 3.6.9 (default, Nov  7 2019, 10:44:02)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

위의 코드 블록과 같이 python에는 심볼릭 링크가 걸려있지 않습니다.

python3은 3.6.9 우분투 18.04.4에서 업데이트할 수 있는 최고 버전으로 설치되어있습니다.

 

python3.8 버전도 실행해보겠습니다.

root@localhost:~# python3.8
Python 3.8.0 (default, Oct 28 2019, 16:14:01)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

python3.8이 실행되는 것도 확인했고, 경로도 확인했습니다.

 

update-alternatives --config

root@localhost:/usr/bin# update-alternatives --config python
update-alternatives: error: no alternatives for python
root@localhost:/usr/bin# update-alternatives --config python3
update-alternatives: error: no alternatives for python3
root@localhost:/usr/bin#

아무것도 등록된 것이 없습니다. error가 발생하는 것이 당연합니다.

 

update-alternatives --install

이제 alternatives를 등록해야 됩니다.

root@localhost:/usr/bin# update-alternatives --install /usr/bin/python python /usr/bin/python3.8 1
update-alternatives: using /usr/bin/python3.8 to provide /usr/bin/python (python) in auto mode
root@localhost:/usr/bin# update-alternatives --config python
There is only one alternative in link group python (providing /usr/bin/python): /usr/bin/python3.8
Nothing to configure.
root@localhost:/usr/bin# python
Python 3.8.0 (default, Oct 28 2019, 16:14:01)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

맨 윗줄 명령어를 보겠습니다.

update-alternatives --install /usr/bin/python python /usr/bin/python3.8 1

 

맨 끝에 숫자 1이 중요합니다. 우선순위의 번호를 달아 주는 것입니다.

/usr/bin/python에 심볼릭 링크된 python명령어를 /usr/bin/python3.8로 대체하겠다는 뜻입니다.

 

update-alternatives --config python를 입력합니다.

1개의 대체 링크만 존재하기 때문에 configure가 없다고 나옵니다.

 

그리고 python을 실행하게 되면 python3.8.0이 실행되는 것을 볼 수 있습니다.

여기에 기존 python3.6.9를 등록하고 config를 보겠습니다.

root@localhost:/usr/bin# update-alternatives --install /usr/bin/python python /usr/bin/python3.6 2
update-alternatives: using /usr/bin/python3.6 to provide /usr/bin/python (python) in auto mode

root@localhost:/usr/bin# python
Python 3.6.9 (default, Nov  7 2019, 10:44:02)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>
[8]+  Stopped                 python
root@localhost:/usr/bin# update-alternatives --config python
There are 2 choices for the alternative python (providing /usr/bin/python).

  Selection    Path                Priority   Status
------------------------------------------------------------
* 0            /usr/bin/python3.6   2         auto mode
  1            /usr/bin/python3.6   2         manual mode
  2            /usr/bin/python3.8   1         manual mode

Press <enter> to keep the current choice[*], or type selection number: 2
update-alternatives: using /usr/bin/python3.8 to provide /usr/bin/python (python) in manual mode
root@localhost:/usr/bin# python
Python 3.8.0 (default, Oct 28 2019, 16:14:01)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

python3.6을 추가로 등록했습니다. 우선수위는 2로 주었습니다.

등록 이후 python을 실행하면 python3.6.9가 실행되는 것을 볼 수 있습니다.

 

다시 python3.8을 python 심볼릭 링크에 등록하고 싶다면, config에서 python3.8을 선택해 주면 됩니다.

추가적으로 auto 모드는 우선순위에 따라 결정됩니다. 우선순위는 1보다 2가 우선이며 2보다는 3이 우선입니다.

 

java와 gcc도 이런 식으로 update-alternatives 명령어를 사용해서 현재 시스템에서 사용할 버전을 선택할 수 있습니다.

마지막으로 config에서 삭제하는 방법도 알아보겠습니다.

 

update-alternatives --remove

root@localhost:/usr/bin# update-alternatives --remove python /usr/bin/python3.8
root@localhost:/usr/bin# update-alternatives --install /usr/bin/python python /usr/bin/python3.8 10
update-alternatives: using /usr/bin/python3.8 to provide /usr/bin/python (python) in auto mode
root@localhost:/usr/bin# update-alternatives --config python
There are 2 choices for the alternative python (providing /usr/bin/python).

  Selection    Path                Priority   Status
------------------------------------------------------------
* 0            /usr/bin/python3.8   10        auto mode
  1            /usr/bin/python3.6   2         manual mode
  2            /usr/bin/python3.8   10        manual mode

Press <enter> to keep the current choice[*], or type selection number:
[13]+  Stopped                 update-alternatives --config python
root@localhost:/usr/bin# update-alternatives --remove python /usr/bin/python3.8
update-alternatives: using /usr/bin/python3.6 to provide /usr/bin/python (python) in auto mode
root@localhost:/usr/bin# update-alternatives --config python
There is only one alternative in link group python (providing /usr/bin/python): /usr/bin/python3.6
Nothing to configure.
root@localhost:/usr/bin# python
Python 3.6.9 (default, Nov  7 2019, 10:44:02)
[GCC 8.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

먼저 python3.8의 우선순위를 10으로 변경했습니다 auto mode가 python 3.8로 변경되었습니다.

 

--remove 옵션과 심볼릭 링크가 걸린 명령어, 그리고 삭제할 버전의 경로를 뒤에 붙였습니다.

python 명령어를 실행했을 때 python3.6.9가 실행되었습니다.

 

이해하게 되면 응용은 쉽습니다.

update-alternatives

 

Comments