233

How can I install a different version of Python using apt-get?

Obviously I realise I can install using the source tar ball, however I would prefer not to install from source and instead use the package manager, as that's what it's there for. Surely somewhere reputable builds .deb files for the latest Python releases (why python.org don't is beyond me) that I can reference.

What do I need to do to reference them and what issues might it create when upgrading to the next version?

If there is no way except for building from source, is there a (pseudo) package that I can can install that will provide all of the dependencies needed without having to find and install each individually? So that I don't get:

The necessary bits to build these optional modules were not found:
_bz2                  _curses               _curses_panel      
_dbm                  _gdbm                 _lzma              
_sqlite3              _ssl                  _tkinter           
readline              zlib  
muru
  • 197,895
  • 55
  • 485
  • 740
AgileZebra
  • 2,433
  • 4
    http://askubuntu.com/questions/674586/python-3-5-has-just-been-released-can-i-get-it-as-a-package-for-ubuntu-15-4 – h0ch5tr4355 Oct 08 '15 at 06:12
  • did you ever get E: Couldn't find any package by regex 'python3.5' and thus failed to install via apt-get install python3.5? – Charlie Parker Dec 16 '16 at 07:06
  • similar question: http://askubuntu.com/questions/310441/how-do-i-install-the-latest-python-version-in-ubuntu – Charlie Parker Jan 16 '17 at 22:22
  • some good suggestions here: https://www.quora.com/Why-is-it-very-difficult-to-install-Python-3-5-for-Ubuntu – Charlie Parker Jan 18 '17 at 00:19
  • related question, seems simpler but can't get it to work: http://askubuntu.com/questions/798123/how-do-i-install-python-3-5-2/811935?noredirect=1#comment1372828_811935 – Charlie Parker Feb 08 '17 at 17:14
  • another related question: http://askubuntu.com/questions/517006/is-it-possible-to-install-python-3-x-in-14-04-lts-or-upgrade-from-python-2-7-6?noredirect=1&lq=1 – Charlie Parker Feb 08 '17 at 17:15
  • 1
    @CharlieParker Did you try installing Anaconda ? – M. Becerra Feb 08 '17 at 18:44
  • @M.Becerra not yet, that was the next thing I was going to try. But the caveat is that I am using all this under a docker container, so I wanted to leave such a solution until the end, unless I really needed it. – Charlie Parker Feb 08 '17 at 19:29
  • this answer should be useful because the current accepted answer seems to provide a version of python without pip. Check out how to fix that here: http://stackoverflow.com/questions/42122639/how-does-one-install-fix-a-failed-numpy-installation-that-works-on-python-3-4-bu/42124828?noredirect=1#comment71418080_42124828 – Charlie Parker Feb 08 '17 at 22:54
  • @M.Becerra I tried anaconda, it seems to work fine. Provided an answer here too if you care to see it. Thanks! – Charlie Parker Feb 09 '17 at 20:24
  • I found none of these answers satisfactory. None mentions the use of update-alternatives

    Follow this link https://www.itsupportwale.com/blog/how-to-upgrade-to-python-3-7-on-ubuntu-18-10/ to upgrade python with update-alternatives

    – goonerify Dec 04 '20 at 09:04
  • I wouldn't use update-alternatives. I would like to just install a Python binary under it's long name (including the version number) and then use that path in a venv installation so that inside that environment the python that's available is whatever version that was. I don't think it's a good idea to move your current system version of Python around. This is the approach that the accepted answer supports. – NeilG Jun 20 '23 at 07:48
  • @AgileZebra can you check my answer, based on standard Python practice and PPF -not recommended, and you set it as proper answer – zhrist Aug 17 '23 at 08:18

11 Answers11

292

Felix Krull runs a PPA offering basically any version of Python (seriously, there is 2.3.7 build for vivid...) for many Ubuntu releases at https://launchpad.net/~deadsnakes/+archive/ubuntu/ppa.

Do the usual:

sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update
sudo apt-get install python3.5

It will not overwrite your existing python3.4 which is still symlinked as python3.

Instead, to run python3.5, run the command python3.5 (or python3.X for any other version of python).


DON'T change the symlink! There are apparently many system functions that don't work properly with python3.5.

I tried this and afterwards couldn't open a terminal, software updater,...

cd /usr/bin
sudo rm python3

The upgrade to Wily will adapt the meta-package python3 to point to python3.5. I don't expect any breakage, but at this point the foreign repository is not needed anymore. So to be really safe, you can purge the PPA before doing the upgrade.

Archisman Panigrahi
  • 28,338
  • 18
  • 105
  • 212
Nephente
  • 5,595
  • 1
  • 17
  • 23
  • 11
    for noob's out there, don't symlink! also not symlinking just means you need to type python3.5 from the command line to run python 3.5 – Chris Hawkes Jan 24 '16 at 01:18
  • 6
    Could you point me, how can I use 3.4's pip with this 3.5? – Groosha Apr 14 '16 at 18:38
  • 2
    @kondra007 I followed instructions from https://pip.pypa.io/en/stable/installing/ (please read the warning there) and these two commands wget https://bootstrap.pypa.io/get-pip.py; sudo python3.5 get-pip.py gave me a working pip for python 3.5 but pip3.4 is not working any more. If anyone knows better please comment – ndemou Jul 24 '16 at 12:55
  • 4
    Also to set python3.5 as the default ~$ vim ~/.bashrc and add this alias python=python3.5. then ~$source ~/.bashrc – Krishnadas PC Oct 16 '16 at 04:01
  • 2
    Tips: if add-apt-repository: command not found, run apt-get install software-properties-common python-software-properties to install – Mithril Dec 16 '16 at 01:44
  • weird, for some reason I get that it can't find python 3.5 to install, E: Couldn't find any package by regex 'python3.5' so it doesn't even install it. – Charlie Parker Dec 16 '16 at 07:04
  • do you mind maybe clarifying or explaining a bit more what the warning means? does it mean your second part of your answer is a really bad idea and we need a new answer for this question? – Charlie Parker Jan 17 '17 at 00:54
  • It seems that the issue right now is that it doesn't come with pip. Does someone know why thats the case and how to fix it? – Charlie Parker Feb 08 '17 at 21:58
  • deadsnakes was updated more recently than this post (2017-01-13) ;) – Antony Hatchkins Apr 20 '17 at 16:41
  • Don't forget to install python3.5-dev in order to build packages for this version. – Caumons Nov 30 '17 at 11:25
  • 1
    Any way to install pip packages such as numpy using this approach? – xjcl Aug 17 '20 at 09:47
  • Can I also support specific Python sub-versions in this way, e.g., Python 3.8.5 instead of 3.8.6? – stefanbschneider Mar 02 '21 at 20:11
  • add-apt-repository: command not found – Shreyan Mehta May 26 '21 at 15:52
  • how to install an exact version like 3.5.x? This method won't allow doing that! – shaik moeed Feb 21 '23 at 11:36
  • @CGFoX Are you able to find a way to do this? – shaik moeed Feb 21 '23 at 11:37
62

This Youtube link helped me to install it.

The steps are:

sudo apt-get install libssl-dev openssl
wget https://www.python.org/ftp/python/3.5.0/Python-3.5.0.tgz
tar xzvf Python-3.5.0.tgz
cd Python-3.5.0
./configure
make
sudo make install

To check if python is installed type python3.5 else:

sudo ln -fs /opt/Python-3.5.0/Python /usr/bin/python3.5
  • 10
    OP said he didn't want to compile from source – Michael Bates Feb 09 '16 at 03:01
  • 5
    Yes you are right. Also compiling from source might introduce some dependency issues further down the road. But that is how I resolved it. If there is a better way I would definitely want to know. – joydeep bhattacharjee Feb 10 '16 at 07:04
  • There is slightly change in the forth command, it should be cd Python-3.5.0 instead of cd Python-3.5.0.tgz. – Imran Jun 21 '16 at 05:26
  • 7
    Consider ./configure --enable-optimizations http://stackoverflow.com/questions/41405728/what-does-enable-optimizations-do-while-compiling-python – warvariuc Jan 11 '17 at 07:08
  • why is this such a long series of commands to do such a simple thing like install a more updated version of python 3? – Charlie Parker Jan 16 '17 at 22:19
  • 1
    @CharlieParker because Python isn't made for a specific OS. It's meant to be run at any platform. For Linux this means that it either each version has to be compiled and distributed for each version of each Linux distro (of which there are many), or they distribute the source and let the user compile it themselves in the environment in which it will be used. The latter is far more feasible. – JimmyMcHoover Sep 03 '17 at 11:24
  • 4
    You also need c/c++ compiler from apt-get install build-essential – bato3 Oct 05 '17 at 13:58
  • You will want to add "apt-get install make gcc" to the above list. – scott Nov 11 '17 at 04:31
  • i got a zlib dependency issue with make install so I just installed all these and it worked https://github.com/pyenv/pyenv/wiki/common-build-problems – Gabe Rogan Mar 25 '20 at 14:44
  • Warning: make install can overwrite or masquerade the python3 binary. make altinstall is therefore recommended instead of make install since it only installs exec_prefix/bin/pythonversion https://docs.python.org/3/using/unix.html#building-python. – user149244 Apr 27 '22 at 12:13
  • If you understood what it is doing, @CharlieParker, you'd realise it's the shortest sequence of commands that you would want to do this job. I suspect you're used to using one of those GUI oriented operating systems that gained market share through criminal activity and maintains it by keeping the systems engineers that use it dumb by offering it's own certificated courses that non-technical management staff think are good. – NeilG Jun 20 '23 at 08:01
