How to Install Python 3.9 on Ubuntu

serhatteker

Serhat Teker

Posted on December 14, 2020

How to Install Python 3.9 on Ubuntu

Python 3.9 released on 2020-10-04 and it offers pretty good changes like generic type annotations. So I want to upgrade my Python to newest version.

For all changes in Python 3.9 you can look at the Changelog : Python 3.9 Changelog

This brief tutorial shows how to install and configure Python 3.9 on Ubuntu.

There are 2 methods to install python on Ubuntu. I prefer first method and strongly recommend that but it depends on developer's taste.

Method 1: Install It From Source Code

I prefer this method since I'd rather always go to source, whether it is documentation for an API or installing something.

Out of the box default python version for Ubuntu 18.{04/10} is python 3.6 and for Ubuntu 19.{04,10} is Python 3.7. As now, Python 3.9.0 is the lastest stable version released as we said earlier. If you find a later version on the site, you can download it instead.

You can find all released python versions in this link: Python Versions


In order to install from source code we need to download the installer file and run the executable.

Before installing python, you must first install some required packages that are needed to build python from source. Maybe you have already installed them. However to get these packages installed, run the commands below:

$ sudo apt update
$ sudo apt upgrade
$ sudo apt dist-upgrade
$ sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev wget
Enter fullscreen mode Exit fullscreen mode

Now create a temporary directory and download the python source code:

$ mkdir ~/tmp
$ cd ~/tmp
$ wget https://www.python.org/ftp/python/3.9.0/Python-3.9.0.tgz

Enter fullscreen mode Exit fullscreen mode

After downloading the package, run the commands below to extract the file and configure the python:

$ tar -xvzf Python-3.9.0.tgz
$ cd Python-3.9.0
$ ./configure
Enter fullscreen mode Exit fullscreen mode

To install the python:

$ sudo make altinstall
Enter fullscreen mode Exit fullscreen mode

Do not use the standard make install as it will overwrite the system's default python3 binary/usr/bin/python3. Since it will probably make a mess of your OS. However it is your choice again.

After make altinstall, python should be installed and ready to use.

To test if python is installed and ready to use, run the commands below

$ python3.9 --version
Enter fullscreen mode Exit fullscreen mode

You should see an output similar to the one below:

Python 3.9.0
Enter fullscreen mode Exit fullscreen mode

For long version:

$ python3.9 --version --version
Python 3.9.0 (default, Oct 14 2020, 19:33:53)
[GCC 7.5.0]
Enter fullscreen mode Exit fullscreen mode

Method 2: Installing Python via PPA

If you want to get the latest version of Python quckly installed and get future updates automatically, you can install it from the below third-party PPA repository.

First install Ubuntu software properties package if it’s not already installed on your system.

$ sudo apt update
$ sudo apt install software-properties-common
Enter fullscreen mode Exit fullscreen mode

After that run the commands to add the PPA.

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

Finally, run below to install Python 3.9

$ sudo apt update
$ sudo apt install python3.9
Enter fullscreen mode Exit fullscreen mode

You can check as below:

$ python3.9 --version
Python 3.9.0

# Long version
$ python3.9 --version --version
Python 3.9.0 (default, Oct 14 2020, 19:33:53)
[GCC 7.5.0]
Enter fullscreen mode Exit fullscreen mode

Bonus

I created a script that installs Python 3.9 as described in Method 1.

You can check it out: install-python.

WARNING
You should always check the source code of any script before you run it.

If you want to use this script and install Python 3.9 from one-line just
run:

$ wget -O - https://gist.githubusercontent.com/SerhatTeker/7d0fc99d27e9bf1d75b4435a38a89fe9/raw/install-python | bash
Enter fullscreen mode Exit fullscreen mode

After the install check python as mentioned before:

$ python3.9 --version
Python 3.9.0

# Long version
$ python3.9 --version --version
Python 3.9.0 (default, Oct 14 2020, 19:33:53)
[GCC 7.5.0]
Enter fullscreen mode Exit fullscreen mode

OK, All done!

💖 💪 🙅 🚩
serhatteker
Serhat Teker

Posted on December 14, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related