问题描述
我想在Ubuntu上安装最新的Python tarball,从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
解压并进入目录:
tar -xvf Python-$version.tgz
cd Python-$version
现在,使用刚刚尝试的命令安装,使用checkinstall
,以便在需要时轻松卸载:
./configure
make
sudo checkinstall
将version
更改为您需要的版本(例如,version=2.7.1
或version=3.6.0
)。
次佳解决方法
除非你真的渴望自己编译它,否则首选的方法是使用DeadSnakes PPA来安装默认不包含的Python版本:
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update
sudo apt-get install python2.7
其他版本,例如python2.4
或python3.6
等也可用。
第三种解决方法
继续记录最新的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 update
,sudo apt-get upgrade
和sudo apt-get dist-upgrade
(请确切注意这些命令实际上在做什么;我假设在此处进行全新安装)。
安装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
但是仍然存在问题,因为您还不能通过pip安装PyPI模块 – 例如,如果您想要jupyter notebook,或者最新的scipy或numpy(etc),则需要安装pip然后点安装,仍然转向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 pip
的python3-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推荐)python2
和python3
(它们只是符号链接到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
...
您现在可以运行stand-alone 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的“最新版本问题”已经部分地从操作系统回购& apt
和PyPI& pip
。)
第四种方法
12.04
如果您遵循Achu’s answer,则术语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
第五种方法
您也可以通过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