개발자들은 파이썬(Python) 여러버전 설치가 필요한 경우가 있다. 이 글에서는 우분투 환경에서 여러버전 설치와 default 파이썬을 선택하는 방법에 대해 알아본다.
Python 설치
우분투는 보통 파이썬 설치가 기본이다. 그래서 현재 우분투에 설치되어 있는 파이썬 버전을 확인해 본다. 필자는 ubuntu 22.04 LTS 버전을 사용하고 있는데, python3이 기본이다. 아래와 같이 버전을 확인한다.
vboxuser@ubuntu:~/jammy$ python3 -V
Python 3.10.12
파이썬 예전 버전을 설치하기 위한 PPA 를 추가한다.
sudo apt install software-properties-common
sudo add-apt-repository ppa:deadsnakes/ppa
파이썬 버전2를 설치한다.
sudo apt install python2
설치한 파이썬 v2의 버전을 확인한다.
vboxuser@ubuntu:~/jammy$ python2 -V
Python 2.7.18
Default 파이썬 버전 설정
설치된 파이썬 바이너리를 확인한다.
vboxuser@ubuntu:~/jammy$ ls -l /usr/bin/python*
lrwxrwxrwx 1 root root 9 Jul 28 2021 /usr/bin/python2 -> python2.7
-rwxr-xr-x 1 root root 3592504 Jul 1 2022 /usr/bin/python2.7
lrwxrwxrwx 1 root root 10 Aug 18 2022 /usr/bin/python3 -> python3.10
-rwxr-xr-x 1 root root 5913032 Jun 11 05:26 /usr/bin/python3.10
-rwxr-xr-x 1 root root 960 Jan 25 2023 /usr/bin/python3-futurize
-rwxr-xr-x 1 root root 964 Jan 25 2023 /usr/bin/python3-pasteurize
Default 설정을 위해 update-alternatives --install
으로 파이썬 각각 버전을 설정한다.
sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.10 2
Default 설정을 바꾸려면 아래와 같이 update-alternatives –config python 실행하여 Default 버전을 선택한다.
vboxuser@ubuntu:~$ sudo update-alternatives --config python
There are 2 choices for the alternative python (providing /usr/bin/python).
Selection Path Priority Status
------------------------------------------------------------
* 0 /usr/bin/python3.10 2 auto mode
1 /usr/bin/python2.7 1 manual mode
2 /usr/bin/python3.10 2 manual mode
Press <enter> to keep the current choice[*], or type selection number: