92

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.

  • If your system is up to date so is the Python on your system (according to your repositories). Am I wrong? – cYrus Feb 5 '11 at 0:19

10 Answers 10

74

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.


Upd: I keep seeing this upvoted; notice that this is a 9-year old answer, things have changed.


What to learn next

From a superuser perspective (not Python developer's), the next things I'd suggest learning to use:

  • pip/pip3/python3 -m pip — this is the npm for Python. Quick tip: try pip3 install --user howdoi (may need to apt install python3-setuptools python3-pip once, before that works)

  • The virtualenv tool. It's 100% developer-oriented, but you'll likely need to use it (perhaps underneath a few wrappers, such as tox) to work with people's source packages.
    Ruby's bundler or Cabal sandbox may be familiar analogues.

  • The conda tool — which is a totally separate python package repository and installer (think: fork of PyPi).

There's humongous variety of tools in the Python ecosystem in 2020. At the very least, make yourself comfortable with pip before going deeper.

Basic pitfalls

For the brave but unwary, a few pitfalls when trying to manually set up a newer CPython on Ubuntu.

  • Leave /usr alone; you can look but you don't touch. Leave it to dpkg, save yourself some confusion. You have the whole /usr/local at your disposal:

    sudo chown -R `whoami` /usr/local
    pip3 install --prefix=/usr/local pydf
    
  • Compiling CPython from source is well-explained on the web; just don't forget your /usr/local prefix. This is the best way to manually test patches and/or pre-releases (those alpha-, rc- builds) of CPython itself. To wipe built artifacts, you can just rm -rf /usr/local/*; sudo ldconfig.

  • Finding a PPA is decent option too; keep in mind that a PPA is just someone else's private build. Look for credible PPAs with CI/CD running.

| improve this answer | |
  • 6
    Great detail about behind-the-scene workings. – efficiencyIsBliss Feb 5 '11 at 23:26
  • @ulidtko This post is still showing prominently on Google. Could you explain how things af changed, so the people coming here 9 years later can find the answer? Thanks. – Phill Healey Apr 11 at 22:27
  • @PhillHealey I don't feel my knowledge is good enough to try to do a 9-year overview. Sorry about that; no I couldn't. But I do see how the answer's been misfitting. Made an attempt on expanding it into a mini-rozetta-stone, glancing the landscape from the "Basic Ubuntu / Basic Python" intersection. – ulidtko Apr 12 at 12:19
  • @ulidtko - It was mostly a joke, with regards to you needed to even mention how old the question/answers are. But also "things have changed" leaves people unsure of whether the whole answer should be ignored or whether it just needs tweaking. I was just really asking for a little indication of why/how things have changed, not a compendium of Python/Ubuntu evolution. ;-) – Phill Healey Apr 17 at 11:47
19
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.

| improve this answer | |
  • 4
    I 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 Dec 26 '16 at 11:53
  • 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 Aug 15 '17 at 10:42
  • 3
    After installing my python version number is same? What could be the reason? – Ramesh Pareek Apr 22 '18 at 3:28
  • 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? – 3pitt Oct 27 '18 at 7:23
10
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
| improve this answer | |
  • 3
    This duplicates another answer and adds no new content. Please don't post an answer unless you actually have something new to contribute. – DavidPostill Feb 6 '17 at 12:43
  • 3
    sorry I just wanted to write the command to get the new one. – G-Ox7cd Feb 6 '17 at 12:48
  • 7
    If 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 Apr 4 '18 at 20:53
  • 3
    sudo add-apt-repository ppa:deadsnakes/ppa -y && sudo apt-get update – ski_squaw Jul 12 '18 at 23:51
  • E: Unable to locate package python3.6 – Dims Aug 15 '18 at 6:34
5

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
| improve this answer | |
  • 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 Feb 4 '11 at 23:54
  • apt wouldn't upgrade anything up to date, so it wouldn't make a difference ;p – Journeyman Geek Feb 5 '11 at 0:15
  • 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 Jan 29 '19 at 8:35
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
| improve this answer | |
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

| improve this answer | |
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.

| improve this answer | |
  • I'm not familiar with the area, but could you explain how this addresses the question and solves the problem? – Ben N Feb 14 '16 at 20:01
  • 1
    In regards to @ulidtko's answer (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 Feb 14 '16 at 21:10
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.

| improve this answer | |
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

| improve this answer | |
  • 1
    Please 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 Nov 4 '16 at 11:31
  • 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 May 21 '19 at 8:01
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.

| improve this answer | |
  • 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 Dec 5 '15 at 20:39
  • @Gino: Thank you for the feedback. Have you had a look askubuntu.com/questions/612973/…, askubuntu.com/questions/223501/… and the release notes? There is a problem with "AMD's fglrx driver". Would you like any other help? – serv-inc Dec 7 '15 at 8:14
  • 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 Feb 26 '16 at 23:58
  • @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 askubuntu.com/questions/240160/…. 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 Feb 28 '16 at 7:38

Not the answer you're looking for? Browse other questions tagged or ask your own question.