Reading Time: 5 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.

About the Author: David Singer

I am a g33k, Linux blogger, developer, student, and former Tech Writer for Liquidweb.com. My passion for all things tech drives my hunt for all the coolz. I often need a vacation after I get back from vacation....

Have Some Questions?

Our Sales and Support teams are available 24 hours by phone or e-mail to assist.

1.800.580.4985
1.517.322.0434

Latest Articles

What is a Webhook?

Read Article

Microsoft Exchange Server Security Update

Read Article

How to Monitor Your Server in WHM

Read Article

How to Monitor Your Server in WHM

Read Article

How to Fix Typical WordPress Errors

Read Article