I want to install the latest Python tarball on Ubuntu, downloaded from http://python.org/download/.
Is this is a correct way to install?
./configure
make
make install
If not, how do I do that?
I want to install the latest Python tarball on Ubuntu, downloaded from http://python.org/download/.
Is this is a correct way to install?
./configure
make
make install
If not, how do I do that?
First, install some dependencies:
Then download using the following command:
Extract and go to the directory:
Now, install using the command you just tried, using
checkinstall
instead to make it easier to uninstall if needed:Change
version
to whichever version you need (version=2.7.1
orversion=3.6.0
, for example).Unless you really have a burning desire to compile it yourself, the preferred way is to use the DeadSnakes PPA to install versions of Python that aren't included by default:
Other versions, such as
python2.4
orpython3.6
, etc. are also available.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):Note: before continuing, you will probably want to do a quick
sudo apt-get update
,sudo apt-get upgrade
, andsudo 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:
The initial output of installing python 2.7 is as follows:
After installing python 2.7,
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 installpip
and thenpip install
those, and still turning toapt-get
to install any needed system dependencies, like graphviz or core system libraries.So to install pip, again, it's as easy as
sudo apt-get install python-pip
:You'll need both
python-pip
for the Python 2.7pip
and thepython3-pip
for the Python 3pip
. The installation viaapt-get
is sure to install the required dependencies; e.g, here's the output for installing pip2:An interesting thing happens as a result of this: you now have the "standard" (and PEP recommended)
python2
andpython3
(which are just symlinks to python 2.7 and python 3.5):You'll also want to
sudo apt-get install python3-pip
; before you install, you have:After installing
pip3
,The resulting versions:
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 thepython
executables or thepip
executables, the actual upgrades are stored in/usr/lib
):You can now run either the stand-alone
pip
or the version bundled withinpython
(viapython -m pip {command}
).[1] Historical recap: older Ubuntu had only Python 2.6, thus all the various methods to get Python 2.7+ installed. Later, after Python 2.7 was added to the public repositories, we still had the same challenge to install the newest Python 2.7 with latest fixes, which was (too) frequently necessary. The situation today is much better/simpler: the current Python 2.7 & 3.5 (basically the only two Python platform versions people care about) that are now in the public repos are very stable, so now we really only have to worry about installing the latest python modules, not the latest python. So now the Python "latest version problem" has moved partly out of the OS repos &
apt
and into PyPI &pip
.)12.04
If you are following Achu's answer, then the term
libread5-dev
should be changed tolibreadline-gplv2-dev
. So the full command would be:You can also download and install it via pyenv
Mostly a mirror of this answer with a tweaked intro
I would recommend pyenv. It automates the build process aside from installing the header dependencies (see below). You can build and install a new (or old) version of Python by simply saying
pyenv install 3.6.0
. Everything runs as your user, so you don't have to worry about messing up the Python used by Ubuntu itself.As opposed to some of the apt-repo-based options (e.g. deadsnakes), it will generally work same day of a release after a
pyenv update
because you don't need to wait for someone else to package it. See all the versions you can install withpyenv install --list
Install pyenv
Install tools and headers needed to build CPythons (exotic Pythons like PyPy or Jython may have other dependencies). Git is used by pyenv, plus it also enables builds/installs of source branches, so you could install whatever 3.8 is right now, i.e. the master branch of CPython fresh off GitHub:
Run the installer script (installs pyenv and some very useful pyenv plugins by the original author; see here for more)
Add init lines to your
~/.profile
or~/.bashrc
(it mentions it at the end of the install script):Restart your shell (close & open or
exec $SHELL
) or reload the profile script. (with e.g.source ~/.bashrc
)Done!
Setting up an environment
To not touch the system Python (generally a bad idea; OS-level services might be relying on some specific library versions, etc.) make your own environment, it's easy! Even better, no
sudo
, for it orpip
installs!Install your preferred Python version (this will download the source and build it for your user, no input required)
Make it a virtualenv so you can make others later if you want
Make it globally active (for your user)
Do what you want to with the Python/pip, etc. It's yours.
If you want to clean out your libraries later, you could delete the virtualenv (
pyenv uninstall general
) or make a new one (pyenv virtualenv 3.6.0 other_proj
). You can also have environments active per-directory:pyenv local other_proj
will drop a.python-version
file into your current folder and any time you invoke Python or pip-installed Python utilities from it or under it, they will be shimmed by pyenv.Troubleshooting
bash: pyenv: command not found
,fish: Unknown command 'pyenv'
$PATH
, there should be one entry that ends in something like.pyenv/bin
. If it's missing make sure you followed #3 AND #4 (restart your shell) under Install pyenv above.pyenv: no such command 'virtualenv'
pyenv commands
.