Hello
readers! This is the second article of our tutorial series on Python -
'Python on Terminal' and in this article we will learn to install Python
on your Linux system. In the last article of the series - Introduction to Python,
I have mentioned that, Python is available on most of the Linux
distributions, nowadays, normally Python version 2.x.x. Even though
Python2 is still being used by 85%-90% industries worldwide, someone
might want to try his hands on Python3. Or some people, who have been
using old Linux distributions, might wish to upgrade their existing
Python version. This article will guide you to install Python or upgrade
the Python version in your Linux systems. Here we go!
Installing or Upgrading Python in Linux
First
of all, we will see how Python can be installed on the Linux systems,
if it is not already there. We do have plenty of package managers in
different flavors of Linux distributions- apt-get and dpkg (for Ubuntu),
yum (Yellowdog Update Manager, for CentOS/RHEL), zypper and yast (for
SuSE Linux), to name a few. These package managers have made our life
very easy, you just need to issue a command and they will search for the
required packages and their dependencies, fetch them and install them
for you. You need not download packages and their dependencies
individually, which rather would have been a difficult task if there
were no package managers.
I have a system with CentOS 7 installed in it. I don't have Python installed in it. So, I can install it using
yum
command as shown below:# If you are 'root' user $ yum install python # If you are 'non-root' user $ sudo yum install python
The command will not work for Ubuntu, as we have different package manager for that Linux distribution -
apt-get
, as mentioned earlier. So, for Ubuntu based systems, we can install Python using apt-get
command as below:$ sudo apt-get install python
This will install
Python in your Linux system. We can check the current version of Python
installed in the system with the command -
python -V
(capital letter 'V') or python --version
.$ python -V Python 2.7.5 $ python --version Python 2.7.5
Another way of
installing Python is to download, build and install from source. This
method can also be used to upgrade Python to a higher version. Let's
say, we need to update the Python version from 2.7.5 to 3.5.2. For this,
we need to download the source archive from the Python download page
available at - https://www.python.org/ftp/python/.
$ wget https://www.python.org/ftp/python/3.5.2/Python-3.5.2.tgz
This file is an archive and needs to be extracted. This can be done using
tar
command as below-$ tar -xvzf Python-3.5.2.tgz
It will be extracted to a directory -
Python-3.5.2
, which you can verify using ls
command. In the next step, we would need to configure, build and
install Python. For that, we need to change to the directory where our
archive is extracted, using cd
command. Then, we configure the installation directory with the command - ./configure --prefix=/usr/local
.
For this, you need to have a C compiler installed in your system. If
it's not there, it will display an error message. You can issue yum install gcc
command in order to install GNU C Compiler in CentOS (sudo apt-get install gcc
in case of Ubuntu). Now, we need to install Python and we do this by using make altinstall
command. Thus, we have used below commands to configure and install Python using the extracted archive-$ cd Python-3.5.2 $ ./configure --prefix=/usr/local $ make altinstall
It will take some
moments for the installation to complete. When it is done, we can verify
the Python versions installed by typing on terminal
python[TAB][TAB]
. It should show both the versions present in your system.$ python python python2 python2.7 python3.5 python3.5m python3.5m-config $ python2.7 -V Python 2.7.5 $ python3.5 -V Python 3.5.2
Thus, we now have
two versions of Python with us, it's up to us which one to use. With
this, we also came to an end of this article. In the next article, we
would be learning Python variables, constants and datatypes in the next
article of this tutorial series. Please let us know about your views and
opinions in the comment section below.
إرسال تعليق