There are various ways to change the default Python version on Linux. This article discuss some of the ways to change the default version of Python. This article is equally applicable for other executable also.

Modify .bashrc

  • Open your .bashrc file in a text editor.
    vim ~/.bashrc.
  • Type alias python=python3 on to a new line at the top of the file
  • Save the file
  • Type source ~/.bashrc. Now your alias should be permanent.

To change to python3, you can use the following command in terminal alias python=python3. But that only work for the current running process in terminal. If I close and open the terminal it will change back to default python. Adding alias python=python3 in the .bashrc change the default python version permanently.

You can also specify the absolute path of executable as shown in below example.

# Check the location of python 3
$ which python3

# Write alias in bash_profile
vi ~/.bash_profile  
alias python='/usr/local/bin/python3'

# Reload bash_profile
source ~/.bash_profile

# Check python version
$ python --version
Python 3.6.5

Using update-alternatives

Use the following command to set python3.6 as default:

sudo update-alternatives --set python /usr/bin/python3.6

You can also define the priority of these. For example to change Python 3.6 as the default in Ubuntu 18.04 to Python 3.7

  1. Install Python 3.7
    sudo apt-get install python3.7
  2. Add Python3.6 & Python 3.7 to update-alternatives
    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
  3. Update Python 3 to point to Python 3.7
    sudo update-alternatives –config python3
  4. Enter 2 for Python 3.7. Priority 2 is assigned to python 3.7 in Step 2.
  5. Test the version of python
    python3 –version