9

I want to upgrade from Ubuntu 16.04.5 LTS to 18.04, so ran sudo do-release-upgrade. After downloading and extracting bionic.tar.gz I get:

Can not upgrade 

Your python3 install is corrupted. Please fix the '/usr/bin/python3'
symlink.

I saw How to fix "python installation is corrupted"? and so I did sudo ln -sf /usr/bin/python3.6 /usr/bin/python3 thinking that it would be a similar problem. But that didn't work (still same error message).

I have a few python versions:

$ ls /usr/lib | grep python
python2.7
python3
python3.5
python3.6

$ update-alternatives --display python3
python3 - auto mode
  link best version is /usr/bin/python3.6
  link currently points to /usr/bin/python3.6
  link python3 is /usr/bin/python3
/usr/bin/python3.5 - priority 1
/usr/bin/python3.6 - priority 2

How do I fix python3?

  • 1
    And what about reinstallation (as mentioned in accepted answer)? – Kulfy Dec 23 '18 at 18:21
12

You need to use the default Python 3 version for 16.04. That's 3.5, not 3.6. So run:

sudo ln -sf /usr/bin/python3.5 /usr/bin/python3

If that doesn't work, try reinstalling the python3 package.

sudo apt-get install --reinstall python3

By the way, update-alternatives --display python3 should give you update-alternatives: error: no alternatives for python3. Different versions of Python are not alternatives in Ubuntu.

23

I just ran into this problem on Pop!_OS 18.04, trying to upgrade to 18.10, and it turns out that the problem lay in the symlink for /usr/bin/python and not for /usr/bin/python3. I had had /usr/bin/python3.6 configured as an alternative for python (not python3), and when I changed this, then I could run do-release-upgrade as expected.

I wish the error message pointed to python and not python3.


Before, with the problem:

$ update-alternatives --display python
python - manual mode
  link best version is /usr/bin/python3.6
  link currently points to /usr/bin/python2.7
  link python is /usr/bin/python
/usr/bin/python2.7 - priority 1
/usr/bin/python3.6 - priority 2 

I fixed it this way:

$ sudo update-alternatives --remove-all python
$ sudo ln -sf /usr/bin/python2.7 /usr/bin/python

Also see this comment below which describes a more precise solution that also better explains what is going on and how to fix it.

  • 1
    Yes, I can confirm that the solution works, this should be accepted answer. – Sumit Jain May 17 at 6:16
  • This worked for me as well – Efi Kaltirimidou May 21 at 9:10
  • 2
    Opting for update-alternatives --remove-all python was definitely an unnecessary overkill: All you needed to do was using update-alternatives --config python to have python point to the latest python2.* (e.g. python2.7), then use update-alternatives --config python3 to have python3 point to specifically python3.6 — which is the default Python 3 version for 18.04. – KiriSakow Jun 9 at 0:45
  • 2
    If you need to upgrade to python 3.7 in Ubuntu 18.04, don't do it systemwide — or you're bound to end up having nasty little problems systemwide with essential tools like gnome-terminal, update-manager, etc. Rather use virtual environments (documentation here and here) – KiriSakow Jun 9 at 1:05
  • @Kiri There are ways to install other versions of Python without replacing the system one(s). For example using the deadsnakes PPA. – wjandrea Jun 17 at 18:14
3

Basically the solution to this problem consists of making /usr/bin/python point to the right version of Python your Ubuntu release expects (for instance, in 16.04 was Python2.7 and in 18.04 was Python3.6).

If you have several versions of Python installed in your system, you might be using update-alternatives to manage them. It doesn't matter much your default alternative for Python is the right version your system expects (3.6 in Ubuntu 18.04), it won't work.

The reason why this doesn't work is that, when using update-alternatives, /usr/bin/python3 points to /etc/alternatives/python3, and it seems that's not exactly the same as making /usr/bin/python3 point to /usr/bin/python3.6.

That's why the solution to this problem often consists of stop managing your Python3 versions with update-alternatives and make /usr/bin/python3 point to the right version of Python3 your system expects.

  • /usr/bin/python doesn't exist on a clean install of 18.04, but it does if you do an upgrade instead of a clean install or install the python package, in which case it should be Python 2.7, not 3.6. See PEP 394. – wjandrea Oct 25 at 21:52
  • To be clear, different versions of Python are not alternatives on Ubuntu and should not be managed with update-alternatives. This is because the OS relies on a certain version being installed. – wjandrea Oct 25 at 21:56
0

I observed this error message on Windows 10 1903 running WSL Ubuntu when I wanted to upgrade from 16.04 LTS to 18.04 LTS.

After do-release-upgrade had failed, I switched python alternatives to every choice offered by update-alternatives --config python and ran the upgrade command again. That did not help.

Then I checked the log file /var/log/dist-upgrade/main.log which contained the lines

2019-09-02 20:58:08,686 DEBUG _pythonSymlinkCheck run
2019-09-02 20:58:08,687 DEBUG python symlink points to: '/etc/alternatives/python', but expected is 'python2.7' or
'/usr/bin/python2.7'
2019-09-02 20:58:08,688 ERROR pythonSymlinkCheck() failed, aborting

So although the error message mentions python3, the issue is about python2.

The upgrade script checks for /usr/bin/python linking to /usr/bin/python2, see the source code of DistUpgrade/DistUpgradeController.py here: ubuntu launchpad

So one solution is to completely remove python from the alternative system and add the link manually, as described in the most popular answer.

If you don't want to remove python from the alternative system, just change the link only for the time during the upgrade process:

# rm /usr/bin/python 
# ln -sf /usr/bin/python2.7 /usr/bin/python
# do-release-upgrade

This worked for me.

During the upgrade process, the link is automatically repaired. So when the upgrade is finished, it points to the python entry in the alternatives directory:

$ ls -l /usr/bin/python
lrwxrwxrwx 1 root root 24 Sep  2 22:01 /usr/bin/python -> /etc/alternatives/python

Edit: for thorough information, the issue might also appear if you upgrade from 18.04 LTS to 19.04 and the anwser applies to this situation, too.

  • Note: This answer also applies to an upgrade from Ubuntu 18.04 LTS to 19.04. I tried it myself after the update to 18.04 had finished successfully. – Daniel K. Sep 5 at 8:27

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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