Installing Python3 in Linux
Tags: python, linux, beginners, tutorial
Published: 2022-03-29 05:19:00

#### Introduction
Python3 has over the years increased in popularity among the developer communities worldwide and still is at the top according to [this statistic](https://bootcamp.berkeley.edu/blog/most-in-demand-programming-languages/). The language was authored by a Dutch programmer called [Guido van Rossum](https://gvanrossum.github.io/). 
Linux is an operating system like Windows and Mac-OS and one of the most popular platforms that power most cloud solutions and in fact, Android is also Linux under the hood. That said, we are going to see how one can get started with Python on Linux by installing it. This article assumes that the reader has some understanding in using Linux. 

#### Why Python3?
Python is a high-level, open-source and dynamic programming language. It is generally easy to learn and code as compared to its other counterparts. Some of its features include:
- **Object-orientation**: Python supports Object-Oriented programming concepts such as use of objects, classes, object encapsulation etc. 
- **High-level language**: unlike in low-level languages where one has to manage memory and remember the system architecture, in Python this is not necessary.
- **Interpreted language**: Execution of Python's code is line-by-line at a time unlike in other programming languages like _C++, Java_ and _C_. Python is easier to debug since there is no need for compilation.
- **Dynamically typed**: The value of a variable is decided at run time rather than previous as it is with the case in statically typed languages like Java where one has to define a type (e.g. int, long, string).
The above are just but a few mentions of the cool features of Python. Others include free and open source, high portability, can be integrated and interoperable with other languages e.g. _C, Java_ and _C#_.

##### Uses of Python
Python is generally used in the areas of game development, data engineering, Artificial Intelligence, Machine Learning, graphical user interface (GUI) development and web development. 
###### Game Development
Python can be employed in developing games and it can also act as a means of learning how to code with Python or deepening one's knowledge in the language. The libraries that can be leveraged for this include: [arcade](https://pypi.org/project/arcade/), [pygame](https://pypi.org/project/pygame/), [Panda3D](https://pypi.org/project/Panda3D/), [cocos2D](https://pypi.org/project/cocos2d/), [pyglet](https://pypi.org/project/pyglet/), [Python-ogre](https://wiki.ogre3d.org/Python-Ogre)

###### Data Engineering
This entails data management methods such as data mining, modelling, crunching and metadata management ad different scales (from small data to big data). Python is primarily employed for data analysis and pipelines. Python libraries used here include:

[Pandas](https://pandas.pydata.org/), [SciPy](https://www.scipy.org/), [Beautiful Soup](https://pypi.org/project/beautifulsoup4/)

###### Artificial Intelligence / Machine Learning
According to [IBM](https://www.ibm.com/cloud/learn/what-is-artificial-intelligence), Artificial Intelligence (AI) is technology that instructs computers to mimic the human mind in decision-making and problem-solving. Machine Learning (ML) is a subset of AI that consist of procedures that leverage on mathematical data models and algorithms to make predictions. Python implements ML and AI with generally fewer lines of code and pre-built libraries and being a scientific language also comes in support of these technologies. Some of the libraries used in AI and ML include:
[Tensorflow](https://pypi.org/project/tensorflow/), [Scikit-Learn](https://scikit-learn.org/stable/index.html), [Numpy](https://numpy.org/), [Keras](https://keras.io/), [Theano](https://pypi.org/project/Theano/) 

###### GUI Development
Python contains a variety of Graphical User Interface (GUI) frameworks that are being used by developers to create interactive windows and widget controls. The libraries include:
[PyQt](https://pypi.org/project/PyQt5/), [Kivy](https://kivy.org/), [Tkinter](https://docs.python.org/3/library/tkinter.html#module-tkinter) and [wxPython](https://pypi.org/project/wxPython/).
For a sneak peek on GUI development with PyQt, please refer to my GitHub repository on [PyQt Interfaces](https://github.com/jkariukidev/PyQt-Project) and [Desktop To-do list](https://github.com/jkariukidev/simple_to-do_list).

###### Web Development
Building, constructing, and maintaining websites is a broad definition of web development. A front-end, which communicates with the client, and a back-end, which contains business logic and interacts with a database, are typical components of web development. Python also supports quite a percentage of the total websites, web apps and software running in the world wide web. The libraries that are applied in web development include: [Django](https://www.djangoproject.com/), [Flask](https://flask.palletsprojects.com/en/2.0.x/), [Pyramid](https://trypyramid.com/) and [Turbogears](https://turbogears.org/) and [Web2Py](https://pypi.org/project/web2py/) 

#### Methods of Installing Python3
Enough of the libraries, features and usage of Python. Let us see how we can successfully install Python3 in Linux. In this article, we are using Ubuntu 20.04 Long Term Release (LTS) distribution of Linux. I have compiled three different approaches that include:
- Using deadsnakes PPA
- Building from source

##### Installing via Apt and deadsnakes PPA
Most factory versions of Ubuntu ship with Python already installed. To confirm if Python is already installed in your OS, enter the following command:
```
python --version
```
If python is not installed, the following output will be shown.
![Python not installed](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/ctnv4vjj4eh9hfrhn3o2.png)
 
The recommended Python is **3.7.x** and above therefore if lower than the version or not installed, continue to the next step. Let us first update and upgrade the repository lists via the following command:
```
sudo apt update && sudo apt upgrade
```
Then install the **software-properties-common** package whose work is to allow one to easily manage distributions and independent software vendor software sources.
The next step is to add the [deadsnakes PPA](https://launchpad.net/~deadsnakes/+archive/ubuntu/ppa) with the following command
```
add-apt-repository ppa:deadsnakes/ppa
```
which shows the following prompt to press enter.
![Deadsnakes PPA](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/aig6t3rr01sfqr7uqyvk.png)
Press enter to proceed and once it is through you should refresh the package list once again via
```
apt update
```
then install Python 3.10 (latest as of the time of this publication) via the following command.
```
apt install -y python3.10
```
Assuming successful installation of Python 3.10, check the specific version via:
```
python3.10 --version
```
which shows the specific version.
![Python version](https://dev-to-uploads.s3.amazonaws.com/uploads/articles/s6oasnge71xs6grs6qv5.png)
 
##### Building Python from Source
Packages and software can be installed by compiling from downloaded binary scripts and Python is no different. We shall begin by installing the prerequisites required for compiling and building Python which include:
- Installing required packages
- Building Python from source
- Installing Python

##### Installing required packages
To install the pre-requisites, enter the following command.
```
apt install -y build-essential checkinstall libreadline-gplv2-dev libncursesw5-dev libssl-dev libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev
```
##### Build Python
To build Python, the first step should be to download the Python version of your choice (3.10.04 used here). I am making use of the **wget** utility package to download from Python website.
```
wget https://www.python.org/ftp/python/3.10.4/Python-3.10.4.tar.xz
```
Extract the downloaded Python archive when download is complete. One can add the v option for verbose logs when extracting e.g.add `-xvf` in place of `-xf` below.
```
tar -xf Python-3.10.4.tar.xz
```
Navigate into the Python directory and configure and ensure [enable-optimization](https://github.com/docker-library/python/issues/160) option is added as shown in the command below.
```
cd Python-3.10.4
./configure --enable-optimizations
```
##### Install Python
To install Python without overwriting the default version that is already installed in Ubuntu, use the following command. Note, if you use `make install` it shall overwrite the default Python and may cause some issues ahead.
```
make altinstall
``` 
To test Python installation, use the same command as in the previous steps.
```
python3.10 --version
```
#### Conclusion
We have been able to install the latest Python in Ubuntu 20.04 using various methods. To install Python on Windows, please refer to my previous post [here](https://dev.to/jkariukidev/installing-python3-in-windows10-4d47).

HAPPY CODING!!!