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 :
Just follow these steps to help change the default python to the newly upgrade python version. Worked well for me.
sudo apt-install python3.7
Install the latest version of python you wantcd /usr/bin
Enter the root directory where python is installedsudo unlink python
or sudo unlink python3
. Unlink the current default pythonsudo ln -sv /usr/bin/python3.7 python
Link the new downloaded python versionpython --version
Check the new python version and you're good to goTo change Python 3.6.8 as the default in Ubuntu 18.04 from Python 2.7 you can try the command line tool update-alternatives
.
sudo update-alternatives --config python
If you get the error "no alternatives for python" then set up an alternative yourself with the following command:
sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 2
Change the path /usr/bin/python3
to your desired python version accordingly.
The last argument specified it priority means, if no manual alternative selection is made the alternative with the highest priority number will be set. In our case we have set a priority 2 for /usr/bin/python3.6.8
and as a result the /usr/bin/python3.6.8
was set as default python version automatically by update-alternatives command.
we can anytime switch between the above listed python alternative versions using below command and entering a selection number:
update-alternatives --config python
get python path from
ls /usr/bin/python*
then set your python version
alias python="/usr/bin/python3"
For another non-invasive, current-user only approach:
# First, make $HOME/bin, which will be automatically added to user's PATH
mkdir -p ~/bin
# make link actual python binaries
ln -s $(which python3) python
ln -s $(which pip3) pip
python
pip
will be ready in a new shell.
A simple safe way would be to use an alias. Place this into ~/.bashrc file: if you have gedit editor use
gedit ~/.bashrc
to go into the bashrc file and then at the top of the bashrc file make the following change.
alias python=python3
After adding the above in the file. run the below command
source ~/.bash_aliases or source ~/.bashrc
example:
$ python --version
Python 2.7.6
$ python3 --version
Python 3.4.3
$ alias python=python3
$ python --version
Python 3.4.3
As an added extra, you can add an alias for pip as well (in .bashrc or bash_aliases):
alias pip='pip3'
You many find that a clean install of python3 actually points to python3.x so you may need:
alias pip='pip3.6'
alias python='python3.6'