當前位置: 首頁>>技術問答>>正文


在Ubuntu上如何安裝最新的Python 2.7.X或3.X?

問題詳細描述

我想在Ubuntu上安裝最新的Python壓縮包(從http://python.org/download/下載)。

下麵的命令正確的安裝方式嗎?

./configure
make
make install

如果不是,我該如何做?

最佳解決辦法

首先,安裝一些依賴項:

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

然後使用以下命令下載:

version=2.7.13
cd ~/Downloads/
wget https://www.python.org/ftp/python/$version/Python-$version.tgz

解壓並cd到相應的目錄:

tar -xvf Python-$version.tgz
cd Python-$version

然後,使用之前嘗試的命令來安裝,即checkinstall,這中安裝方式的好處是在需要時更容易卸載:

./configure
make
sudo checkinstall

注意,上麵的環境變量version可以改為任意需要的版本(例如version=2.7.1version=3.6.0)。

次佳解決辦法

除非你特別想要嘗試自己編譯安裝,否則首選的方法是使用DeadSnakes PPA來安裝非默認版本的Python:

sudo add-apt-repository ppa:fkrull/deadsnakes
sudo apt-get update
sudo apt-get install python2.7

當然,其他版本,如python2.4python3.6等等,在上麵的命令中也可用。

第三種解決思路(全麵,長文慎入)

這是對最佳方案的補充,術語libread5-dev應改為libreadline-gplv2-dev。所以完整的命令是:

sudo apt-get install libreadline-gplv2-dev libncursesw5-dev libssl-dev libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev

第四種辦法

對於Ubuntu 16.04.1服務器,默認的Python是3.5版本,默認情況下不安裝Python 2.7。注意,在新版本服務器上,甚至沒有python的可執行文件):

$ type python3 python2 python 
python3 is /usr/bin/python3
-bash: type: python2: not found
-bash: type: python: not found

$ python3 --version 
Python 3.5.2

$ python --version 
The program 'python' can be found in the following packages:
 * python-minimal
 * python3
Try: sudo apt install <selected package>

安裝python 2.7的命令很簡單:

$ sudo apt-get install python2.7

安裝python 2.7的初始輸出如下:

$ sudo apt-get install python2.7
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  libpython2.7-minimal libpython2.7-stdlib python2.7-minimal
Suggested packages:
  python2.7-doc binutils binfmt-support
The following NEW packages will be installed:
  libpython2.7-minimal libpython2.7-stdlib python2.7 python2.7-minimal
0 upgraded, 4 newly installed, 0 to remove and 0 not upgraded.
Need to get 3,735 kB of archives.
After this operation, 15.8 MB of additional disk space will be used.
Do you want to continue? [Y/n] Y
...etc, etc...

安裝了python 2.7之後,

$ type python3 python2.7 python3.5 python2 python
python3 is /usr/bin/python3
python2.7 is /usr/bin/python2.7
python3.5 is /usr/bin/python3.5
bash: type: python2: not found
bash: type: python: not found

好了2.7安裝好了。但是還有一個問題:不能通過pip安裝PyPI模塊 – 例如,如果您想要使用jupyter notebook或最新的scipy或numpy(etc),則需要安裝pip,然後通過pip install安裝前述功能。當然,還得使用apt-get安裝所需的係統依賴關係,如graphviz或核心係統庫。

$ type pip3 pip2 pip
bash: type: pip3: not found
bash: type: pip2: not found
bash: type: pip: not found

$ python3 -m pip --version 
/usr/bin/python3: No module named pip

所以要安裝pip,命令是sudo apt-get install python-pip,也很簡單:

$ sudo apt-cache search -n pip | egrep '^python[0-9]*-pip'
python-pip - alternative Python package installer
python-pip-whl - alternative Python package installer
python3-pip - alternative Python package installer - Python 3 version of the package

需要為Python 2.7 pip安裝python-pip,也要為Python 3 pippython3-pip。通過apt-get確保安裝所需的依賴項;例如,這裏是安裝pip2的輸出:

$ sudo apt-get install python-pip
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  binutils build-essential dpkg-dev fakeroot g++ g++-5 gcc gcc-5 libalgorithm-diff-perl
  libalgorithm-diff-xs-perl libalgorithm-merge-perl libasan2 libatomic1 libc-dev-bin libc6-dev
  libcc1-0 libcilkrts5 libdpkg-perl libexpat1-dev libfakeroot libfile-fcntllock-perl libgcc-5-dev
  libgomp1 libitm1 liblsan0 libmpx0 libpython-all-dev libpython-dev libpython-stdlib libpython2.7
  libpython2.7-dev libquadmath0 libstdc++-5-dev libtsan0 libubsan0 linux-libc-dev make
  manpages-dev python python-all python-all-dev python-dev python-minimal python-pip-whl
  python-pkg-resources python-setuptools python-wheel python2.7-dev
