Linux – Updating Python on Ubuntu system

linux-distributionspythonUbuntu

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.

Best Answer

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). Then for example, howdoi --all compile python3 ubuntu.

  • 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 classic 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.