Updating Python on Ubuntu system

81

22

I want to update the Python build on my Linux box, but the only way I know how to do it is uninstalling the current version and installing the new one. My system is already up to date (I updated yesterday). I wanted to know if there is a way to update a specific program from the command line, like sudo apt-get update <program-name>. I know this command doesn't exist, but I'm hoping something equivalent does.

efficiencyIsBliss

Posted 2011-02-04T23:18:04.067

Reputation: 1 849

If your system is up to date so is the Python on your system (according to your repositories). Am I wrong? – cYrus – 2011-02-05T00:19:25.673

Answers

66

As others already noted, bare sudo apt-get install package will install latest available version, replacing the older one if needed.

But with some software (among which is Python) the situation is somewhat different. Some major, very- and incompatibly-different versions get their own packages. For instance, Python 2.6, Python 2.7, Python 3.1 all live in separate packages on Ubuntu.

Of particular importance is the fact that one of Ubuntu policies is to extensively use Python for writing end-user software. So in fact, fairly large part of the system is written in Python. At the moment, the code runs on Python 2.6 — so this version is the default upon installation; and the code won't easily run on, say, Python 2.7 — because incompatibilities exist. To switch the system to Python 2.7 there needs to be done a piece of work, consisting of updating and re-testing all the scripts. This can't be done easily; that is, you can't just "switch" your system to Python 2.7 and delete the older version.

But. If you don't care about fancy gears of your system and just need newer Python — see no obstacles. Go and sudo apt-get install python3 and code for 3.x Python bravely; just remember to launch your scripts with python3 and use #!/usr/bin/env python3 shebang line.

ulidtko

Posted 2011-02-04T23:18:04.067

Reputation: 2 356

6Great detail about behind-the-scene workings. – efficiencyIsBliss – 2011-02-05T23:26:15.590

16

sudo apt-get install python 3.3.3

this is for python(3.3.3) for different version the corresponding version number should be used.

Mrinal

Posted 2011-02-04T23:18:04.067

Reputation: 169

4I think the space between python and version should not be there. sudo apt-get install python3.6 worked for me, but doesn't affect Python 3.5 in any way. – GUI Junkie – 2016-12-26T11:53:51.177

sudo apt-get install python 3.6.1 > Note, selecting 'libghc-unix-time-dev-0.3.6-1b8cf' for regex '3.6.1' ...

Separated arguments tell apt to install multiple packages – Clancy Hood – 2017-08-15T10:42:30.850

3After installing my python version number is same? What could be the reason? – Ramesh Pareek – 2018-04-22T03:28:00.107

I used this method, but can't find where it was installed (ie how to open a 3.7 shell). @RameshPareek this might have been your problem too. doesnt seem to be in /usr/bin. python is already the newest version (2.7.12-1~16.04). python set to manually installed. any ideas? – Bjorks number one fan – 2018-10-27T07:23:30.490

9

sudo apt-get install python3.6

This installs python 3 in linux along side python 2.To access python 3 enter after you opened the terminal.

python3

G-Ox7cd

Posted 2011-02-04T23:18:04.067

Reputation: 117

2This duplicates another answer and adds no new content. Please don't post an answer unless you actually have something new to contribute. – DavidPostill – 2017-02-06T12:43:04.607

3sorry I just wanted to write the command to get the new one. – G-Ox7cd – 2017-02-06T12:48:31.347

7If I run that I get E: Unable to locate package python3.6 E: Couldn't find any package by glob 'python3.6' E: Couldn't find any package by regex 'python3.6' – Kajsa – 2018-04-04T20:53:39.643

2sudo add-apt-repository ppa:deadsnakes/ppa -y && sudo apt-get update – ski_squaw – 2018-07-12T23:51:29.247

E: Unable to locate package python3.6 – Dims – 2018-08-15T06:34:44.877

4

You're close with thinking of a command like sudo apt-get update (which is an actual command, but doesn't do what you want it to.)

To upgrade Python, and everything else you have installed, just do the command:

sudo apt-get upgrade

Wuffers

Posted 2011-02-04T23:18:04.067

Reputation: 16 645

The OP wants to update python specifically. Telling them to update all of the packages they have is a lazy and bad advice. – Przemek D – 2019-01-29T08:35:36.073

Thanks, but I already knew about the update command, which is why I posted the fact that my system is already up to date. I was hoping to find a command that could be targeted to specific apps. – efficiencyIsBliss – 2011-02-04T23:54:24.773

apt wouldn't upgrade anything up to date, so it wouldn't make a difference ;p – Journeyman Geek – 2011-02-05T00:15:57.293

3

From our sister site: https://askubuntu.com/questions/44122/how-to-upgrade-a-single-package-using-apt-get

Each of these commands updates-by-installing, which should work just fine in many cases, but might not be what you're looking for in a specific case.

If you only want to upgrade a specific package AND only if it is already installed, then use the command:

sudo apt-get install --only-upgrade packagename

music2myear

Posted 2011-02-04T23:18:04.067

Reputation: 34 957

3

In case you are looking to upgrade only minor version of python package like 2.7.11+ t0 2.7.12 then do sudo apt-get install python2.7 should do for you

