Python, a versatile and popular programming language, continues to evolve with its latest release, Python 3.12. This guide will walk you through the process of compiling and installing Python 3.12 on Ubuntu, Debian, and Linux Mint systems. Compiling Python from source allows you to customize the build and potentially achieve better performance.

Advertisement

Prerequisites

  • Operating System: Ensure you are running Debian-based Linux distributions.
  • Basic Terminal Knowledge: Familiarity with Linux command line interface is required.
  • Required Tools and Libraries: Development tools and libraries need to be installed to compile Python.

Step 1: Update Your System

Before starting, update your system’s package list and upgrade the existing packages to the latest version:

sudo apt update 
sudo apt upgrade 

Step 2: Install Required Packages

Install essential build tools and libraries needed for Python compilation:

sudo apt-get install wget build-essential checkinstall 
sudo apt-get install libreadline-gplv2-dev libncursesw5-dev libssl-dev \
    libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev libffi-dev zlib1g-dev 

Step 3: Download Python 3.12 Source Code

Download the most recent source code of Python 3.12 directly from the official Python download website. The current available version is Python 3.12.1. To download Python 3.12.1, execute the following command:

cd /tmp 
wget https://www.python.org/ftp/python/3.12.1/Python-3.12.1.tar.xz 

After downloading, extract the tarball:

tar -xf Python-3.12.1.tar.xz 
cd Python-3.12.1 

Step 4: Prepare for the Compilation

Prepare the source code for the compilation process:

./configure --enable-optimizations 

The --enable-optimizations flag optimizes the Python binary by running multiple tests, which may increase the compilation time.

Step 5: Compile Python Source

Compile Python source code with the following command:

make -j $(nproc) 

The -j $(nproc) option speeds up the compilation process by using all available cores.

Step 6: Install Python 3.12

Once compiled, install Python 3.12:

sudo make altinstall 

Note: Use altinstall instead of install to prevent replacing the default system Python version.

Step 7: Verify the Installation

Check the Python version to verify the successful installation:

python3.12 --version 

If the installation was successful, you should see the version of Python displayed, which should be Python 3.12.1.

Step 8: Setting up Pip (Optional)

If you need to install Python packages, you might want to set up pip, Python’s package installer. You can install pip for your new Python version by:

curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py 
sudo python3.12 get-pip.py 

Step 9: Cleaning Up (Optional)

After the installation, you can remove the downloaded source code and other unnecessary files to free up space:

cd /tmp 
rm -rf Python-3.12.1.tar.xz Python-3.12.1 

Conclusion

Congratulations! You have successfully compiled and installed Python 3.12 on your Ubuntu, Debian, or Linux Mint system. This customized installation allows you to have more control over your Python environment and can be beneficial for developing Python applications that are optimized for your system.

Additional Notes:

  • When compiling Python from the source, remember that it may take more time compared to installing it from the package manager.
  • Keep your system Python intact; many system utilities depend on the Python version packaged with your distribution.
  • For specific projects, consider using virtual environments to manage different Python versions and dependencies.

By following these steps, you can ensure that your development environment is up-to-date and tailored to your needs, allowing you to take full advantage of Python’s features and improvements in its latest version.

Share.
Leave A Reply


Exit mobile version