256

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
  • 207,228
AgileZebra
  • 2,663

12 Answers12

319

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.

Nephente
  • 5,875
68

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
47

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,747
28

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.

Tested on Ubuntu 18.04, pyenv 1.2.15.

Conda

I'm not a huge fan of conda's bloatedness, but for better or worse it has become a popular way to manage Python versions and prebuilt shared libraries.

Install miniconda on Linux with:

mkdir -p ~/miniconda3
wget https://repo.anaconda.com/miniconda/Miniconda3-latest-Linux-x86_64.sh -O ~/miniconda3/miniconda.sh
bash ~/miniconda3/miniconda.sh -b -u -p ~/miniconda3
rm ~/miniconda3/miniconda.sh

and add this to your .bashrc:

PATH="$PATH:$HOME/miniconda3/bin"

then to create a new environment for a project with a specific Python version:

conda create -y -n mytest3.9 python=3.9

and then for each shell where you want to use it in:

eval "$(command conda 'shell.bash' 'hook' 2> /dev/null)"
conda activate mytest3.9

then:

python --version

gives:

Python 3.9.21

and:

which python

gives:

/home/ciro/miniconda3/envs/mytest3.9/bin/python

It also ships its own pip:

which pip

giving:

/home/ciro/miniconda3/envs/mytest3.9/bin/pip

from which you can pip install as in a virtualenv.

You can also let conda modify your .bashrc automatically for you with:

conda init

but unfortunately the code it adds automatically puts you into Conda on every shell which I don't like, so I prefer to do the eval manually per shell as mentioned at: https://stackoverflow.com/questions/55507519/python-activate-conda-env-through-shell-script

Tested on conda 25.1.1, Ubuntu 24.10.

Ultimate mega master compatibility table

OK let's do this:

          Python 3.x
        +-----------------+
        | 13 12 11 10 9 8 |
+-------+-----------------+
| 25.04 |  D  M           |
| 24.10 |  M  D           |
| 24.04 |     D           |
| 22.04 |        M  D     |
| 20.04 |             M D |
+-------+-----------------+

Search for official PPAs

Installing python: who is deadsnakes and why should I trust them? asks if the popular deadsnakes PPA is trustworthy. While it is not maintained by Canonical employees directly (huge missed opportunity!), at least one of the developers, Anthony Sottile, has a video of hiself presenting the project: https://www.youtube.com/watch?v=Xe40amojaXE So at least we know that if it is a SCAM we have at least one American head to go after which gives it some confidence.

Related threads

5

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.

4

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,752
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
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,610
  • 2
  • 42
  • 52
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
  • 22,082
-1

Another modern alternative that does not use the system package manager is uv, a Python packager manager written in Rust.

uv allows to easily install different Python versions, e.g., Python 3.12 can be installed using

uv python install 3.12
-1

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
  • 30,112
-2

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