# Python - Ubuntu 安裝多個版本 ###### tags: `Python` `Ubuntu` `Install` Ubuntu 18.0.4 裝好有內建 Python3.6, 但想裝 Python3.7 之後隔很久又想裝 Python3.8 一開始,我是參照這篇文章, # [How to upgrade to Python 3.8 on Ubuntu 18.04 LTS](https://www.itsupportwale.com/blog/how-to-upgrade-to-python-3-8-on-ubuntu-18-04-lts/) 但裝完後,我的 __Terminal (終端機)__ 就開不起來了!! 也許是執行有影響 ``` sudo apt-get update apt list | grep python3.8 ``` --- 之後我參考這篇 # [How to Install Python 3.8 on Ubuntu, Debian and LinuxMint](https://tecadmin.net/install-python-3-8-ubuntu/) ## Step 1 – Prerequisite As you are going to install Python 3.8 from the source. You need to install some development libraries to compile Python source code. Use the following command to install prerequisites for Python: ``` sudo apt-get install build-essential checkinstall sudo apt-get install libreadline-gplv2-dev libncursesw5-dev libssl-dev libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev libffi-dev zlib1g-dev libpq-dev ``` 編譯需要的 lib: ``` sudo apt-get install zlib1g-dev sudo apt-get install libssl-dev sudo apt-get install libffi-dev sudo apt-get install libreadline-gplv2-dev sudo apt-get install libncursesw5-dev sudo apt-get install libsqlite3-dev sudo apt-get install libgdbm-dev sudo apt-get install libbz2-dev sudo apt-get install tk-dev --> 差異最大 sudo apt-get install libc6-dev -> 不需要 sudo apt-get install libpq-dev --> psycopg2-binary==2.8.1 需要 ``` ## Step 2 – Download Python 3.8 Download Python source code using the following command from python official site. You can also download the latest version in place of specified below. ``` cd /opt sudo wget https://www.python.org/ftp/python/3.8.2/Python-3.8.2.tgz ``` Then extract the downloaded source archive file ``` sudo tar xzf Python-3.8.2.tgz ``` 下載 ``Python-3.8.2.tgz`` 並解壓縮 ## Step 3 – Compile Python Source Use the below set of commands to compile Python source code on your system using the altinstall command. make 與安裝 ``` cd Python-3.8.2 sudo ./configure --enable-optimizations sudo make altinstall ``` 在我的機器都常看見 ``` WARNING: The directory '/home/elite_lin/.cache/pip/http' or its parent directory is not owned by the current user and the cache has been disabled. Please check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag. WARNING: The directory '/home/elite_lin/.cache/pip' or its parent directory is not owned by the current user and caching wheels has been disabled. check the permissions and owner of that directory. If executing pip with sudo, you may want sudo's -H flag. ``` 使用這種方式安裝 Python, 目錄是在 ``/opt`` 底下,而非 ``/usr/bin/python3X`` 但用起來正常,可以使用 ``` python3.8 -m venv venv38 ``` 建立虛擬工作目錄