본문 바로가기
Developments/Python

우분투(Ubuntu)에서 여러 버전의 Python을 사용하는 방법

by 푸르늬 2024. 4. 12.

개요

Docker를 활용하여 Ubuntu 환경에서 Python을 사용하거나, CentOS와 같이, 직접 Linux에서 Python을 사용하는 경우, 사용하는 라이브러리에 의해, Python의 버전을 바꿔가며 사용해야 할 때가 있다. 여기서는 Ubuntu 환경에서 Python의 venv와 같은 가상 환경을 만들지 않고도, Python의 버전을 바꿔가며 사용할 수 있는 방법을 알아보고자 한다.

 

환경

- Ubuntu 18.04 LTS in Docker (Container)

- Python3.6 (Default version in Ubuntu 18.04)

 

세팅 방법

여기에서는 Ubuntu18.04에 디폴트로 설치되어져 있는 Python 3.6 버전을 Python 3.8 버전으로 업그레이드해서 사용하려고 한다.

또한 언제든지 Python 3.6 버전으로 되돌려서 사용할 수 있도록 설정하려고 한다.

 

 

Step 0 : Python 버전 및 설치 위치 확인

python3 -V
python3 --version

which python3

 

Output :

python 3.6.9
/usr/bin/python3

 

 

Step 1 : Install Python3.8

$ sudo apt update -y
$ sudo apt install python3.8

 

apt update를 해주지 않으면 아래와 같이 python이 인스톨되지 않는다.

apt update를 하지 않고 python을 install하면 아무것도 인스톨되지 않는 것을 확인할 수 있다.

 

 

 

Step 2 : 각각의 Python 버전을 등록 (update-alternatives --install)

# python3.8을 우선 순위 1로 등록
$ sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 1

#python3.6을 우선 순위 2로 등록
$ sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 2

 

 

Step 3 : 사용하고 싶은 Python 버전을 선택 후 사용 (update-alternatives --config)

sudo update-alternatives --config python3
user@ubuntu1804:~$ sudo update-alternatives --config python3
There are 2 choices for the alternative python3 (providing /usr/bin/python3).

  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:

위의 type selection number에 1을 지정하면 Python3.8을, 2를 지정하면 Python3.6을 시스템의 메인 Python 버전으로 설정할 수 있게된다.

 

Step 4 : Python 버전 확인