105

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.

Giacomo1968
  • 58,727

12 Answers12

84

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.

ulidtko
  • 3,086
21
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
  • 319
13
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
  • 157
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
Wuffers
  • 19,619
4

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
  • 49,799
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

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
  • 143
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

Ramhound
  • 44,080
Big Data Guy
  • 111
  • 2
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
  • 185
  • 1
  • 1
  • 8
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
  • 538
  • 1
  • 4
  • 18
0

I recommend using conda as it makes it easier to manage multiple Python versions and doesn't risk messing up Ubuntu.

Here are the commands I use to install conda on Ubuntu 24.04 LTS with bash:

wget https://repo.anaconda.com/archive/Anaconda3-2024.10-1-Linux-x86_64.sh 
bash Anaconda3-2024.10-1-Linux-x86_64.sh -b
if ! [[ $PATH =~ "$HOME/anaconda3/bin" ]]; then PATH="$HOME/anaconda3/bin:$PATH"; fi
conda init bash
source ~/.bashrc 
conda update conda

The -b installs conda without any user input.

To install Python 3.12:

conda list
conda create -y -n mpt312 python=3.12
conda activate mpt312

To remove anaconda3, see https://stackoverflow.com/a/42182997/395857.

Franck Dernoncourt
  • 24,246
  • 64
  • 231
  • 400
0

I don't actually recommend updating your system Python beyond what your package manager will allow, but within the range of what it already allows, just install python3-all or something and do sudo apt update; sudo apt upgrade every now and then. The package manager will update it, but not necessarily to the absolute most recent Python version. The most recent one available in the package manager, yes, but not the newest version available anywhere.

The reason I don't recommend it is because a lot of things in your system use Python and may depend on a specific version. If you update it beyond what the standard package manager allows, you might break your system, or make it super buggy.

What I recommend doing instead is to download the newest version of the Python source code. Then compile it for local use (system-wide use is going to be more dangerous). You can do it like this:

Get the source code here (the current version as I type this is 3.13.3):

https://www.python.org/downloads/source/

Decompress it and go in the directory on the command-line.

Install dependencies. You might get them all by doing this (it worked for me in Xubuntu 22.04.5):

sudo apt update
sudo apt install build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev libffi-dev liblzma-dev tk-dev libgdbm-dev libnss3-dev libssl-dev libsqlite3-dev libreadline-dev libbz2-dev
sudo apt-get build-dep python3

Configure and install it like this:

./configure --prefix=$HOME/programs/Python/python3.13.3 --enable-optimizations --with-ensurepip=install
make
make altinstall

Adjust the --prefix path above to wherever you want your Python installation to permanently live. It's not portable; so, don't rename or move it after you install it. The above example puts it at ~/programs/Python/python3.13.3.

Then in your ~/.bashrc file enter this line:

export PATH=$PATH:/home/putYourUserDirectoryNameHere/programs/Python/python3.13.3/bin

Then you can use the commands python3.13 and pip3.13 from the command-line, for your user.

Well, there are pros to doing a system-wide alternate install (but you don't want to replace/update the one your package manager installed). I mean, malicious code is less likely to be able to alter a system-wide install, if the permissions prohibit it.