I was trying to set default python version to python3
in Ubuntu 16.04
. By default it is python2
(2.7). I followed below steps :
Set priority for default python in Linux terminal by adding this:
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 10
sudo update-alternatives --install /usr/bin/python python /usr/bin/python2 1
Here, we set python3
to have priority 10 and python2
to priority 1. This will make python3
the default python. If you want Python2 as default then make a priority of python2 higher then python3
EDIT:
I wrote this when I was young an naive, update-alternatives
is the better way to do this. See @Pardhu's answer.
Open your .bashrc file
nano ~/.bashrc
. Typealias python=python3
on to a new line at the top of the file then save the file with ctrl+o and close the file with ctrl+x. Then, back at your command line typesource ~/.bashrc
. Now your alias should be permanent.
If you have Ubuntu 20.04 LTS (Focal Fossa) you can install python-is-python3:
sudo apt install python-is-python3
which replaces the symlink in /usr/bin/python
to point to /usr/bin/python3
.
The best way in ubuntu 18.04 which will work for all users is
sudo vim /etc/bash.bashrc
add lines
alias python=python3
alias pip=pip3
Save the changes and restart .
After restart what ever version of python 3 you have in the system along with python 2.7 will be taken as default. You could be more specific by saying the following in alias if you have multiple version of python 3.
sudo vim /etc/bash.bashrc
add lines
alias python=python3.6
alias pip=pip3.6
Simply remove python-is-python2
:
sudo apt purge python-is-python2
And install python-is-python3
:
sudo apt install python-is-python3
It will automate the process of transition to new python3. Optionally you can get rid of remaining packages later:
sudo apt autoremove && sudo apt autoclean
The second line mentioned can be changed to
update-alternatives --install /usr/bin/python python /usr/bin/python3 10
This gives a priority of 10 for the path of python3
. The disadvantage of editing .bashrc
file is that it will not work while using the commands with sudo
.
Update: Please use sudo
while running the command like this:
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 10