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

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).


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).