Installing Python Compiler (CPython) from Source Using GCC on Ubuntu
To install Python from source code using GCC on Ubuntu, you need to prepare the compilation environment, download Python’s source code, configure the build, compile it, and install it. Below are the detailed steps:
Before compiling Python, you must install essential tools and libraries required for the build process. Run the following commands in your terminal:
sudo apt update # Update the package list to ensure latest versions
sudo apt install build-essential # Installs GCC, Make, and other core build tools
sudo apt install libssl-dev zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libreadline-dev libffi-dev curl # Installs Python dependencies
These packages include the GCC compiler, Make utility, and libraries needed to compile Python’s core modules (e.g., SSL for secure connections, zlib for compression).
Choose the Python version you want to install (e.g., 3.11.4) and download its source code from the official Python website. You can use wget or curl for this:
wget https://www.python.org/ftp/python/3.11.4/Python-3.11.4.tgz # Replace with desired version
Extract the downloaded tarball:
tar -xzf Python-3.11.4.tgz # Creates a directory named 'Python-3.11.4'
Navigate to the extracted directory and run the configure script. This script checks your system for required dependencies and generates a Makefile tailored to your environment. To optimize performance and specify an installation path (to avoid conflicts with the system Python), use:
cd Python-3.11.4 # Enter the source directory
./configure --enable-optimizations --prefix=/usr/local/python3.11 # Enables optimizations and sets install path
The --enable-optimizations flag improves Python’s performance by enabling profile-guided optimizations. The --prefix option specifies where Python will be installed (change /usr/local/python3.11 to your preferred directory if needed).
Use the make command to compile the source code into executable binaries. To speed up compilation, use the -j flag with the number of CPU cores available (e.g., -j 4 for a quad-core CPU):
make -j 4 # Compiles Python using 4 parallel jobs
This step may take several minutes to an hour, depending on your system’s performance.
After successful compilation, install Python to the directory specified in the --prefix option during configuration. Use altinstall instead of install to prevent overwriting the system’s default Python version:
sudo make altinstall # Installs Python without replacing system Python
This command copies the compiled binaries (e.g., python3.11) and libraries to the /usr/local/python3.11 directory.
Check if Python was installed correctly by running:
/usr/local/python3.11/bin/python3.11 --version # Replace with your installed version
You should see output like Python 3.11.4, confirming the installation.
To use the newly installed Python without specifying the full path, add its bin directory to your PATH environment variable. Edit your shell configuration file (e.g., ~/.bashrc or ~/.zshrc) and append:
export PATH="/usr/local/python3.11/bin:$PATH"
Save the file and apply the changes:
source ~/.bashrc # Or source ~/.zshrc if using Zsh
Now, you can run python3.11 directly from the terminal.
By following these steps, you’ll have a custom-compiled Python installation using GCC on Ubuntu, giving you full control over the build process and optimization options.