Senthil Kumar Vaithiyanathan

Posted 2011-02-04T23:18:04.067

Reputation: 131

2

pyenv may be a good option for you: https://github.com/yyuu/pyenv


In regards to @ulidtko's answer, a possible alternative to replacing the system python altogether may be to use an isolated environment with your desired python version. This is similar to a "virtual environment", but for python itself. I've used this in the past for legacy projects that run on python 2.6, as well as when upgrading from legacy version of python to python3.

nicefinly

Posted 2011-02-04T23:18:04.067

Reputation: 143

I'm not familiar with the area, but could you explain how this addresses the question and solves the problem? – Ben N – 2016-02-14T20:01:36.440

1

In regards to @ulidtko's answer (http://superuser.com/a/241903/152143), a possible alternative to replacing the system python altogether may be to use an isolated environment with your desired python version. This is similar to a "virtual environment", but for python itself. I've used this in the past for legacy projects that run on python 2.6, as well as when upgrading from legacy version of python to python3.

– nicefinly – 2016-02-14T21:10:13.027

1

As we all know that Python comes with 3.5 version. So it is very simple to update Python 2.7(which is by default present into Ubuntu 16.04) to Python 3.5.

Follow the following Steps:

  1. Open terminal.
  2. Become root user by using sudo command.
  3. Use the code below to update Python 2.7 to 3.5

    sudo apt-get install python3.5

  4. You will be asked for your permission, then Give option Y.

  5. Wait till complete the process.
  6. clear the screen by using command clear
  7. Check python version by below command.

    python -version

Big Data Guy

Posted 2011-02-04T23:18:04.067

Reputation: 111

1Please use proper formatting in the future and stay away from statements like "Hope this will help you." they are just noise. By answering the question, it is assumed, your attempting to answer the question with something that is helpful. You should go through your answer and fix the minor grammatical mistakes I didn't fix and provide screenshots of the process. I fixed the formatting for you this time, I won't be doing it again, I strongly believe authors should be held to fixing their own answers and questions. – Ramhound – 2016-11-04T11:31:17.140

And it is not that simple, this does not update symbolic links properly so you end up with 3.4 and 3.5 side by side. Python2 is generally side by side with python3 anyway, we do not upgrade Python2 to Python3, we migrate our work. – mckenzm – 2019-05-21T08:01:45.507

1

apt-get install python

I believe this should work. You will need to change 'python' to match the appropriate package name in your repository obviously.

mbreedlove

Posted 2011-02-04T23:18:04.067

Reputation: 185

0

In the single case that you are running a LTS version, your python might be behind by a minor version, say 2.7.5 instead of 2.7.10.

One possibility would be to upgrade the system:

  • set prompt=normal in /etc/update-manager/release-upgrades
  • upgrade the system
    • on the command line, you can type sudo do-release-upgrade to make the upgrade manager do its job to upgrade to the latest (=non-LTS) version.
    • the GUI solution uses the Update Manager

As always when upgrading, have a look at the release notes, as the upgrade might break your system in a few cases, see @Gino's comment.

serv-inc

Posted 2011-02-04T23:18:04.067

Reputation: 400

Dude, I tried your change and it broke my Ubuntu 14.04 system ! Basically, it tried to update from 14.04 to Ubuntu 15 and it was not able to do so successfully, leaving my system in a half way state where the graphic login no longer worked -- e.g. I would login via the graphic login and it would kick me back out to the same graphical login. Just spent the past hour+ reinstalling Ubuntu 14.04 onto my machine to fix the break your purported solution. – Gino – 2015-12-05T20:39:25.897

@Gino: Thank you for the feedback. Have you had a look http://askubuntu.com/questions/612973/caught-in-loop-when-trying-to-login-after-upgrade-from-14-10-to-15-04#, http://askubuntu.com/questions/223501/ubuntu-gets-stuck-in-a-login-loop and the release notes? There is a problem with "AMD's fglrx driver". Would you like any other help?

– serv-inc – 2015-12-07T08:14:04.283

I think your suggestion, to circumvent the normal safety checks, thereby allowing an upgrade to a non-LTS version is fraught with peril. To this day, I don't know why the upgrade failed, because it didn't show any diagnosable error messages. Instead, I ended up with a system that was no longer usable. If I remember correctly, I ended up rerunning the Ubuntu installation with certain settings that was able to restore most of my system. And, as a side note, I'm a professional programmer, so I generally know my way around a linux system. – Gino – 2016-02-26T23:58:43.513

@Gino: This seems to be the standard procedure to switch from LTS to non-LTS. It '[changes] the awareness level of new releases. One can set this to "supported" (regular) releases, "long term only" (LTS) releases, or even "nothing" (no major upgrades)." ' For the quote see http://askubuntu.com/questions/240160/can-ubuntu-server-10-04-4-lts-be-upgraded-to-non-lts. It uses the system's upgrade manager. Unfortunately, in your case, the (default) upgrade did not work, which might be due to a buggy graphics driver (see the links above).

– serv-inc – 2016-02-28T07:38:27.840