Suggested packages:
  binutils-doc debian-keyring g++-multilib g++-5-multilib gcc-5-doc libstdc++6-5-dbg gcc-multilib
  autoconf automake libtool flex bison gdb gcc-doc gcc-5-multilib gcc-5-locales libgcc1-dbg
  libgomp1-dbg libitm1-dbg libatomic1-dbg libasan2-dbg liblsan0-dbg libtsan0-dbg libubsan0-dbg
  libcilkrts5-dbg libmpx0-dbg libquadmath0-dbg glibc-doc libstdc++-5-doc make-doc python-doc
  python-tk python-setuptools-doc
The following NEW packages will be installed:
  binutils build-essential dpkg-dev fakeroot g++ g++-5 gcc gcc-5 libalgorithm-diff-perl
  libalgorithm-diff-xs-perl libalgorithm-merge-perl libasan2 libatomic1 libc-dev-bin libc6-dev
  libcc1-0 libcilkrts5 libdpkg-perl libexpat1-dev libfakeroot libfile-fcntllock-perl libgcc-5-dev
  libgomp1 libitm1 liblsan0 libmpx0 libpython-all-dev libpython-dev libpython-stdlib libpython2.7
  libpython2.7-dev libquadmath0 libstdc++-5-dev libtsan0 libubsan0 linux-libc-dev make
  manpages-dev python python-all python-all-dev python-dev python-minimal python-pip
  python-pip-whl python-pkg-resources python-setuptools python-wheel python2.7-dev
0 upgraded, 49 newly installed, 0 to remove and 0 not upgraded.
Need to get 61.1 MB of archives.
After this operation, 169 MB of additional disk space will be used.
Do you want to continue? [Y/n] Y
...etc...

有趣的是:現在有了”standard(標準的)”(PEP推薦)python2python3(不過它們隻是python 2.7和python 3.5的符號鏈接):

$ type python3 python2 python python2.7 python3.5 
python3 is /usr/bin/python3
python2 is /usr/bin/python2
python is /usr/bin/python
python2.7 is /usr/bin/python2.7
python3.5 is /usr/bin/python3.5

上麵是pip2,pip3的話要執行sudo apt-get install python3-pip;在安裝pip3之前:

$ type pip pip2 pip3
pip is /usr/bin/pip
pip2 is /usr/bin/pip2
-bash: type: pip3: not found

$ python2 -m pip --version 
pip 8.1.1 from /usr/lib/python2.7/dist-packages (python 2.7)

$ python3 -m pip --version 
/usr/bin/python3: No module named pip

而安裝pip3後,

$ sudo apt-get install python3-pip
Reading package lists... Done
Building dependency tree       
Reading state information... Done
The following additional packages will be installed:
  libpython3-dev libpython3.5-dev python3-dev python3-setuptools python3-wheel python3.5-dev
Suggested packages:
  python-setuptools-doc
The following NEW packages will be installed:
  libpython3-dev libpython3.5-dev python3-dev python3-pip python3-setuptools python3-wheel python3.5-dev
0 upgraded, 7 newly installed, 0 to remove and 0 not upgraded.
Need to get 38.0 MB of archives.
After this operation, 55.2 MB of additional disk space will be used.
Do you want to continue? [Y/n] 
...etc...

最終的版本情況:

$ type python python2 python3 pip pip2 pip3
python is /usr/bin/python
python2 is hashed (/usr/bin/python2)
python3 is hashed (/usr/bin/python3)
pip is /usr/bin/pip
pip2 is /usr/bin/pip2
pip3 is /usr/bin/pip3

$ pip --version 
pip 8.1.1 from /usr/lib/python2.7/dist-packages (python 2.7)

$ pip3 --version 
pip 8.1.1 from /usr/lib/python3/dist-packages (python 3.5)

$ python2 -m pip --version 
pip 8.1.1 from /usr/lib/python2.7/dist-packages (python 2.7)

$ python3 -m pip --version 
pip 8.1.1 from /usr/lib/python3/dist-packages (python 3.5)

第五種辦法

簡單有效的方法:

sudo add-apt-repository ppa:fkrull/deadsnakes-python2.7
sudo apt-get update 
sudo apt-get install python2.7

來自這個網頁here的最後一個答案

其他解決思路

也可以通過pyenv進行下載並安裝

#Install Pyenv
git clone https://github.com/pyenv/pyenv.git ~/.pyenv
export PATH=~/.pyenv/bin:/usr/local/hadoop/bin/:$PATH
echo 'export PYENV_ROOT="~/.pyenv"' >> ~/.bash_profile
echo 'export PATH="$PYENV_ROOT/bin:$PATH"' >> ~/.bash_profile
echo 'eval "$(pyenv init -)"' >> ~/.bash_profile

#Install Python
pyenv install 2.7.8
pyenv global 2.7.8

pyenv install 3.4.5
pyenv global 3.4.5

本文由ubuntuer整理自

本文由《純淨天空》出品。文章地址: https://vimsky.com/zh-tw/article/3577.html,未經允許,請勿轉載。