Development Programming Uncategorized
T_nology  

How to Compile Python on Rocky Linux / Fedora

If you are using Rocky Linux, another RHEL-based distribution, or Fedora, you may need to compile Python yourself. Don’t worry, it’s not as difficult as it might seem. Simply follow the instructions below:

  1. Install the required dependencies with the following command:
sudo dnf install wget make openssl-devel bzip2-devel libffi-devel zlib-devel libsqlite3x-devel.x86_64 xz-devel tk-dev python3-tkinter gcc

This will install wget, which is used to grab yhe Python source code from a URL, and make, which is the command used to compile Python. It also installs zlib-devel, openssl-devel, bzip2-devel, libffi-devel, libsqlite3x-devel.x86_64, python3-tkinter, xz-devel and gcc which are dependencies for the compilation. It’s installing these packages through dnf as root.

Next, you’ll want to install some development tools using the following command:

sudo dnf groupinstall "Development Tools"

2. Head over to the Python Source Code Page and get the link to the version you wish to download. For example, if you want to get Python 3.11.3, then you should get the link to that.

3. Make sure you’re in a good directory to do this in (which can be changed with cd) and type wget followed by the source code link. For example, if we want to get version 3.11.3:

wget https://www.python.org/ftp/python/3.11.3/Python-3.11.3.tar.xz

4. Extract the downloaded archive with the following command:

tar -xvf Python-3.11.3.tar.xz

(Note: If you don’t want each file to be listed when extracting the archive, so your console os cleaner, use -xf in place of -xvf. This will make it not be verbose.)

5. A new folder will be created. Use cd to change your current directory into it.

6. Run the configuration script with the following command:

./configure --enable-optimizations

7. Compile Python using the following command:

sudo make altinstall

⚠️Warning: It is extremely important that you use altinstall instead of install in your make command. If you just use make install, the compiled Python version will replace the default Python version already installed. While this may sound like something you’d want, it actually isn’t, and it can and possibly will cause issues on your system to an extent of needing to reinstall the OS.

8. Verify that your Python 3.11 installation is working with the following command:

python3.11 --version

If there were no errors/warnings during compilation, and this command works properly, then you should hopefully be all good to go. If there were some warnings/errors during compilation, however, but this command still works, just be on the lookout for any weird issues that might come along your way.

Thanks for reading!

Leave A Comment