How to Install and Update Python to 3.9 in Ubuntu

Reading Time: 4 minutes

In this article, we will explore the newest methods to install or update to the latest version of Python on our Ubuntu system. 

What is Python?

python-logo

Python is an interpreted, object-oriented, high-level programming language with dynamic semantics. It’s high-level built in data structures, combined with dynamic typing and dynamic binding, make it very attractive for Rapid Application Development and use as a scripting or glue language to connect existing components together. Python’s simple, easy to learn syntax emphasizes readability and therefore reduces the cost of program maintenance. Python supports modules and packages, which encourages program modularity and code reuse.

https://www.python.org/doc/essays/blurb/

Prerequisites

We begin as always, by running our updates. This ensures that all of our system software is up to date.

root@host:~# apt update

Next, we will add software-properties-common. This software provides some additional scripts that are useful for adding and removing a PPA.

root@host:~# apt install software-properties-common

Add Repository, Update, and Install

Now, we add the repository and then run another update to allow our system to recognize the changes.

root@host:~# add-apt-repository ppa:deadsnakes/ppa
root@host:~# apt update

Finally, we run the installation to add the latest version of Python. As an aside, we have opted to install the -dev and -venv features as well. This ensures we have Python 3.8, pip, and virtualenv. 

root@host:~# apt install python3.9 python3.9-venv python3.9-dev
Reading package lists… Done
 Building dependency tree
 Reading state information… Done
 The following additional packages will be installed:
   libpython3.9-minimal libpython3.9-stdlib python3.9-minimal
 Suggested packages:
   python3.9-venv python3.9-doc binfmt-support
 The following NEW packages will be installed:
   libpython3.9-minimal libpython3.9-stdlib python3.9 python3.9-minimal
 0 upgraded, 4 newly installed, 0 to remove and 0 not upgraded.
 Need to get 4952 kB of archives.
 After this operation, 19.8 MB of additional disk space will be used.
 Do you want to continue? [Y/n] y
 Get:1 http://archive.ubuntu.com/ubuntu focal-updates/universe amd64 libpython3.9-minimal amd64 3.9.0-5~20.04 [751 kB]
 Get:2 http://archive.ubuntu.com/ubuntu focal-updates/universe amd64 python3.9-minimal amd64 3.9.0-5~20.04 [2017 kB]
 Get:3 http://archive.ubuntu.com/ubuntu focal-updates/universe amd64 libpython3.9-stdlib amd64 3.9.0-5~20.04 [1776 kB]
 Get:4 http://archive.ubuntu.com/ubuntu focal-updates/universe amd64 python3.9 amd64 3.9.0-5~20.04 [408 kB]
 Fetched 4952 kB in 2s (2068 kB/s)
 Selecting previously unselected package libpython3.9-minimal:amd64.
 (Reading database … 92316 files and directories currently installed.)
 Preparing to unpack …/libpython3.9-minimal_3.9.0-5~20.04_amd64.deb …
 Unpacking libpython3.9-minimal:amd64 (3.9.0-5~20.04) …
 Selecting previously unselected package python3.9-minimal.
 Preparing to unpack …/python3.9-minimal_3.9.0-5~20.04_amd64.deb …
 Unpacking python3.9-minimal (3.9.0-5~20.04) …
 Selecting previously unselected package libpython3.9-stdlib:amd64.
 Preparing to unpack …/libpython3.9-stdlib_3.9.0-5~20.04_amd64.deb …
 Unpacking libpython3.9-stdlib:amd64 (3.9.0-5~20.04) …
 Selecting previously unselected package python3.9.
 Preparing to unpack …/python3.9_3.9.0-5~20.04_amd64.deb …
 Unpacking python3.9 (3.9.0-5~20.04) …
 Setting up libpython3.9-minimal:amd64 (3.9.0-5~20.04) …
 Setting up python3.9-minimal (3.9.0-5~20.04) …
 Setting up libpython3.9-stdlib:amd64 (3.9.0-5~20.04) …
 Setting up python3.9 (3.9.0-5~20.04) …
 Processing triggers for mime-support (3.64ubuntu1) …
 Processing triggers for gnome-menus (3.36.0-1ubuntu1) …
 Processing triggers for man-db (2.9.1-1) …
 Processing triggers for desktop-file-utils (0.24-1ubuntu3) …
root@host:~# 

Verify Installation

To verify our installation of the required version, use the -V flag to ensure the correct version is set up.

root@host:~# python3.9 -V
Python 3.9.0+
root@host:~#

Source Installation

Install Prerequisite

As you are going to install Python 3.8 from the source. You need to install some development libraries to compile Python source code. Use the following command to install prerequisites for Python:

Search for Latest Version

If by chance you try and update your system and apt returns no results, we can use the apt list command to locate the updates we need.

root@host:~# apt list | grep python3.9
 WARNING: apt does not have a stable CLI interface. Use with caution in scripts.
 idle-python3.9/focal-updates 3.9.0-5~20.04 all
 libpython3.9-dbg/focal-updates 3.9.0-5~20.04 amd64
 libpython3.9-dev/focal-updates 3.9.0-5~20.04 amd64
 libpython3.9-minimal/focal-updates 3.9.0-5~20.04 amd64
 libpython3.9-stdlib/focal-updates 3.9.0-5~20.04 amd64
 libpython3.9-testsuite/focal-updates 3.9.0-5~20.04 all
 libpython3.9/focal-updates 3.9.0-5~20.04 amd64
 python3.9-dbg/focal-updates 3.9.0-5~20.04 amd64
 python3.9-dev/focal-updates 3.9.0-5~20.04 amd64
 python3.9-doc/focal-updates 3.9.0-5~20.04 all
 python3.9-examples/focal-updates 3.9.0-5~20.04 all
 python3.9-minimal/focal-updates 3.9.0-5~20.04 amd64
 python3.9-venv/focal-updates 3.9.0-5~20.04 amd64
 python3.9/focal-updates 3.9.0-5~20.04 amd64
root@host:~# 

Download and Setup

root@host:~# apt-get install build-essential checkinstall
root@host:~# apt-get install libreadline-gplv2-dev libncursesw5-dev libssl-dev \
    libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev libffi-dev zlib1g-dev

root@host:~# cd /usr/src/
root@host:~# wget https://www.python.org/ftp/python/$version/Python-$version.tgz
root@host:~# wget https://www.python.org/ftp/python/3.8.6/Python-3.8.6.tgz

root@host:~# tar zxf Python-$version.tgz
root@host:~# tar xzf Python-3.8.6.tgz

root@host:~# cd Python-$version
root@host:~# cd Python--3.8.6

root@host:~# ./configure --enable-optimizations --with-ensurepip=install
root@host:~# make -j8
root@host:~# make altinstall

Rationalize Command Usage

In the three commands above, we configured our installation to enable the optimizations within Python. This allows it to run about 10 percent faster. Adding this flag may add more time to the overall compilation period, but it is worth it if you often work in Python. Using the –with-ensurepip=install flag installs pip bundled with this deployment. 

Using the -j option tells make command to split the process into “parallel steps” to speed up the compilation process. Even with this option enabled for parallel builds, this step can take a while.

We also run the command make altinstall to prevent our installation from replacing the default python binary file in /usr/bin/python.

Test Python

We can also run the Python test suite to ensure the installation is working as expected on our system. To run the test suite, type in the following command.

root@host:~# python3.8 -m test

Our server will be running tests for some time, and if all the tests pass, we can be sure that our new Python build is working as expected!

Clean Up

After a successful installation, remove the downloaded archive to save disk space

 root@host:~# cd /usr/src/
 root@host:~# rm -f Python-3.8.6.tgz 

Add Python to the system PATH

(Optional Info)

We can also configure Python into the system PATH to allow users to run the pip command within a terminal to install packages into our home directory. The recommended method to add Python to the PATH is to append Python’s version that you installed to the system-wide PATH variable. For example, this can be defined by a script within the /etc/profile.d/ directory.

root@host:~# vim /etc/profile.d/python.sh

PATH=/opt/python/<PYTHON-VERSION>/bin/:$PATH
(<PYTHON-VERSION> is the actual version of Python that we installed earlier.) 

Remove Old Python Versions

Typically, there is no reason to remove older versions of python as there may be scripts on the operating system which require their presence. The most effective way to run an alternative version of Python is within a virtual environment (venv). We can, however, review what versions of Python we currently have installed on our system. 

root@host [~]# ls /usr/bin/python*
/usr/bin/python@   /usr/bin/python2.7* /usr/bin/python2-config@  /usr/bin/python3.6*   /usr/bin/python-config@ /usr/bin/python2@  /usr/bin/python2.7-config*  /usr/bin/python3@         /usr/bin/python3.6m*
 root@host [~]# 

Conclusion

We pride ourselves on being The Most Helpful Humans In Hosting™!

Our Support Teams are filled with experienced Linux technicians and talented system administrators who have intimate knowledge of multiple web hosting technologies, especially those discussed in this article.

Should you have any questions regarding this information, we are always available to answer any inquiries with issues related to this article, 24 hours a day, 7 days a week 365 days a year.