44

Just follow this steps (tested) :

Step 1 – Install Required Packages

Use the following command to install prerequisites for Python before installing it.

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

Step 2 – Download Python 3.5.2

Download Python using following command from python official site. You can also download latest version in place of specified below.

cd /usr/src
sudo wget https://www.python.org/ftp/python/3.5.2/Python-3.5.2.tgz

Now extract the downloaded package.

sudo tar xzf Python-3.5.2.tgz

Step 3 – Compile Python Source

Use below set of commands to compile python source code on your system using altinstall.

cd Python-3.5.2
sudo ./configure
sudo make altinstall

make altinstall is used to prevent replacing the default python binary file /usr/bin/python.

Step 4 – Check the Python Version

Check the latest version installed of python using below command.

$ python3.5 -V

Python 3.5.2

Source.

Bilal
  • 3,699
  • 7
    +1 for using altinstall by default. There's no much real scenarios where you can live just with one version. Even of systems with Python3 as default you may be forced to install Python as reversed -> Python2 instead or adding Python2. – m3nda May 25 '17 at 15:41
  • 1
    I got an error as described here - https://bugs.python.org/issue31652 had to run sudo apt-get install libffi-dev to resolve it. – Nishan Aug 13 '19 at 17:59
  • I would also add sudo apt-get install liblzma-dev to enable lzma support. – v.grabovets Sep 02 '19 at 10:16
  • good guide, I think the sudos for the tar and configure commands are not necessary – mtdb Jul 13 '21 at 17:02
  • 1
    best and more complete answer for sure. – Kenny Aires Aug 09 '21 at 16:13
  • Very good my friend, you helpet me alot – Allamo Olsson Oct 21 '21 at 16:48
  • where is the python3.11 installed ? – pippo1980 Nov 29 '22 at 07:13
  • As @warvariuc wrote: Consider ./configure --enable-optimizations to build an optimized version of the interpreter based on workload tests. You may also add --with-lto option for "Link Time Optimization". In order to compile at the full speed you may run make -j: "If the -j option is given without an argument, make will not limit the number of jobs that can run simultaneously" – bartolo-otrit Dec 22 '22 at 09:45
  • My dependencies for Ubuntu: libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev xz-utils tk-dev libffi-dev liblzma-dev libgdbm-dev libgdbm-compat-dev My configuration: ./configure CPPFLAGS="-I/usr/include/openssl" LDFLAGS="-L/usr/lib/x86_64-linux-gnu" --enable-optimizations --with-lto – bartolo-otrit Dec 22 '22 at 10:25
23

pyenv

This method does not use apt-get, but it is, I believe, the best option available today, as it can easily compile any Python version from source for you, so you don't have to rely on any PPAs.

https://github.com/pyenv/pyenv

