How to safely switch to python3 as default after upgrade to Ubuntu 18.04
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).
Related videos on Youtube
Comments
-
zazi almost 2 years
Since
python3
is the default python version in Ubuntu 18.04 andpython2
won't be shipped by default on a fresh Ubuntu 18.04 installation, how can I makepython3
default after an upgrade to Ubuntu 18.04 (from 16.04). Currently, after the upgrade topython2
it still defaulted (e.g.python
command directs topython2
etc.). However, purging thepython
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 onpython2
dependencies and thereby collect allpython2
dependencies that could be replaced by an equivalentpython3
dependency (which will be resolved by the upgrade then).-
Juan Antonio almost 6 yearsPlease 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 almost 6 yearsThanks 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 almost 6 yearsIs this safe, what is the end of removing python2 or leaving it. If it's there then something needs it!
-
dobey almost 6 yearsIn 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 almost 6 years"what is the end of removing python2" - a rather clean system. my python installations where somehow broken after the upgrade.
-
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
python2
dependencies - even parts that rely on the "deprecated"python2
dependency after upgrade, i.e., all those parts are obviously replaced by "equivalent"python3
dependencies now. it looks like that only a few packages that I've installed before really rely on thepython2
dependency.
-
-
Kev Inski almost 6 yearsyou 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 almost 6 yearsno, 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, wherepython3
is default andpython2
is not installed per default) -
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 keepingpython2
installed? -
zazi almost 6 yearsI have no disadvantage keeping
python2
installed, if really necessary, but I want to clearly setpython3
as default, i.e., remove thepython2
as often as possible (and a clean install of Ubuntu 18.04 demonstrates that this is possible). -
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
python2
dependency by using its defaultpython3
dependencies/packages. -
Kev Inski almost 6 yearsIndeed it does.
-
George Udosen almost 6 yearsIs this safe, what is the end of removing python2 or leaving it. If it's there then something needs it!
-
dobey almost 6 yearsThis is bad advice. You should never make
/usr/bin/python
point to python3. It is advised against by upstream Python. -
xenoid almost 6 yearsYes, 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 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"python2
dependency, but still have it after the upgrade and that's what's bugs me somehow atm. -
Melik Karapetyan over 4 yearsnice answer. in my case, python2.7 was being installed by nodejs/npm and somehow set as default for
python
. I usedsudo update-alternatives --remove-all python
to remove all python, and then added the only python I want to usesudo update-alternatives --install /usr/bin/python python /usr/bin/python3.7 10
. Also good to know issudo update-alternatives --list python
to see what is what. -
Daniel over 4 yearsyou may also want to do
sudo apt install python3-pip
andsudo update-alternatives --install /usr/bin/pip pip /usr/bin/pip3 10
-
chovy over 3 yearsi still get 2 after doing this.