Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
update-alternatives for python3 on Ubuntu

Ubuntu 16 default python is almost python 3

Loads of solutions exist, but for changing the system default, alias is not the way to go.

$ update-alternatives --list python update-alternatives: error: no alternatives for python

ls -larth `which python`*
-rwxr-xr-x 2 root root 4.3M Nov 17 19:23 /usr/bin/python3.5
-rwxr-xr-x 1 root root 3.4M Nov 19 09:35 /usr/bin/python2.7
lrwxrwxrwx 1 root root   24 Feb  5 09:36 /usr/bin/python -> /etc/alternatives/python

So we have 2 - use these in update-alernatives


sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.5 2



$ sudo update-alternatives --config python
There are 2 choices for the alternative python (providing /usr/bin/python).

  Selection    Path                Priority   Status
------------------------------------------------------------
* 0            /usr/bin/python3.5   2         auto mode
  1            /usr/bin/python2.7   1         manual mode
  2            /usr/bin/python3.5   2         manual mode

Press <enter> to keep the current choice[*], or type selection number:

Done

$ python --version
Python 3.5.2
$ python2 --version
Python 2.7.12
$ python3 --version
Python 3.5.2
@GrowtopiaJaw

This comment has been minimized.

Copy link

@GrowtopiaJaw GrowtopiaJaw commented Aug 11, 2018

Updated gist with python3 at https://gist.github.com/GrowtopiaJaw/7c5b7556c9b9a48aa68fe063107e1a5d. Credits added and thanks patrickmmartin!

@thom-nic

This comment has been minimized.

Copy link

@thom-nic thom-nic commented Aug 27, 2018

Can anyone comment as to whether this is problematic on Bionic? Ref: https://stackoverflow.com/questions/43062608/how-to-update-alternatives-to-python-3-without-breaking-apt

@hemulin

This comment has been minimized.

Copy link

@hemulin hemulin commented Oct 5, 2018

@thom-nic
Just followed it after upgrading to 18.04.1. It worked great.

@patrickmmartin
Thanks 👍

@javiergarval

This comment has been minimized.

Copy link

@javiergarval javiergarval commented Feb 12, 2019

Changing between python configurations (python2 to python3.7) give me this thread error:

Original exception was:
Traceback (most recent call last):
  File "/usr/bin/pip", line 9, in <module>
    from pip import main
ImportError: cannot import name 'main' from 'pip'

Which is properly solved modifying /usr/bin/pip file with this:

from pip import main
if __name__ == '__main__':
    sys.exit(main())

But, again, if I change the python interpeter, pip conf is complaining.

Do you know any possible solution?

Thanks!

@malvernbright

This comment has been minimized.

Copy link

@malvernbright malvernbright commented Aug 11, 2019

That worked for me.....thanx

@MVoz

This comment has been minimized.

Copy link

@MVoz MVoz commented Nov 13, 2019

python2
sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 1
sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.4 5

python3
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.5 5
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7 1

@husseinpasha

This comment has been minimized.

Copy link

@husseinpasha husseinpasha commented Feb 12, 2020

Thanks!

@EAsencios

This comment has been minimized.

Copy link

@EAsencios EAsencios commented Apr 29, 2020

Thanks!!

@duwudi

This comment has been minimized.

Copy link

@duwudi duwudi commented Jan 5, 2021

Thanks!

@50935

This comment has been minimized.

Copy link

@50935 50935 commented Feb 10, 2021

Hello , when i started upgrade python 3,

dpkg: error: unable to create new file '/var/lib/dpkg/arch-new': permission denied

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment