How to install python 3.9.0 from source on Ubuntu 20.04 LTS
python3
Install the dependencies
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
wget https://www.python.org/ftp/python/3.9.0/Python-3.9.0.tgz
tar -xzf Python-3.9.0.tgz
cd Python3.9.0
sudo ./configure --enable-optimizations
sudo makesudo make testsudo 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.
Leave a Comment