Python is a powerful and versatile programming language used for various purposes, including web development, data analysis, and scripting. While Ubuntu 18.04 comes with Python pre-installed, it might not include the latest Python version. If you need Python 3.12 for your projects and prefer to compile it from source, this step-by-step guide will walk you through the process.
Before you start, ensure you have the following prerequisites:
- An Ubuntu 18.04 system.
- Access to a terminal with sudo privileges.
- A reliable internet connection.
Step 1: Update and Upgrade
Begin by updating the package list and upgrading your system’s installed packages to their latest versions. Open a terminal and run the following commands:
sudo apt update
sudo apt upgrade -y
This ensures your system is up to date and ready for the compilation process.
Step 2: Install Dependencies
To compile Python 3.12 from source, you’ll need to install some essential development packages. Use the following command to install them:
sudo apt install -y build-essential libssl-dev zlib1g-dev libncurses5-dev libncursesw5-dev libreadline-dev libsqlite3-dev libgdbm-dev libdb5.3-dev libbz2-dev libexpat1-dev liblzma-dev libffi-dev wget
These packages are required for building Python and ensuring that it runs smoothly on your system.
Step 3: Download Python 3.12 Source Code
Visit the Python official website to get the latest source code for Python 3.12: Python Downloads.
Right-click on the “Gzipped source tarball” link and select “Copy link address”. Then, use the wget command to download the source code directly to your system. For example:
For Python 3.12, the file name may be something like
Step 4: Extract the Source Code
Once the source code is downloaded, extract it using the following command, replacing
tar -xzf Python-3.12.1.tgz
This will create a directory with the Python source code.
Step 5: Configure the Build
Navigate to the extracted Python source code directory using the cd command. Then, run the following command to configure the build:
This command prepares Python for compilation with optimizations for your system. It may take some time to complete.
Step 6: Compile and Install Python
Now that you’ve configured the build, it’s time to compile and install Python 3.12. Use the following commands:
sudo make altinstall
-j$(nproc) flag tells make to use all available CPU cores for faster compilation.
Step 7: Verify Python Installation
To verify that Python 3.12 has been successfully installed, run the following command:
You should see the Python version displayed in the terminal, confirming that Python 3.12 is installed on your system.
You have successfully installed Python 3.12 on your Ubuntu 18.04 system by compiling it from source. This method allows you to have the latest Python version on your system, which can be useful for specific projects or if you prefer having more control over your Python installation. Remember to keep your system updated and consider virtual environments for managing different Python versions for your projects.