189

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?

muru
  • 197,895
  • 55
  • 485
  • 740
mohammads
  • 2,692
  • 2
    At one time, I imagine this was asked in order to install a newer version of python on Ubuntu. Now, it's required to install an older version of python on Ubuntu (because we still require python 2.7 for a lot of things)... tl;dr: this answer is no longer current/correct. Instead, assuming python3 is installed by default (and perhaps a minimal python2.x), then run: sudo apt-get install python-2.7 python-pip – michael Sep 20 '16 at 13:00
  • @michael_n what are you talking about? compiling from source does not make this answer incorrect or outdated. its just a different method. you can use apt to automate the process for you if needed. –  Sep 29 '16 at 01:23
  • u use what u need. I dont really see how this supports your claim of this being outdated. like you said yourself, u use it when you need to because it may not be available. For instance, windows subsystem for linux uses some dated software packages even tho the distro is ubuntu 16.04.1 (beta) and uses python2.6.2, and python3 even tho the current is python2.7.12. In thay case, I should compil if I want up-to-date packages –  Sep 29 '16 at 03:34
  • 1
    @jargonjunkie you're off-topic, but correct. If the question is "how do I install python 2.7 on ubuntu 16.04+" which it is (since 16.04 is now available, which is my point), then you simply sudo apt-get install python2.7. This is not only easier, it's the only correct answer, since it's the only way you'll be able to handle installing python modules via apt-get and their dependencies in turn. – michael Sep 29 '16 at 06:07
  • hope this helps illustrate (added answer). the python ecosystem is very particular/finicky, and you can easily get into "dependency hell" if you don't carefully construct your python environment http://askubuntu.com/a/831075/17060 – michael Sep 29 '16 at 08:54
  • you're totally right. you can easily index dependencies from the site. and actually, subsystem uses 14.04.5 and uses 2.7.2 by default (along w py3 of course). apt-get may not always be an options and its helpful to kmow that dpkg is there. this is useful for custom scenarios where these options may not be available and while not always the best choice (obviously, if u can use apt, u should use apt). I have a few custom environs which don't support a lot of debian packages, so I find myself using dpkg quite often. knowing how to use it in a pinch can be extremely valuable. –  Oct 01 '16 at 23:43

6 Answers6

205

First, install some dependencies:

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

Then download using the following command:

version=2.7.13
cd ~/Downloads/
wget https://www.python.org/ftp/python/$version/Python-$version.tgz

Extract and go to the directory:

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

Now, install using the command you just tried, using checkinstall instead to make it easier to uninstall if needed:

./configure
make
sudo checkinstall

Change version to whichever version you need (version=2.7.1 or version=3.6.0, for example).

Achu
  • 21,237
  • 20
    Use sudo make install and not altinstall to set it as default python version – Shagun Sodhani Jul 22 '13 at 14:35
  • use CXX=g++ ./configure if ./configure issues a warning saying g++ was not found – srj Nov 07 '14 at 11:55
  • 2
    after installing as instructed above, what commands do I run in the console to check that indeed it is installed? – J86 Jan 29 '15 at 10:51
  • Unable to locate package libreadline-gplv2-dev -> Seems like it has been renamed apt-cache search libreadline to get the alternatives – Pierre de LESPINAY Jul 25 '15 at 07:23
  • In response to "what commands do i run in the console to check......" Surely it is quicker to Google something like "check if python installed" for instant detailed information instead of waiting for responses? – Angry 84 Jul 30 '15 at 08:36
  • The program 'checkinstall' is currently not installed.

    :D

    – hfrmobile Dec 15 '16 at 10:00
  • python -V to check installed version. – Lukas Liesis May 17 '17 at 12:37
  • 12
    It's quite extraordinary that the best answer to the simple question "how to install Python" would carry such terrible warning, it's like "this is the best way we know to install Python and it might very well break your system"... It's also very surprising that the official Python website has loads of downloads and not a single page of install instructions. – patb May 30 '17 at 12:03
  • 4
    pat -- the best answer to the simple question "how to install python" is sudo apt-get install python, the more complicated answer is "how to install the latest python", the implication being "from source". You'll have the same issue on any OS unless you're installing precompiled binaries from a 3rd party, which, again, is risky on any OS (and varies greatly across different OS). Also this answer is from 2012; for more recent advice, see https://askubuntu.com/questions/101591/how-do-i-install-the-latest-python-2-7-x-or-3-x-on-ubuntu/831075#831075 – michael Oct 29 '17 at 05:22
  • hello pals if you want install pip along latest python use. ./configure --with-ensurepip=install instaead ./configure – christianbueno.1 Jan 11 '18 at 19:10
  • Followed this and then read the answers mentioned below. Try installing python via "sudo apt-get install python2.7" first. If it doesn't work, then try this. Something went wrong in this and took me 3 hours to fix 2 friggin' issues that shouldn't have been there had I not tried to build python myself. Still, thanks for the long answer for people who would want to build it. :) – Xonshiz May 29 '19 at 08:52
  • @achu How do I uninstall it if I did this? – Chef Pharaoh Apr 12 '20 at 15:58
