DEV Community

Cover image for Installing Python3 in Linux
Joseph Kariuki
Joseph Kariuki

Posted on • Updated on

Installing Python3 in Linux

Introduction

Python3 has over the years increased in popularity among the developer communities worldwide and still is at the top according to this statistic. The language was authored by a Dutch programmer called Guido van Rossum.
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, pygame, Panda3D, cocos2D, pyglet, 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, SciPy, Beautiful Soup

Artificial Intelligence / Machine Learning

According to IBM, 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, Scikit-Learn, Numpy, Keras, 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, Kivy, Tkinter and wxPython.
For a sneak peek on GUI development with PyQt, please refer to my GitHub repository on PyQt Interfaces and Desktop 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, Flask, Pyramid and Turbogears and 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
Enter fullscreen mode Exit fullscreen mode

If python is not installed, the following output will be shown.
Python not installed

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
Enter fullscreen mode Exit fullscreen mode

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 with the following command

add-apt-repository ppa:deadsnakes/ppa
Enter fullscreen mode Exit fullscreen mode

which shows the following prompt to press enter.
Deadsnakes PPA
Press enter to proceed and once it is through you should refresh the package list once again via

apt update
Enter fullscreen mode Exit fullscreen mode

then install Python 3.10 (latest as of the time of this publication) via the following command.

apt install -y python3.10
Enter fullscreen mode Exit fullscreen mode

Assuming successful installation of Python 3.10, check the specific version via:

python3.10 --version
Enter fullscreen mode Exit fullscreen mode

which shows the specific version.
Python version

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
Enter fullscreen mode Exit fullscreen mode
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
Enter fullscreen mode Exit fullscreen mode

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
Enter fullscreen mode Exit fullscreen mode

Navigate into the Python directory and configure and ensure enable-optimization option is added as shown in the command below.

cd Python-3.10.4
./configure --enable-optimizations
Enter fullscreen mode Exit fullscreen mode
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
Enter fullscreen mode Exit fullscreen mode

To test Python installation, use the same command as in the previous steps.

python3.10 --version
Enter fullscreen mode Exit fullscreen mode

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.

HAPPY CODING!!!

Top comments (0)