Install Python from Source on Ubuntu
I’m writing this article as a personal reference because I always find myself searching for the steps whenever I need to install Python from source on Ubuntu. If you find it useful too, that’s a bonus!
Sometimes, you may need to install a specific version of Python that isn’t available in the Ubuntu repositories, or you may want to compile Python from source to customize it according to your needs. This guide will walk you through the process step by step.
Step 1: Install Required Dependencies
First, install the necessary tools and libraries that are required to build Python from source.
Open a terminal and run the following commands:
sudo apt-get update
sudo apt-get install build-essential checkinstall
sudo apt-get install libncursesw5-dev libssl-dev libffi-dev libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev
These packages provide essential development tools and libraries like the C compiler, and libraries for working with SSL, SQLite, and more.
Step 2: Download Python Source Code
Download the Python source code from the official Python website. You can download the specific version you need.
For this example, we are downloading Python 3.11.5:
wget https://www.python.org/ftp/python/3.11.5/Python-3.11.5.tgz
Step 3: Extract the Python Source Code
Once the .tgz is downloaded, open terminal from the location the python tgz is downloaded at and run the following command:
tar xzvf Python-3.11.5.tgz
This will extract the contents of the Python-3.11.5.tgz
file into a folder called Python-3.11.5.
Step 4: Configure the Build
Navigate to the extracted directory and configure the build process. This step checks your system for required libraries and prepares Python for installation.
cd Python-3.11.5
./configure
Step 5: Compile and Install Python
Compile and install Python using the commands below:
make
sudo make install
Step 6: Verify the Installation
After installation, check the Python version to ensure everything worked as expected.
python3 --version
You should see the version of Python you just installed (e.g. Python 3.11.5
).