186

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:

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

Other versions, such as python2.4 or python3.6, etc. are also available.

brousch
  • 2,620
  • 13
    Program note: If you're on 10.04, you may need to install python-software-properties prior to using add-apt-repository. –  Apr 09 '12 at 19:59
  • 3
    It's recommended to build python in your local environment, instead of installing it from pre-build binaries – pylover Oct 22 '13 at 01:10
  • 17
    +1 :) really have a burning desire to compile it yourself – Watt Nov 13 '13 at 22:35
  • 5
    Missed to mention that: 1. the ppa is not officially endorsed, so the most secure way is to build from source. 2. this will result in a global python, that non-sudoers cannot modify or install packages to, not a local one. 3. Multiple pythons can be installed locally at different locations by building from source. – 0 _ Aug 09 '14 at 01:06
  • @IoannisFilippidis The downside to building from source is it doesn't get tracked by apt. :( – weberc2 Jul 06 '15 at 14:14
  • 2
    apt-get install python2.7This install the binary python2.7 instead of python...ugly!! – Dhawal Dec 24 '15 at 19:35
  • Will be exposed as python2.7 command. Location of the executable is /usr/bin/python2.7 – Nitin Dec 09 '16 at 10:56
  • 1
    this might break your system when/if you go to upgrade... – dermen Apr 25 '17 at 01:04
  • It turns out to not be that hard to compile it yourself, so I think it's better to do it, because then you have one less parties to trust, which is always good – Display Name Jun 25 '17 at 14:41
  • This doesn't work for me on Ubuntu 14.04, because deadsnakes only includes Python 2.7 for the distro versions that don't have their own. So Ubuntu 14.04 remains stuck on Python 2.7.6 rather than 2.7.13. – giorgiosironi Aug 03 '17 at 09:53
  • just turned out to try on ubuntu server (AWS ec2 instance) and python2.7 does not become an available command after apt-get install. the package missed setting environment variables ? – v.oddou Mar 11 '18 at 11:13
  • ok. additional comment: though, after installing pip sudo apt-get install python-pip python starts to work for good. – v.oddou Mar 11 '18 at 11:14
  • @giorgiosironi Use a virtualenv for that. And I would recommend the same for anyone who needs a different micro version number. – wjandrea Jun 17 '19 at 18:20
28

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>

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

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...

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

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

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

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...

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

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

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...

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)

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
...

You can now run either the stand-alone pip or the version bundled within python (via python -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.)

