34

Ubuntu 14.4 comes with Python 2.7.6 built-in, but I'd like to upgrade it to 2.7.9 (it has security features that I want).

Is that even possible? If so, how can this be achieved?

moomima
  • 497
  • 2
  • 5
  • 8
  • 2
    The upstream version of python 2.7 is always going to be 2.7.6. Check the distribution version number, and then check the changelog (http://packages.ubuntu.com/source/trusty/python2.7) to see if those security updates have been backported. – Roger Sherman Feb 19 '15 at 14:30

6 Answers6

31

ppa:fkrull/deadsnakes is the latest version of python2.7

sudo add-apt-repository ppa:fkrull/deadsnakes
sudo apt-get update
sudo apt-get upgrade

It will upgrade python to 2.7.10

Like
  • 419
  • 1
  • 4
  • 5
  • 1
    tried in development machines (all 14.04 desktops), very good alternative. – Yonsy Solis Sep 28 '15 at 21:24
  • But I found it was not compatible with my app. – Like Oct 25 '15 at 15:46
  • 4
    Beware that relying on an unofficial package, and/or a PPA, especially for something as fundamental as Python in Ubuntu, bypasses important security processes & can lead to many problems. Some packages explicitly depend on the official packages or versions. Instead, you may well want to use a virtualenv with a more recent version of python for just the apps you need it for. See also [Upgrade to Python 2.7.9 on Ubuntu 14.04 LTS, and make your own .deb package for deployment - Renoir Boulanger](https://renoirboulanger.com/blog/2015/04/upgrade-python-2-7-9-ubuntu-14-04-lts-making-deb-package/) – nealmcb Jan 29 '16 at 18:30
  • Good. You are right. I gave up my approach several months ago. – Like Jan 30 '16 at 11:53
  • 3
    This will upgrade **all** your packages. – mpen May 14 '16 at 01:26
  • Yes, this solution brings in much of side effect. – Like Jun 08 '16 at 05:37
18

You can use pyenv:

 git clone https://github.com/pyenv/pyenv.git ~/.pyenv
 git clone https://github.com/pyenv/pyenv-virtualenv.git ~/.pyenv/plugins/pyenv-virtualenv

Then add

   #   for PyEnv
  export PYENV_ROOT="$HOME/.pyenv"
  export PATH="$HOME/.pyenv/bin:$PATH"
  export PATH="$HOME/.pyenv/shims:$PATH"
  eval "$(pyenv init -)"

to .bash_profile then you can see the python version you want to install or update:

pyenv install --list

want python 2.7.18? First you install it:

 pyenv install 2.7.18

after which you can create a virtualenv using it:

 pyenv virtualenv 2.7.18

Hope it can help you.

Jeremy Anifacc
  • 296
  • 2
  • 2
  • For Ubuntu, that block works better in ~/.bashrc rather than ~/.bash_profile. Then do exec "$SHELL". – brendan Aug 18 '17 at 10:39
14

I'm not a fan of previous answers suggesting installing from various PPAs. No disrespect intended, but I don't know the people who built them and I have no idea what might be in there. In any environment where someone has to answer to a security professional that practice would be frowned upon.

I just found that downloading 2.7.18 from source and installing it side-by-side was a good start. This awesome blog post covered the steps. (recreated here to conform to stack overflow guidelines)

wget https://www.python.org/ftp/python/2.7.18/Python-2.7.18.tgz
tar xfz Python-2.7.18.tgz
cd Python-2.7.18/
./configure --prefix /usr/local/lib/python2.7.18
make
make install

Test if the version works

/usr/local/lib/python2.7.18/bin/python -V
Python 2.7.18

Now that I have 2.7.18 installed I can call it directly or symlink to it from wherever I want. (or copy it into a virtualenv etc.)

And note that I got here from a situation where I was getting insecure platform warnings, and SNI Missing warnings. Which led me here. I imagine many people are finding this question through the same path. And if that's why you're here this snippet may be of use to you too

pip install urllib3[secure]
jorfus
  • 715
  • 7
  • 14
  • This is actually the better answer, especially if you are already using `virtualenv`. Reason i feel that this is better, is that it doesn't rely on any third party applications or repositories that may break something else. – Andre Sep 13 '19 at 08:33
  • Don't do this without a `virtualenv` you will damage the default python! – julian-alarcon Jun 12 '20 at 23:46
  • BTW the last version of Python 2.7 is 2.7.18. Just substitute it in the code above. – Finesse Sep 17 '20 at 00:08
3

Here is new upgraded third party repository:

sudo add-apt-repository ppa:jonathonf/python-2.7
sudo apt-get update
sudo apt-get install python2.7
python --version
arbabnazar
  • 499
  • 6
  • 9
  • `Failed to fetch http://ppa.launchpad.net/jonathonf/python-2.7/ubuntu/dists/trusty/main/binary-amd64/Packages 403 Forbidden` – Finesse Sep 17 '20 at 00:03
2

You can go to the python.org and download the .tar.gz file compile and install it. You will need the basic tools in order to compile the source code. I don't remember if the "build-essential" package will suffice but give it a try.

Dave M
  • 4,494
  • 21
  • 30
  • 30
jaumzors
  • 21
  • 2
2

The version you want is already in Ubuntu, in Vivid (pre)release. If you are brave, you can mix releases and install Vivid's version of Python under Trusty. Below is a generic solution that works on some packages, I can't warranty it with Python. The chances are ~ 50/50 that it will succeed. If you however find a proper backports repository for 14.04 with your required package version, then it should be a better choice.

  1. You have '... trusty main' line in your /etc/apt/sources.list, copy it twice, changing 'trusty' to 'utopic' and 'vivid'.

  2. Run apt-get update and then apt-get -t vivid install python2.7. Review dependencies installed before proceeding. Abort upgrade when you suspect any damage to your system (deleted important packages, etc.).

  3. Remove or hash the two lines from /etc/apt/sources.list.

Never do it on production machines. Make a backup of your OS and prepare boot media to restore it if needed. Expect problems on upgrades. You've been warned.

sam_pan_mariusz
  • 2,053
  • 1
  • 12
  • 15