By default Ubuntu runs python 2 on its system. Even if you install python 3 on Ubuntu, it will still refer to python 2, whenever you call ‘python’ command. But sometimes you may need to set python 3 as the default version on Ubuntu, in case you have programs that use latest python features. In this article, we will look at how to set default python version in Ubuntu. You can use these steps to switch from python 2 to 3 in Ubuntu. You can also use it to change default python version to any other version you like, as long as you use the right file paths to python installations on your system.
How to Set Default Python Version in Ubuntu
Here are the steps to set default python version in Ubuntu. Please note you need to be logged into Ubuntu as root user or user with sudo privileges for the following steps.
1. Check python version
Open terminal and run the following command to check python version in Ubuntu.
$ python --version 2.7
2. Execute update-alternatives
Run the following command to set default python in Ubuntu.
$ sudo update-alternatives --install /usr/bin/python python /usr/bin/python3 10
In the above command you need to specify old python’s path after –install option, followed by new python’s path. The above command sets a priority of 10 to path of python3. If you have installed python3 to another folder, please update the paths above as per your requirement.
You may use this command to not only update python 2 to 3 but also change python 2.4 to 2.7 or change it to any other version. You just need to change the paths of your old python version and new python version in the above command.
3. Check Python version again
Now check the python version again and you will find it has changed to your new python’s version.
$ python --version 3.0
That’s it. In this article, we have learnt how to set default python version in Ubuntu. Once you have set this python version, it will be applicable to all python scripts run on your system. This is a better option that using an alias in .bashrc which does not work with commands using sudo keyword. Using update-alternatives changes your default python system wide. In fact, you can even use it to downgrade your python version if you are facing issues.