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

moh*_*ads 185 python software-installation

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

这是正确的安装方法吗?

./configure
make
make install
Run Code Online (Sandbox Code Playgroud)

如果没有,我该怎么做?

Ach*_*chu 203

首先,安装一些依赖项:

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
Run Code Online (Sandbox Code Playgroud)

然后使用以下命令下载:

version=2.7.13
cd ~/Downloads/
wget https://www.python.org/ftp/python/$version/Python-$version.tgz
Run Code Online (Sandbox Code Playgroud)

解压并进入目录:

tar -xvf Python-$version.tgz
cd Python-$version
Run Code Online (Sandbox Code Playgroud)

现在,使用您刚刚尝试过的命令checkinstall进行安装,如果需要,使用它可以更轻松地卸载:

./configure
make
sudo checkinstall
Run Code Online (Sandbox Code Playgroud)

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

  • 使用 `sudo make install` 而不是 `altinstall` 将其设置为默认的 python 版本 (20认同)
  • 对“如何安装 Python”这个简单问题的最佳答案会带有如此可怕的警告,这非常不寻常,就像“这是我们所知道的安装 Python 的最佳方式,它很可能会破坏您的系统”……这也是非常令人惊讶的是,官方 Python 网站有大量下载,而没有一页安装说明。 (11认同)
  • pat -- 简单问题“如何安装 python”的最佳答案是`sudo apt-get install python`,更复杂的答案是“如何安装 _latest_ python”,其含义是“来自源代码”。除非您从第三方安装预编译的二进制文件,否则您在任何操作系统上都会遇到同样的问题,这在任何操作系统上都有风险(并且在不同的操作系统之间差异很大)。这个答案也是从 2012 年开始的;有关更多最新建议,请参阅 https://askubuntu.com/questions/101591/how-do-i-install-the-latest-python-2-7-x-or-3-x-on-ubuntu/831075# 831075 (4认同)
  • 按照上面的说明安装后,我在控制台中运行哪些命令来检查它是否确实安装了? (2认同)

小智 186

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

sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update
sudo apt-get install python2.7
Run Code Online (Sandbox Code Playgroud)

也可以使用其他版本,例如python2.4python3.6等。

  • +1 :) `真的很想自己编译它` (17认同)
  • 程序说明:如果您使用的是 10.04,则可能需要在使用 add-apt-repository 之前安装 python-software-properties。 (13认同)
  • 漏提一下: 1. `ppa` 没有得到官方认可,所以最安全的方式是从源代码构建。2. 这将导致一个全局 python,非`sudo`ers 不能修改或安装包,而不是本地 python。3. 可以通过从源代码构建的方式将多个python安装在本地不同的位置。 (5认同)
  • 建议在本地环境中构建 python,而不是从预构建的二进制文件中安装它 (3认同)
  • `apt-get install python2.7`这个安装二进制python2.7而不是python...丑!! (2认同)

mic*_*ael 26

Continuing to document this for the latest Ubuntu releases1 : for Ubuntu 16.04.1 server, the default Python is version 3.5, and Python 2.7 is not installed by default. On a fresh install (note that there's not even a python executable):

$ 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>
Run Code Online (Sandbox Code Playgroud)

Note: before continuing, you will probably want to do a quick sudo apt-get update, sudo apt-get upgrade, and sudo apt-get dist-upgrade (please do note exactly what these commands are in fact doing; I'm assuming a fresh install here.)

Installing python 2.7 is as easy as:

$ sudo apt-get install python2.7
Run Code Online (Sandbox Code Playgroud)

The initial output of installing python 2.7 is as follows:

$ 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...
Run Code Online (Sandbox Code Playgroud)

After installing 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
Run Code Online (Sandbox Code Playgroud)

But there's still a problem, since you can't yet install PyPI modules via pip -- e.g., if you want jupyter notebook, or the latest scipy or numpy (etc), you'll want to install pip and then pip install those, and still turning to apt-get to install any needed system dependencies, like graphviz or core system libraries.

$ 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
Run Code Online (Sandbox Code Playgroud)

So to install pip, again, it's as easy as 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
Run Code Online (Sandbox Code Playgroud)

You'll need both python-pip for the Python 2.7 pip and the python3-pip for the Python 3 pip. The installation via apt-get is sure to install the required dependencies; e.g, here's the output for installing 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...
Run Code Online (Sandbox Code Playgroud)

An interesting thing happens as a result of this: you now have the "standard" (and PEP recommended) python2 and python3 (which are just symlinks to python 2.7 and 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
Run Code Online (Sandbox Code Playgroud)

You'll also want to sudo apt-get install python3-pip; before you install, you have:

$ 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
Run Code Online (Sandbox Code Playgroud)

After installing 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...
Run Code Online (Sandbox Code Playgroud)

The resulting versions:

$ 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)
Run Code Online (Sandbox Code Playgroud)

And one last thing before you can go and start installing all your favorite python PyPI modules: you'll probably have to upgrade pip itself (both pip2 and pip3, separately; also, it doesn't matter if pip is invoked via the python executables or the pip executables, the actual upgrades are stored in /usr/lib):

$ sudo -H python2 -m pip install --upgrade pip
...
$ sudo -H python3 -m pip install --upgrade pip
...
Run Code Online (Sandbox Code Playgroud)

You can now run either the stand-alone pip or the version bundled within python (via 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 repos &apt转移到 PyPI & 中pip。)

  • 按照 Ubuntu 16.04.1 LTS 上的说明成功设置 python 2.7 和 pip,如下所示: sudo apt-get install python2.7; sudo apt-get install python-pip; sudo -H python2 -m pip install --upgrade pip; # 检查版本:$ pip --version -&gt; pip 9.0.1 from /usr/local/lib/python2.7/dist-packages (python 2.7); $ python --version -&gt; Python 2.7.12 (2认同)

小智 11

12.04

如果您遵循Achu 的回答,则该术语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
Run Code Online (Sandbox Code Playgroud)


小智 5

也可以通过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
Run Code Online (Sandbox Code Playgroud)

  • 关于 pyenv 的两个警告:(1)它只能从 bash shell(或者可能是 zsh,但肯定不是 dash,它在 Ubuntu 上是 /bin/sh),并且(2)它需要一个登录 shell(例如`bash --login `),这并不总是容易实现,例如从 Ansible。最适合交互式使用,不太适合脚本服务器。 (2认同)