If you're using Ubuntu Linux, chances are you will be using some if not many, python-based applications.
Python is a simple to use, easy learning programming language and ranks as one of the top most popular programming languages.
Python is a programming language known for its easy syntax. It is one of the best and most commonly used languages for machine learning and AI software.
Here at LinuxAPT, as part of our Server Management Services, we regularly help our Customers to perform related Python queries.
In this context, we shall look into how to install Python 3.9 on Ubuntu 20.04 from a PPA repository, compile it from source code and alternatively compile and install it using Linuxbrew tool.
Note that these same steps apply for all Ubuntu-based distribution, like Linux Mint and Elementary OS, and also including other Ubuntu flavors like Kubuntu and previous Ubuntu releases.
1. How to install Python 3.9 on Ubuntu using APT ?
By default, Ubuntu 20.04 comes with Python 3.8 installed.
You can check that out by typing:
$ python3 --version
Now, we will see how to install Python 3.9 from the PPA repository.
It is a pretty easy and straightforward process to complete.
i. First, we will update the system:
$ sudo apt update && sudo apt upgrade
ii. Before adding the Python Ubuntu repository, we need to install the software-properties-common package to manage independent software vendor software sources:
$ sudo apt install software-properties-common
iii. Now add the repository to the system's repository list:
$ sudo add-apt-repository ppa:deadsnakes/ppa
You'll see the text info about PPA repository and prompt to continue.
Now press the enter to continue, and add the new repository.
You should see the newly added repo deadsnakes in the output at the end:
Get:1 http://ppa.launchpad.net/deadsnakes/ppa/ubuntu focal InRelease [18.1 kB]
iv. Once the repository is added, you can install Python 3.9 simply by executing:
$ sudo apt install python3.9
We can verify that the operation was successful by running:
$ python3.9 --version
The following command can help you identify the install location of Python executable binary:
$ which python3.9
2. How to Install Python 3.9.1 on Ubuntu 20.04 from Source Code ?
Now we will see how to compile Python from the source code. When you compile Python from the source code, you can install the latest version of the application and customize the build options.
i. First, we must install the required build dependencies by installing the following APT packages:
$ sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev libsqlite3-dev wget libbz2-dev
ii. Next, download the latest source code archive using wget tool:
$ wget https://www.python.org/ftp/python/3.9.1/Python-3.9.1.tgz
iii. Now, extract the downloaded archive:
$ tar -xf Python-3.9.1.tgz
iv. Enter the Python-3.9.1 directory and run the configuration script as follows.
$ cd Python-3.9.1
$ ./configure –enable-optimizations
v. When the script is finished, run the make command to compile the binary executable:
$ make -j 8
If you have more than 8 cores on your computer, you can adjust the above command (-j parameter) to reflect the number of your CPU cores for faster build time.
You can use nproc command to see how many processor cores you have on your machine.
vi. When the compilation is completed, install the binary file using:
$ sudo make altinstall
We use altinstall in this case because if the install parameter is used, the installation process will overwrite the default Python3 system binary.
vii. When the installation operation is finished, you can check the location of the binary file with:
$ which python3.9
We can see that Python 3.9 has been installed on your Linux system.
3. How to Install Python 3.9.1 on Ubuntu 20.04 with Linuxbrew (HomeBrew) ?
i. To install Linuxbrew, type the following in the terminal:
$ /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
ii. Next, add Linuxbrew to your PATH in /home/USERNAME/.profile (change USERNAME in command with your logged-in user):
$ echo 'eval $(/home/linuxbrew/.linuxbrew/bin/brew shellenv)' >> /home/USERNAME/.profile
$ eval $(/home/linuxbrew/.linuxbrew/bin/brew shellenv)
iii. Next, install Python 3.9 by running this command:
$ brew install firstname.lastname@example.org
After some time, we will see this log at the end:
Python has been installed as
We have successfully installed the latest python3.9 using Linuxbrew.
This is the simplest method to install it.
We can also check and verify the installation directory:
$ which python3
[Need assistance in installing Python on your Linux System ? We are available to help you. ]