Building and Installing Python from Source on Ubuntu/Debian
===========================================================
Step 1: Open the Terminal
--------------------------
Press Ctrl + Alt + T or search for "Terminal" in your system.
Step 2: Install Required Build Dependencies
-------------------------------------------
Run the following command to install the packages needed to build Python:
sudo apt update
sudo apt install -y \
make build-essential libssl-dev zlib1g-dev \
libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm \
libncursesw5-dev xz-utils tk-dev libxml2-dev libxmlsec1-dev \
libffi-dev liblzma-dev
Step 3: Download the Latest Python Source Code
----------------------------------------------
Visit https://www.python.org/downloads/ and get the latest stable version.
Or use wget to download directly (example uses Python 3.12.2):
cd /tmp
wget https://www.python.org/ftp/python/3.12.2/Python-3.12.2.tgz
Step 4: Extract the Archive
---------------------------
Extract the downloaded tarball:
tar -xf Python-3.12.2.tgz
cd Python-3.12.2
Step 5: Configure the Build Environment
---------------------------------------
Run the configure script to prepare the build:
./configure --enable-optimizations
Tip: You can add `--prefix=/usr/local` to install in a custom location.
Step 6: Build Python
--------------------
Build Python using `make`. You can speed this up with the `-j` flag based on CPU
cores:
make -j$(nproc)
Step 7: Install Python
----------------------
Use `make install` to install:
sudo make install
Note: You can use `make altinstall` instead to avoid overwriting the system
`python3`.
Step 8: Verify the Installation
-------------------------------
Check the installed version:
python3.12 --version
You can now use the newly installed Python version.
Optional: Set Python 3.12 as default (use with caution)
--------------------------------------------------------
To use this Python version as default, you can update alternatives:
sudo update-alternatives --install /usr/bin/python python
/usr/local/bin/python3.12 1
Then select the default:
sudo update-alternatives --config python
Done!
-----
Python is now built and installed from source on your system.