Linux: How to set python3 as default python on linux

2017, December, 17

LinuxHow-To

Python 3 is not backward compatible with Python 2.

Any program written in Python 3 will not compile with Python 2 interpreter and vice versa.

If you are learning Python, then it is better to use Python 3 as usage of Python 3 will keep rising in future and it will receive new features and updates.

In most linux distributions like Ubuntu and Linuxmint, both Python 2 and Python 3 come pre-installed. Many linux tools still require Python 2 to work and hence it is required in linux based operating systems.

In linux we can check the default version via:

$ python –version

image1

Ubuntu by default uses Python 2.

We can check all distributions on Python installed on our system.

$ ls -lh /usr/bin/python*

image2

Here,

  • /usr/bin/python2.7
  • /usr/bin/python3.5
  • /usr/bin/python3.5m

are actual executable files(highlighted in green). Rest are symbolic links to the executables (highlighted in blue).

Our goal is to use python3 as default python on our linux system. We need to set a alias for python as follows:

$ alias python=’/usr/bin/python3’

image3

After if we check python version, we can see that it points to python3

image4

But setting alias is only temporary. An alias is only valid for current login session. So if you logout or restart, alias will be gone and linux will use Python 2 as default python.

To make the change permanent we need to create ‘.bash_aliases’ file in our home directory.

$ touch .bash_aliases

image5

We need to edit this file and add alias information: $ vi .bash_aliases

In vi editor, press ‘i’ to go into Insert mode and type the following:

alias python=’usr/bin/python3’

image6

press escape key to exit Insert mode.

Type :wq to save and close

We can check the file has been successfully edited and saved.

image7

Now if we ever logout or restart system, Linux will use Python 3 as default

image8