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.
These instructions were performed as the root user on a Liquid Web Self-Managed Ubuntu 18.04 server.
Install with Apt
Apt is the defacto package manager for Ubuntu 18.04. It’s convenient to install packages using it because it typically handles all the needed dependencies.
Step #1: Update Packages and Install Dependencies
To kick things off update system packages by running the following command.
root@ubuntu:~# apt update -y
Once that has finished up, to get the environment prepped, install the following dependency.
root@ubuntu:~# apt install software-properties-common
Step #2: Set up PPA and Install Python 3.8
The Python 3.8.3 package is available through a PPA. To add the PPA and make apt aware of the new package run the following command.
root@ubuntu:~# add-apt-repository ppa:deadsnakes/ppa -y
Now in order to install Python 3.8.3, we will run this command.
root@ubuntu:~# apt install python3.8 -y
Step #3: Verify the Installation
The simplest way to verify the installation of Python 3.8 is to run the following command to drop into a Python shell and observe the version reported.
root@ubuntu:~# python3.8 Python 3.8.3 (default, May 14 2020, 20:11:43) [GCC 7.5.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>>
Install from Source
In some cases, it might be necessary to install Python from source to get a bleeding-edge version.
Step #1: Install Dependencies and Download Python Source
Before getting started with installing Python from source, there are a few dependencies that need to be installed to ensure success.
root@ubuntu:~# apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev \ > libnss3-dev libssl-dev libreadline-dev libffi-dev libsqlite3-dev \ > wget libbz2-dev -y
Next, we will download the Python source from the Python Downloads page. At the time of writing this article, the latest available pre-release candidate was Python 3.9.0b4 – July 3, 2020.
root@ubuntu:~# wget https://www.python.org/ftp/python/3.9.0/Python-3.9.0b4.tgz --2020-07-07 20:52:59-- https://www.python.org/ftp/python/3.9.0/Python-3.9.0b4.tgz Resolving www.python.org (www.python.org)... 220.127.116.11, 2a04:4e42:50::223 Connecting to www.python.org (www.python.org)|18.104.22.168|:443... connected. HTTP request sent, awaiting response... 200 OK Length: 25023724 (24M) [application/octet-stream] Saving to: 'Python-3.9.0b4.tgz' Python-3.9.0b4.tgz 100%[==============================================================>] 23.86M 16.2MB/s in 1.5s 2020-07-07 20:53:01 (16.2 MB/s) - 'Python-3.9.0b4.tgz' saved [25023724/25023724]
Step #2: Extract and Install Python
Now that we have the Python source downloaded, we need to extract it and change into the resultant directory.
root@ubuntu:~# tar -xf Python-3.9.0b4.tgz root@ubuntu:~# cd Python-3.9.0b4 root@ubuntu:~/Python-3.9.0b4#
Next, run the following command to see how many cores are available to the server.
root@ubuntu:~/Python-3.9.0b4# nproc 4
This will come in handy during the make step.
After that run the following command to produce the makefile that contains the set of instructions to set Python up.
root@ubuntu:~/Python-3.9.0b4# ./configure --enable-optimizations
Next, run the make -j command to kick off the installation, providing the number from the nproc command we ran earlier:
root@ubuntu:~/Python-3.9.0b4# make -j 4
To finalize the installation, run the following command.
root@ubuntu:~/Python-3.9.0b4# make altinstall
Step #3: Verify the Installation
Verifying this new Python installation is the same as in the previous section; specify the new version when running this python command.
root@ubuntu:~/Python-3.9.0b4# python3.9 Python 3.9.0b4 (default, Jul 7 2020, 21:07:40) [GCC 7.5.0] on linux Type "help", "copyright", "credits" or "license" for more information. >>>
There you have it! It is a huge convenience that most Linux distributions are coming equipped with Python 3. However, understanding how to obtain more recent Python versions is invaluable, depending on your needs as a user. Staying on top of changes to Python releases is a sure-fire way to keep projects running smoothly by ferreting out any breaking changes as soon as possible, so they can be addressed.
Our Support Teams are full of experienced Linux technicians and System administrators who have an intimate knowledge of multiple web hosting technologies, especially those discussed in this article.
If you are a Fully Managed VPS server, Cloud Dedicated, VMWare Private Cloud, Private Parent server or a Dedicated server owner and you are uncomfortable with performing any of the steps outlined, we can be reached via phone @800.580.4985, a chat or support ticket to assisting you with this process.