当前位置: 首页>>技术问答>>正文


在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/article/3577.html,未经允许,请勿转载。