linux系統安裝python3.6

糖冰橙發表於2020-10-26

如果系統已經安裝了python2.x,不需要解除安裝,直接安python3因為可能有程式依賴目前的python2環境,比如yum。

  • 安裝依賴環境
yum -y install zlib-devel bzip2-devel openssl-devel ncurses-devel sqlite-devel readline-devel tk-devel gdbm-devel db4-devel libpcap-devel xz-devel

如果安裝依賴不成功,則執行如下命令安裝依賴

sudo apt-get install -y make build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev xz-utils tk-dev
  • 下載python3的包
wget https://www.python.org/ftp/python/3.6.1/Python-3.6.1.tgz

官網:https://www.python.org/downloads/

  • 解壓並拷貝到/usr/local目錄下
tar -zxvf Python-3.6.1.tgz -C /usr/local
  • 編譯安裝
cd Python-3.6.1

./configure --prefix=/usr/local/python3

make

make install
  • 建立python3的軟鏈
ln -s /usr/local/python3/bin/python3 /usr/bin/python3
  • 將/usr/local/python3/bin加入PATH
vim ~/.bash_profile
# .bash_profile
 
# Get the aliases and functions
 
if [ -f ~/.bashrc ]; then
 
. ~/.bashrc
 
fi
 
# User specific environment and startup programs
 
PATH=$PATH:$HOME/bin:/usr/local/python3/bin
 
export PATH
  • 生效環境變數配置
source ~/.bash_profile
  • 檢查Python3及pip3是否正常可用
~ python3
Python 3.6.1 (default, Oct 26 2020, 19:14:50)
[GCC 4.8.5 20150623 (Red Hat 4.8.5-39)] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> 

相關文章