Pyenv allows you to manage multiple Python versions without sudo for a single user, much like Node.js NVM and Ruby RVM.

Install Pyenv:

curl https://pyenv.run | bash

Then add to your .bashrc:

export PATH="${HOME}/.pyenv/bin:$PATH"
eval "$(pyenv init -)"
eval "$(pyenv virtualenv-init -)"

Find Python version to install:

pyenv install --list

Install the python version you want:

# Increase the chances that the build will have all dependencies.
# https://github.com/pyenv/pyenv/wiki/Common-build-problems
sudo apt build-dep python3
sudo apt-get install -y make build-essential libssl-dev zlib1g-dev libbz2-dev \
  libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev \
  xz-utils tk-dev libffi-dev liblzma-dev python-openssl git

Build and install a Python version from source.

pyenv install 3.8.0

List available Python versions:

pyenv versions

We now have:

* system (set by /home/cirsan01/.pyenv/version)
  3.8.0

Select a different python version:

pyenv global 3.8.0
python --version
python3 --version

Both output:

Python 3.8.0

We can now proceed to install and use packages normally:

pip install cowsay
python -c 'import cowsay; cowsay.tux("Python is fun")'
cowsay 'hello'

We can confirm that everything is locally installed in our clean environemnt with:

python -c 'import cowsay; print(cowsay.__file__)'
which cowsay

We see that which python points to:

~/.pyenv/shims/python

because ~/.pyenv/shims is prepended to PATH with the rc scripts.

TODO understand further. The global version is somethow determined by:

~/.pyenv/version

which now contains:

3.8.0

Per project usage

In the previous section, we saw how to use pyenv in a global setup.

However, what you usually want is to set a specific python and package version on a per-project basis. This is how to do it.

First install your desired Python version as before.

Then, from inside your project directory, set the desired python version with:

pyenv local 3.8.0

which creates a file .python-version containing the version string.

And now let's install a package locally just for our project: TODO: there is no nice way it seems: https://stackoverflow.com/questions/30407446/pyenv-choose-virtualenv-directory/59267972#59267972

Now, when someone wants to use your project, they will do:

pyenv local

which sets the Python version to the correct one.

Related threads:

Tested on Ubuntu 18.04, pyenv 1.2.15.

6

As far as I can tell, at least in a docker container, one can definitively apt-get python 3.

First I ran into a ubuntu container with container:

docker run -it --rm ubuntu:latest bash

then for some reason it needed to update some ubuntu stuff so I did (inside the container):

apt-get update && apt-get install -y build-essential git libjpeg-dev

and then I simply installed python3 and it seems it automatically got python 3.5:

apt-get install python3
apt-get install python3-pip

and to test if pip works lets download something:

pip3 install tensorflow

all seems to work fine for me.

Important Note: it seems that if you already have python 3.4 installed then apt-get install python3 does not work because it says you already have it. It seems that was one of my problems because I was starting from a docker image from tensorflow (in particular gcr.io/tensorflow/tensorflow:latest-devel-py3) and something in that image (I assume its that they already have python 3.4 but it might something else) didn't allow me to update my python to get python 3.5.


credit: I discovered this when I asked the following: https://stackoverflow.com/questions/42122826/can-one-use-python-3-5-in-a-docker-container-based-out-of-the-tensorflow-docker


Note: one can also just install Anaconda. For a docker example one can do:

docker pull continuumio/anaconda3
docker run -i -t continuumio/anaconda3 /bin/bash

from their official website: https://hub.docker.com/r/continuumio/anaconda3/

This solution installs python 3.6 but I am sure if you look into it there you can get python 3.5 if thats what you want.

Note: you should probably be using a virtual environment like virtual env or conda/anaconda anyway unless your using docker anyway. Just wanted to remind people.

Charlie Parker
  • 425
  • 1
  • 5
  • 11
  • 2
    "in a docker container" is a very generic statement: it all depends which image the container is based on, for example which version of Ubuntu. Moreover, there can be minor versions like 3.5 or 3.6 that are not available in one Ubuntu version (14.04 for example) – giorgiosironi Sep 12 '17 at 08:28
3

