Solved: Install Python 3 in Linux and set it as default

In this post we will show you how to install python 3 in a Linux box. We have Centos 7 installed in our server.

Check installed python version

Most of the Linux versions by default have python installed. So let’s check the current version.

[ec2-user@cloudvedas ~]$ python --version
 Python 2.7.5
[ec2-user@cloudvedas ~]$

As we can see from the above output, currently we have Python 2 installed and we need Python 3 so let’s install it.

Install pre-requisites

Before we install python 3 let’s first update the yum.

[ec2-user@cloudvedas ~]$ sudo yum -y update

Also, install yum utils.

[ec2-user@cloudvedas ~]$ sudo yum -y install yum-utils

And at the end install the CentOS development tools which will help you build and compile software from source code.

[ec2-user@cloudvedas ~]$ sudo yum -y groupinstall development

Install python 3.6.4

The standard yum repos does not have the latest python release, so we will install IUM (Inline with Upstream Stable) which will have the latest packages.

[ec2-user@cloudvedas ~]$ sudo yum -y install

Now let’s install Python 3.6.

[ec2-user@cloudvedas ~]$ sudo yum -y install python36u

Next we will install pip, which will manage software packages for Python:

New customer offer! Top courses from $10.99 when you first visit Udemy
[ec2-user@cloudvedas ~]$ sudo yum -y install python36u-pip

Finally, we will install the development packages.

[ec2-user@cloudvedas ~]$ sudo yum -y install python36u-devel

Let’ check our  python version

[ec2-user@cloudvedas ~]$ python3.6 -V
Python 3.6.4
[ec2-user@cloudvedas ~]$

Update default python

But if you still run the normal command to check python version it will show the Python 2 version if it was installed by default.

[ec2-user@cloudvedas ~]$ python --version
 Python 2.7.5
 [ec2-user@cloudvedas ~]$

To reflect the new python version by default you create an alias in .bashrc file as described below in the last line of the file.

[ec2-user@cloudvedas ~]$ sudo vi ~/.bashrc

# .bashrc
# Source global definitions
if [ -f /etc/bashrc ]; then . /etc/bashrc fi
# Uncomment the following line if you don't like systemctl's auto-paging feature:
# User specific aliases and functions
alias python=python3.6

After making the changes save the file. Now, logout and login again, it should show you the new version.

[ec2-user@cloudvedas ~]$ python -V
Python 3.6.4
[ec2-user@cloudvedas ~]$

Check all installed python versions

Do remember that the default python 2 will still remain in the system as lot of system binaries have dependency on it.

To check all the python versions in your box you can check the installed packages as below.

[ec2-user@cloudvedas ~]$ yum list installed | grep -i python

If you want to create virtual environments in python read our next post.

One Reply to “Solved: Install Python 3 in Linux and set it as default”

Leave a Reply

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