How to install python 3.9.0 from source on Ubuntu 20.04 LTS

Ubuntu 20.04 LTS is already shipped with Python3.8.5. It can be easily verified by launching a fresh console and running the following command.

python3


Once you have managed to execute the above command on your terminal console, the Python interactive shell is going to fire up.



Let's say you want to install a different version of Python than the default one on your fresh Ubuntu machine.
 

Install the dependencies

 
Before proceeding with the installation of Python3.9.0 from source, it is a must that you install the required libraries. So launch a new terminal console on your Ubuntu machine and run the command shown below.

 
sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev

Install Python 3.9.0 alongside the default one

 
There are many ways to install a fresh copy of Python on Ubuntu. One approach would be through the usage of a PPA. Through this tutorial, I am going to compile Python3.9.0 directly from the source ball.


Before going any further, you need the source code of the Python package. I prefer to make use of the wget utility.


Launch a fresh console, and run the following command.

wget https://www.python.org/ftp/python/3.9.0/Python-3.9.0.tgz


Unpack the archive with the help of the command shown below.

tar -xzf Python-3.9.0.tgz


Then change the current directory to Python3.9.0.

cd Python3.9.0


Run the configuration command.

sudo ./configure --enable-optimizations


Then install Python 3.9.0 with the help of the commands shown below.

sudo make
sudo make test 
sudo make altinstall
 


Check the fresh version of Python

 

Launch a fresh console and type the following command.



python3.9



On success, you are going to receive a Python3.9.0 shell like shown in the following screenshot.

Python3.9.0 interactive shell


Now you should have two versions of Python on your Ubuntu 20.04 LTS machine; the one we just installed from source, and the one which was already shipped by default.



The following command can help to identify the proper install location of each one of the versions of Python.


which python3


The execution of the above command produces the following output on my own console. 


/usr/bin/python3


As for the installation path of Python3.9.0, execute the following command on the terminal console.


which python3.9


The result of the above command tells the exact location of Python3.9.0; the one we just installed.


/usr/local/bin/python3.9

Setup a python project

 

Virtual environments are very useful when it comes to working on multiple projects with the same version of Python. Fortunately for us, Python3.9.0 comes with a package venv which helps to automatically create virtual environments in one or more target directories.


In order to properly setup a fresh Python project, the installation of pip is required.


First, download get-pip.py with the help of the command shown below.


wget https://bootstrap.pypa.io/get-pip.py


Then install the pip utility with the help of the following command.


python3.9 get-pip.py


On success, you should get the following output on your terminal console.


Successfully installed pip-20.2.4


Once you have managed to successfully install pip, create a fresh virtual environment with the help of the following command.



python3.9 -m venv env


Activate the virtual environment with the help of the command shown below.


source env/bin/activate


And then use the pip to install the required packages for your project.

Final thoughts 

Now you have a fresh copy of Python3.9.0 on your Ubuntu machine. With the help of the venv utility, you can create as many projects as you want; completely independent from each other.


No comments

Powered by Blogger.