36

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

6
  • 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. Aug 15, 2018 at 13:16
  • 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.
    – zazi
    Aug 15, 2018 at 13:20
  • Is this safe, what is the end of removing python2 or leaving it. If it's there then something needs it! Aug 15, 2018 at 14:24
  • 2
    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.
    – dobey
    Aug 15, 2018 at 14:29
  • "what is the end of removing python2" - a rather clean system. my python installations where somehow broken after the upgrade.
    – zazi
    Aug 16, 2018 at 9:43

3 Answers 3

39

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.

3
  • 3
    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.
    – philshem
    Oct 8, 2019 at 9:15
  • 3
    you may also want to do sudo apt install python3-pip and sudo update-alternatives --install /usr/bin/pip pip /usr/bin/pip3 10
    – Daniel
    Mar 8, 2020 at 3:47
  • i still get 2 after doing this.
    – chovy
    Dec 31, 2020 at 15:21
18

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' 
2
  • "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.
    – zazi
    Aug 15, 2018 at 14:16
  • 1
    Is this safe, what is the end of removing python2 or leaving it. If it's there then something needs it! Aug 15, 2018 at 14:24
1

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

8
  • 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 ;-)
    – Kev Inski
    Aug 15, 2018 at 13:41
  • 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)
    – zazi
    Aug 15, 2018 at 13:58
  • 2
    @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?
    – Kev Inski
    Aug 15, 2018 at 14:05
  • 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
    Aug 15, 2018 at 14:12
  • Indeed it does.
    – Kev Inski
    Aug 15, 2018 at 14:19

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .