Python is a high-level programming language, mostly used to write scripting and automation. It is a very popular language known for its simplicity and easy syntax. Python one of the best language for for artificial intelligence (AI).
Python 3.9 is the latest version as of writing this article. The simplest way to install Python is by using apt command and to get the latest have to compile from the source.
This tutorial shows two ways to install Python 3.9 on Ubuntu 20.04.
Install Python 3.9 on Ubuntu 20.04 using APT
Ubuntu 20.04 LTS by default shipped with Python 3.8. It can be easily verified by launching the console and running the following command.
Note: Use the following command exit() or Ctrl-D (i.e. EOF) to exit
To install a different version of Python than the default one, the easiest way is to add the dead snakes PPA.
First steps is update package list, type:
$ sudo apt update
Before adding the PPA its good to install software-properties-common package to easily manage distribution and independent software vendor software sources.
$ sudo apt install software-properties-common
The following command add deadsnakes PPA:
$ sudo add-apt-repository ppa:deadsnakes/ppa
Now install python 3.9 using apt command:
$ sudo apt-get install python3.9
The following command can help to identify the proper install location of Python:
$ which python3
The execution of the above command produces the following output on console:
Install Python 3.9.1 on Ubuntu 20.04 from Source
The other way is to compile and install python 3.9.1 directly from the source.
To download package information from all configured sources, type:
$ sudo apt-get update
Before proceeding with the installation of Python 3.9.1 from source, it is a must that you install the required libraries.
To install prerequisites dependencies, type:
$ sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev
$ sudo wget https://www.python.org/ftp/python/3.9.1/Python-3.9.1.tgz
Unpack the archive with the help of the command shown below.
$ sudo tar -xf Python-3.9.1.tgz
Then change the current directory to Python3.9.1
$ cd Python-3.9.1
Run the configure command with --enable-optimizations, which gives better performance in executing python code.
$ sudo ./configure --enable-optimizations
Note: The above command makes the build process slower but advised to run it.
Now, to compile and build Python 3.9.1 - which will take some time to complete, run:
$ sudo make -j 4
Note: You can specify the number of processor units or cores using -j option. Here my computer has 4 core CPU.
Finally to install binaries, run:
$ sudo make altinstall
The make altinstall command is recommended over make install because it does not overwrite the python3 binary. ie Make altinstall protect from replacing the default python installed in /usr/bin/python.
To find the location of Python 3.9.1, type:
Check version of Python
Launch the console and type the following command:
On success, you are going to receive a Python 3.9.1 interactive shell.
Python 3.9.1 (default, Jan 30 2021, 04:45:04) [GCC 9.3.0] on linux Type "help", "copyright", "credits" or "license" for more information.
$ python3.9 --version
Check Python path
Instead which command we saw earlier, can use type command to check python path:
$ type -a python3
python3 is /usr/bin/python3 python3 is /bin/python3
$ type -a python3.9
python3.9 is /usr/local/bin/python3.9
How to install python2 on Ubuntu 20.04 LTS
To install python2 on Ubuntu 20.04 , the following command should do the job:
$ sudo apt install python2
Through this article, you learned how to install python 3.9 by making use of the easiest way, through the PPA, and also how to compile and install it from the source.
Thanks for reading and please leave your comments.