Set Python 3 as default on CentOS 7

Your out-of-the-box CentOS system likely already has an installation of Python on it. For versions older than CentOS 7, the default version of Python is 2.7.x. In this article, I will explain how to install Python 3 and how to make this version your default. 

 

Prerequisites:

Running CentOS

 

First, let us check to see which version of Python is installed on the OS.

python -V
Python 2.7.8

See that the version of Python installed is 2.7.8.

What we will do now is install Python 3 and then set that version to the default version to use. 

First, install the repository to yum.

sudo yum install -y https://centos7.iuscommunity.org/ius-release.rpm

Update so that the install can complete

sudo yum update

Download and install Python and pip. Pip will help with installing add-ons in the future. 

sudo yum install -y python36u python36u-libs python36u-devel python36u-pip

Now that Python 3 is installed let’s change the default version. 

For the sake of this article I will be using vi as my text editor, but feel free to use whichever editor you prefer. 

Open bashrc

sudo vi ~/.bashrc

Find or create the following line and modify it to reflect the version of Python that needs to be the default. 

alias python='/usr/bin/python3.6'

Next, source or reload bashrc. 

. ~/.bashrc

Check to see which version of Python is the default.

python -V
Python 3.6.8

Great! You now have Python 3 set as the default. 

Leave a Reply

Your email address will not be published. Required fields are marked *