There are a number of newer python distributions available to install via apt-get listed in Ubuntu Packages

as an example the following versions are currently available:

python2.7

python3.2

python3.4

python3.5

python3.6

Availability varies with Ubuntu release to a degree. For example 3.5 is available for Xenial, Yakkety, and Zesty and 3.6 is available for Yakkety and Zesty but you must enable the Universe repository if not enabled to obtain them via apt-get. To check availability for your version of Ubuntu check the links above.

A quick peek indicates that this answer is also still valid as that PPA has 3.6 even for Trusty.

Elder Geek
  • 36,023
  • 25
  • 98
  • 183
2

I disagree with accepted answer, as the PPA is totally not needed not recommended for python.

Answer is as simple as:

apt-get update && apt-get install -y \
python3.9 \
python3-pip
zhrist
  • 121
  • 2
0

This is an update to Nephente's answer (i.e. the top-ranked) as of 2019-10 (with excerpts from there used under CC BY-SA 4.0):

Felix Krull runs a PPA offering many versions of Python for many Ubuntu releases at https://launchpad.net/~deadsnakes/+archive/ubuntu/ppa. Not all combinations are supported though. If your desired combination is not supported, but your desired Python version is available there for an older Ubuntu release, you can often still install it as follows (here for the example of Python 3.5):

sudo add-apt-repository "deb http://ppa.launchpad.net/deadsnakes/ppa/ubuntu $(lsb_release -sc) main"
sudo apt-get update
sudo apt-get install python3.5 python3.5-venv

It will not overwrite your existing Python 3.x, which is still symlinked as python3 afterwards. The hack to install packages from an older Ubuntu release is from here. I just used this to install Python 3.5 under Ubuntu 19.04.

tanius
  • 6,303
  • 1
  • 39
  • 49
0

Some great answers here with alternatives for almost every use-case (install from source, PPA's, pyenv, Docker, Conda). Only a few of them technically answer the question's explicitly stated apt-get use-case, but the orthogonal answers are helpful to other readers (like me) as well.

@CharlieParker's Docker-based answer pointed me in the right direction for my use-case and is greatly appreciated. However, as noted in the comments, you are still limited to apt-get installing a version of Python in the container based on the Ubuntu release of the container.

As an alternative, there are official Docker images for each currently supported Python release (as well as the last 2.x release). These are preinstalled in a working environment, with pip included.

Simply docker pull python:3.7, for instance.

The default images are Debian based, so you can still apt install other dependencies into them as needed.

Note that they are fairly hefty at nearly 1GB in size. There are also official images in both -slim and -alpine (musl libc-based) versions that come in around 125GB and 6GB, respectively.

See the Python Docker repo for a complete list and details.

NotTheDr01ds
  • 17,888
0

Conda is actively updated and allows you to install multiple python versions in managed venvs without the tedium of setting it all up yourself. Binary extension pathing problems may have been solved in many of the anaconda managed dependency/pip chains it uses.

RobotHumans
  • 29,530
-1

I would consider using virtual environments instead of installing python versions via apt-get.

Using virtual environments, see here, are a good practice for developing with python. They let you isolate your python environment from the system installed python versions.

In addition you don't have to get sudo access while installing any library (via pip etc.).

  • 12
    I think you need to install the version of python you want before you can use it in a virtual environment. – Nzbuu Jul 09 '16 at 11:15
  • 1
    You install the desired version of python in the virtual environment, than you change your python related path by just running the "activate" script in virtual environment. – devrimbaris Jul 12 '16 at 07:16
  • This is actually exactly what I want to do—the very first thing I tried doing after following @Nephente's answer was point to it using virtualenv -p. But it blew up with "ImportError: cannot import name 'HTTPSHandler'". – Michael Scheper Nov 02 '16 at 18:51
  • 1
    why was this downvoted? virtual envs usually work fine. – Charlie Parker Dec 16 '16 at 07:05
  • 1
    how do you install a virtual env in ubuntu (as in your suggestion) but with python 3.5? – Charlie Parker Jan 17 '17 at 00:16
  • @CharlieParker You can check the official guide, https://virtualenv.pypa.io/en/stable/installation/ – devrimbaris Jan 17 '17 at 13:05