michael
  • 2,089
  • 2
    Following this instructions on Ubuntu 16.04.1 LTS successfully setup python 2.7 and pip like this: sudo apt-get install python2.7; sudo apt-get install python-pip; sudo -H python2 -m pip install --upgrade pip; # Checking version: $ pip --version -> pip 9.0.1 from /usr/local/lib/python2.7/dist-packages (python 2.7); $ python --version -> Python 2.7.12 – Robert Lujo Mar 03 '17 at 10:55
  • Pardon my ignorance but may I know what does "hashed" mean when showing Python versions? – Failed Scientist Oct 07 '17 at 13:33
  • 1
    @TalhaIrfan It's a good question (and, if you'll allow a simplified answer): when a command is typed in bash (as w/ most shells), the cmd could be an alias, function, or executable file found in the $PATH. The env var PATH is a long list of directories with literally thousands of files therein. To speed up finding the executable, the shell uses a cached hashtable for quick lookup. It could become stale, so "hashed" lets you know that it's "remembered" this command mapped to that executable file https://unix.stackexchange.com/questions/5609/how-do-i-clear-bashs-cache-of-paths-to-executables – michael Oct 08 '17 at 07:54
  • @michael Thanks a lot! It's really useful answer: Brief and to-the-point! – Failed Scientist Oct 08 '17 at 07:57
  • Excellent Answer. Followed it to get python 2.7 installed side by side on UBUNTU 16.04 LTS machine. – rjha94 May 10 '18 at 10:20
  • Incredible answer! Thank you @michael. Before running the last two cmds 2/3 -m pip install --upgrade pip, I was getting pip --version: pip 9.0.1 from /usr/lib/python2.7/dist-packages (python 2.7) and pip3 --version: pip 9.0.1 from /usr/lib/python3/dist-packages (python 3.6). Now if run them I get Traceback (most recent call last): File "/usr/bin/pip(3)", line 9, in <module> from pip import main ImportError: cannot import name main. If I run with sudo it works as I get the version pip 18.0 from .... How do I make them work without sudo again? – Gaia Jul 28 '18 at 00:14
  • 1
    @gaia you may want to ask a separate question, since more back & forth may be required to figure it out (or if anything has changed since I wrote this up). Perhaps, first verify that both pip (2.7) & pip3 (3.x) were completely installed/upgraded, and not just the former -- i.e., sudo apt-get install python-pip python3-pip and sudo -H python2 -m pip install --upgrade pip and sudo -H python3 -m pip install --upgrade pip ...Or if it really is a permission issue (indicated by "it works with sudo"), then it's going to be a little more difficult to track down. – michael Jul 30 '18 at 08:24
11

12.04

If you are following Achu's answer, then the term libread5-dev should be changed to libreadline-gplv2-dev. So the full command would be:

sudo apt-get install libreadline-gplv2-dev libncursesw5-dev libssl-dev libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev
ThatGuy
  • 111
5

You can also download and install it via 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
Whispered
  • 183
  • This is the cleanest way to get a really up to date version of Python on an older version of Ubuntu. It includes virtualenv support as well, via the pyenv-virtualenv plugin, making it easy to install applications with different module requirements without clashes. – RichVel May 06 '17 at 15:35
  • 2
    Two warnings about pyenv: (1) it only works from a bash shell (or maybe zsh, but certainly not dash which is /bin/sh on Ubuntu), and (2) it requires a login shell (e.g. bash --login), which is not always easy to achieve e.g. from Ansible. Best for interactive use, less good for scripting servers. – RichVel May 08 '17 at 07:10
0

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 with pyenv install --list

Install pyenv

  1. 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:

    sudo apt-get install -y git
    sudo apt-get install -y build-essential libbz2-dev libssl-dev libreadline-dev \
                            libffi-dev libsqlite3-dev tk-dev
    
    # optional scientific package headers (for Numpy, Matplotlib, SciPy, etc.)
    sudo apt-get install -y libpng-dev libfreetype6-dev    
    
  2. Run the installer script (installs pyenv and some very useful pyenv plugins by the original author; see here for more)

    curl -L https://github.com/pyenv/pyenv-installer/raw/master/bin/pyenv-installer | bash
    
  3. Add init lines to your ~/.profile or ~/.bashrc (it mentions it at the end of the install script):

    export PATH="~/.pyenv/bin:$PATH"
    eval "$(pyenv init -)"
    eval "$(pyenv virtualenv-init -)"
    
  4. 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 or pip installs!

  1. Install your preferred Python version (this will download the source and build it for your user, no input required)

    pyenv install 3.6.0
    
  2. Make it a virtualenv so you can make others later if you want

    pyenv virtualenv 3.6.0 general
    
  3. Make it globally active (for your user)

    pyenv global general
    
  4. 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'

    1. Check your $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'

    1. If you didn't use the installer script, you likely only installed the root pyenv package. See pyenv-virtualenv for instructions to add the plugin
    2. If you used the installer script, check if it shows up with pyenv commands.
Nick T
  • 2,483