How to Make python3.7 default on Unix/Linux

Are you looking to find a method on how to make python3.7 default on your Linux environment? In this post, I will show you how you can make python3.7 as default on your system.

So, you have recently installed the python 3.7 version on your system and you have no idea how to set it as default.

Make python3.7 Default

The simple solution to this problem can be simple using the below code in your .bashrc file. You need to edit the .bashrc file.

alias python3=python3.7

Note that as per the above code, whenever you will use python3 it will be replaced with python3.7 and it will be set as default.

Now suppose you want to set different alias such as python2.7 and want to make that equal to the python3.7 to make it default then you can rewrite the above code as below in the .bashrc file.

alias python2.7=python3.7

Alternate Approach to Set python3.7 as Default

The alternate approach you can take if the above solution is not working for you is that you can use the command update-alternatives. Using this command you can easily set Python 3.7 as default in your Linux environment. The below method is most preferred.

sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.6 1
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.7 2

Note again, in the above code you use anything instead of python3. Suppose you want python3.7 to show whenever you use command python3 or anything of your liking then you can replace python3 with the word you want to use.

How to Make python3.7 default on Unix/Linux

And as per the above code, python3.7 will always be preferred above python3.6. Now enter the below line in the terminal and press enter.

sudo update-alternatives --config python3

I strongly suggest that you use update-alternatives rather than the alias-approach, because aliases do not work in non-shell contexts and are therefore ineffective (also non-interactive shells ignore .bashrc).

As an illustration, consider the following python-file that begins with the shebang #!/usr/bin/env python3: If you ran with the command./, it would ignore your alias, however, python3 would execute in python 3.7.1 in your situation.

Other common ambiguity-prone situations include running commands over ssh or from within a shell script, among others.

If you liked our answer then please follow us on FacebookTwitter and Subscribe to our Newsletter to join a great community of developers around the world. Let us know the questions and answer you want to cover in this blog.

Further Read:

  1. Stop/Kill a process from the command line after a certain amount of time Python

Leave a Comment