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

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

我想在 Ubuntu 上安装最新的 Python tarball,从以下网址下载http://python.org/download/

这是正确的安装方法吗?

./configure
make
make install

如果没有的话,我该怎么做?

答案1

首先,安装一些依赖项:

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

解压并进入目录:

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

现在,使用您刚刚尝试的命令进行安装,使用checkinstall以便在需要时更轻松地卸载:

./configure
make
sudo checkinstall

更改version为您需要的任何版本(例如version=2.7.1或)。version=3.6.0

答案2

除非你真的很想自己编译,否则首选方法是使用DeadSnakes PPA安装默认情况下不包含的 Python 版本:

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

还有其他版本,如python2.4python3.6等也可用。

答案3

继续记录最新的 Ubuntu 版本1:对于 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>

注意:在继续之前,您可能需要快速执行sudo apt-get updatesudo apt-get upgradesudo apt-get dist-upgrade(请注意这些命令实际上在做什么;我假设这里是全新安装。)

安装 python 2.7 非常简单:

$ sudo apt-get install python2.7

安装python2.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...

安装python2.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

但还有一个问题,因为你还不能通过安装 PyPI 模块pip——例如,如果你想要 jupyter notebook,或者最新的 scipy 或 numpy(等等),你需要先安装这些,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-pipPython 2.7pippython3-pipPython 3 的版本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...

由此产生了一件有趣的事情:你现在有了“标准”(和 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

您还需要sudo apt-get install python3-pip;在安装之前,您需要:

$ 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)

在开始安装所有您喜欢的 python PyPI 模块之前,还有最后一件事:您可能必须升级 pip 本身(pip2 和 pip3,分别升级;此外,无论是pip通过python可执行文件还是pip可执行文件调用,实际的升级都存储在中/usr/lib):

$ sudo -H python2 -m pip install --upgrade pip
...
$ sudo -H python3 -m pip install --upgrade pip
...

您现在可以运行独立版本pip或捆绑版本python(通过python -m pip {command})。


[1]历史回顾:旧版 Ubuntu 只有 Python 2.6,因此有各种方法可以安装 Python 2.7+。后来,在 Python 2.7 添加到公共存储库后,我们仍然面临着同样的挑战,即安装最新的 Python 2.7 和最新修复,这(太)频繁了。今天的情况好多了/简单多了:现在公共存储库中的当前 Python 2.7 和 3.5(基本上是人们关心的仅有的两个 Python 平台版本)非常稳定,所以现在我们只需要担心安装最新的 Python模块,不是最新的Python。因此,Python“最新版本问题”已转移部分地离开 OS 存储库 &apt并进入 PyPI & pip。)

答案4

您也可以通过以下方式下载并安装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

相关内容