If you are a Fully Managed VPS server, Cloud Dedicated, VMWare Private Cloud, Private Parent server, Managed Cloud Servers, or a Dedicated server owner and you are uncomfortable with performing any of the steps outlined, we can be reached via phone at @800.580.4985, a chat or support ticket to assisting you with this process.

The Top Five Open Source Nocode Tools

Reading Time: 7 minutes

Introduction to NoCode Tools

A No-code development platform allows programmers and non-programmers to create application software through graphical user interfaces and configuration instead of traditional computer programming1.” Various software tools and applications are being used all around us each day. You might be asking yourself, “How do they do it?”. 

Continue reading “The Top Five Open Source Nocode Tools”

MySQL Performance: How To Leverage MySQL Database Indexing

Reading Time: 4 minutes
A Mysql Indexing Logo

Throughout this tutorial, we will cover some of the fundamentals of indexing. As part of the MySQL series, we will introduce capabilities of MySQL indexing and the role it plays in optimizing database performance. Liquid Web recommends consulting with a DBA before making any changes to your production level application.

Continue reading “MySQL Performance: How To Leverage MySQL Database Indexing”

Connecting to MySQL Using Python

Reading Time: 5 minutes

In this article, we will be discussing how to connect to MySQL using Python. Python is one of the most productive and widely used programming languages in use today. Its simple and elegant syntax makes it ideal for new programmers, while experienced programmers enjoy the extensive list of available modules and functionalities. 

MySQL is a robust open-source SQL-based, relational database management system that is used in many software programs and web servers. This article aims to show how to connect to use Python to connect to MySQL and perform some basic tasks. 

Continue reading “Connecting to MySQL Using Python”

MySQL Performance: Identifying Long Queries

Reading Time: 6 minutes

Every MySQL backed application can benefit from a finely tuned database server. The Liquid Web Heroic Support team has encountered numerous situations over the years when some minor adjustments have made a world of difference in website and application performance. In this series of articles, we have outlined some of the more common recommendations that have had the largest impact on performance.

Continue reading “MySQL Performance: Identifying Long Queries”

How to Create and Use MySQL Views

Reading Time: 6 minutes

What is a MySQL View?

A MySQL view is simply an ordinary database object that can save SQL query writers a lot of time when used correctly. A view is a stored query that a user can reference just like a table. Many times users will find themselves using the same base query over and over to solve multiple problems. Views are a way of quickly saving that query and referencing it later.  

Continue reading “How to Create and Use MySQL Views”

How to Install and Configure Jupyter Notebook

Reading Time: 4 minutes

What is Jupyter Notebook?

Jupyter Notebook is an extremely powerful open-source, web-based tool that facilitates the creation of documentation. There are many different avenues to provide technical documentation or demonstrations, but Jupyter Notebook makes it possible to embed visualizations and execute live code. It is useful to be able to utilize documentation to describe development concepts or planning, but providing working examples within documentation can be a more effective way of conveying information. This tutorial will cover how to install Jupyter Notebook on an Ubuntu 18.04 LTS server and connect to it remotely via an SSH tunnel.

Continue reading “How to Install and Configure Jupyter Notebook”

Inserting Fields into Database Tables with PhpMyAdmin

Reading Time: 2 minutes

When working on a database, we sometimes find that additional data should be added for each record, but will not fit into any of the existing fields or structures. Adding a new column into an existing database can help with this problem, and it is very easy to accomplish with PhpMyAdmin.

This guide assumes that you have already logged in to PhpMyAdmin as the root user. Next, we will go over how to copy a table, and then review a few of the options PhpMyAdmin provides.

Continue reading “Inserting Fields into Database Tables with PhpMyAdmin”

Deleting Tables from a Database with PhpMyAdmin

Reading Time: 2 minutes

Managing databases and tables from within PhpMyAdmin is usually very straightforward. This guide will demonstrate how to remove columns and tables from a database using PhpMyAdmin.

We must first state that the potential for unintentional data removal can be elevated when working with any database. Before proceeding, it is highly recommended taking a backup before manipulating any data. Also, prior to amending data, we should confirm is not being used in a live environment.

Continue reading “Deleting Tables from a Database with PhpMyAdmin”

How to Install Python 3 on Ubuntu 18.04

Reading Time: 3 minutes

Python is fast becoming one of the most popular programming languages worldwide. Its low entry barrier for new programmers and simple, elegant syntax makes it a fantastic language to start learning. Python is excellent for task automation, and thankfully most Linux distributions come with Python installed right out of the box. This is true of Ubuntu 18.04; however, the Python package distributed with Ubuntu 18.04 is version 3.6.8. This article will cover how to install a newer version of Python, specifically, the latest stable version 3.8.3.

Continue reading “How to Install Python 3 on Ubuntu 18.04”