How to safely switch to python3 as default after upgrade to Ubuntu 18.04

63,515

Solution 1

This post is a bit old, but I believe a better alternative exists: enter update-alternatives. The following will set your /usr/bin/python to default to 2.7 but have 3.6 available when you want:

sudo update-alternatives --install /usr/bin/python python /usr/bin/python2.7 20
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.6 10

The highest priority here is used as the "automatic" choice for /usr/bin/python but you can easily switch by running sudo update-alternatives --config python.

Solution 2

To completely remove python2, you have to purge the python2.x-minimal package which is done by

sudo apt purge python2.x-minimal

replacing x with the exact version of python 2 on your system. But make sure to look at what other packages are removed as you may have carried packages that still depend on python 2 even after the upgrade, and those packages will be uninstalled as well and cease to work.


There isn't such a thing as a 'default' python interpreter because it just depends on which actual file /usr/bin/python points to, to change this to python use the ln command to update the link, for instance let's say you want it to point to python 3.6

sudo ln -sfn /usr/bin/python3.6 /usr/bin/python

Alternatively, if you just want this for your user, you can set it as your alias in your .bashrc, to do that, open ~/.bashrc in your editor of choice and add the following line

alias python='python3.6' 

Solution 3

On my 16.04 /usr/bin/python is just a link to /usr/bin/python2.7 so I assume you would just have to change this link to point to /usr/bin/python3.x (with adequate x of course).

Share:
63,515

Related videos on Youtube

zazi
Author by

zazi

believe in music to survive!

Updated on September 18, 2022

Comments

  • zazi
    zazi almost 2 years

    Since python3 is the default python version in Ubuntu 18.04 and python2 won't be shipped by default on a fresh Ubuntu 18.04 installation, how can I make python3 default after an upgrade to Ubuntu 18.04 (from 16.04). Currently, after the upgrade to python2 it still defaulted (e.g. python command directs to python2 etc.). However, purging the python package will result in removing too many packages that rely on it, so this is not an option here.

    Ideally, I want to remove the python2 dependency as much possible. Maybe the upgrade process could be designed in such a way that it checks all packages, whether they still really rely on python2 dependencies and thereby collect all python2 dependencies that could be replaced by an equivalent python3 dependency (which will be resolved by the upgrade then).

    • Juan Antonio
      Juan Antonio almost 6 years
      Please check the new wording. Previous was somewhat unclear on what you were asking. Tried to sort this out from the title of your post.
    • zazi
      zazi almost 6 years
      Thanks a lot for the edit @JuanAntonio. They majority of the edits look good. Just did some further corrections to get this questions into right direction.
    • George Udosen
      George Udosen almost 6 years
      Is this safe, what is the end of removing python2 or leaving it. If it's there then something needs it!
    • dobey
      dobey almost 6 years
      In fact python 2 is not installed by default in 18.04, so if you have it, it is because you installed some additional package (or did an upgrade with said packages already installed), which still requires python 2. That 3.x is default does not mean there are no more things using 2.x in the archive. 20.04 will surely not have python 2.x any more though, as it will no longer be supported by upstream at that point.
    • zazi
      zazi almost 6 years
      "what is the end of removing python2" - a rather clean system. my python installations where somehow broken after the upgrade.
    • zazi
      zazi almost 6 years
      "so if you have it, it is because you installed some additional package (or did an upgrade with said packages already installed), which still requires python 2" <- yes, but after a risky cleanup (i.e. actually I did the risky purge step), my systems generally runs now without python2dependencies - even parts that rely on the "deprecated" python2dependency after upgrade, i.e., all those parts are obviously replaced by "equivalent" python3dependencies now. it looks like that only a few packages that I've installed before really rely on the python2 dependency.
  • Kev Inski
    Kev Inski almost 6 years
    you could also point /usr/bin/python to /usr/bin/python3, which is a link to the latest python3 version installed. Or just an alias or a function in your ~/.bashrc. Tons of options ;-)
  • zazi
    zazi almost 6 years
    no, I guess, that this is not really what I want to do here. I want to get rid of python2 in my system (as it is the default case when installing a fresh ubuntu 18.04, where python3 is default and python2 is not installed per default)
  • Kev Inski
    Kev Inski almost 6 years
    @zazi I would not recommend purging python2. It seems that many other packages are depending on it. Do you have any disadvantages by keeping python2 installed?
  • zazi
    zazi almost 6 years
    I have no disadvantage keeping python2 installed, if really necessary, but I want to clearly set python3 as default, i.e., remove the python2 as often as possible (and a clean install of Ubuntu 18.04 demonstrates that this is possible).
  • zazi
    zazi almost 6 years
    "as you may have carried packages that still depend on python 2 even after the upgrade" <- yes, and my intention is to get rid of this dependency as it is not really necessary, since Ubuntu 18.04 runs them without python2dependency by using its default python3 dependencies/packages.
  • Kev Inski
    Kev Inski almost 6 years
    Indeed it does.
  • George Udosen
    George Udosen almost 6 years
    Is this safe, what is the end of removing python2 or leaving it. If it's there then something needs it!
  • dobey
    dobey almost 6 years
    This is bad advice. You should never make /usr/bin/python point to python3. It is advised against by upstream Python.
  • xenoid
    xenoid almost 6 years
    Yes, but that was before 18.04. In addition there is no other python on the system... In fact giving a python2 script to python3 usually makes it croak even before running it (you need a single print statement...) somewhere in the code. The real problem is wanting to completely get rid on python2. There are still packages that depend on it (Gimp, for instance).
  • zazi
    zazi almost 6 years
    @xenoid: yes, Gimp is one of those examples that still really rely on python2, but there are many that do not really need anymore this "deprecated" python2dependency, but still have it after the upgrade and that's what's bugs me somehow atm.
  • Melik Karapetyan
    Melik Karapetyan over 4 years
    nice answer. in my case, python2.7 was being installed by nodejs/npm and somehow set as default for python. I used sudo update-alternatives --remove-all python to remove all python, and then added the only python I want to use sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.7 10. Also good to know is sudo update-alternatives --list python to see what is what.
  • Daniel
    Daniel over 4 years
    you may also want to do sudo apt install python3-pip and sudo update-alternatives --install /usr/bin/pip pip /usr/bin/pip3 10
  • chovy
    chovy over 3 years
    i still get 2 after doing this.