Home  »  ArticlesGuidesHow ToProgrammingTechnologyTools   »   A Step-by-Step Guide to Installing Python 3.12 on Ubuntu 24.04 LTS

A Step-by-Step Guide to Installing Python 3.12 on Ubuntu 24.04 LTS

Python 3.12 has arrived with a host of new features and performance improvements, making it a must-have for developers and enthusiasts alike. Whether you’re working on web development, data science, or automation projects, upgrading to the latest version of Python ensures you have access to the latest tools and capabilities. In this guide, we’ll walk you through the process of installing Python 3.12 on your Ubuntu 24.04 LTS system.

Why Upgrade to Python 3.12?

Python 3.12 introduces several enhancements, including new syntax features, improved performance, and enhanced error messages. With these improvements, developers can write cleaner, more efficient code and benefit from faster execution times. Upgrading to Python 3.12 also ensures compatibility with the latest libraries and frameworks.

Prerequisites

Before you begin, ensure that you have the following:

  1. A system running Ubuntu 24.04 LTS: This guide is tailored specifically for Ubuntu 24.04 LTS users.
  2. Sudo privileges: You’ll need administrative access to install new software packages.

Step 1: Update the Package List

Start by updating the package list to ensure you have the latest information on available packages:

$ sudo apt update

Step 2: Install Prerequisites

You’ll need some prerequisites to build Python from source. Install the necessary packages by running:

$ sudo apt install -y build-essential libssl-dev libffi-dev libbz2-dev libreadline-dev libsqlite3-dev zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev liblzma-dev tk-dev

Step 3: Download Python 3.12 Source Code

Next, download the latest Python 3.12 source code from the official Python website. You can use wget for this purpose:

$ wget https://www.python.org/ftp/python/3.12.0/Python-3.12.0.tgz

Step 4: Extract the Downloaded File

Extract the downloaded tarball file:

$ tar -xf Python-3.12.0.tgz

Step 5: Compile and Install Python 3.12

Navigate to the extracted directory:

$ cd Python-3.12.0

Now, configure the build environment:

$ ./configure --enable-optimizations

The --enable-optimizations flag optimizes the Python binary by running multiple tests, which can significantly speed up the compilation process but will take longer to complete.

Compile and install Python 3.12:

$ make -j $(nproc)
$ sudo make altinstall

The make altinstall command prevents overwriting the default system Python binary, ensuring that your existing Python environment remains intact.

Step 6: Verify the Installation

Once the installation is complete, verify that Python 3.12 has been installed correctly by checking the version:

$ python3.12 --version

You should see an output similar to:

$ Python 3.12.0

Step 7: Set Up a Virtual Environment (Optional)

It’s a good practice to use virtual environments to manage your Python projects. This keeps dependencies isolated and avoids conflicts. Create a virtual environment using venv:

$ python3.12 -m venv myenv

Activate the virtual environment:

$ source myenv/bin/activate

You can now install packages within this virtual environment using pip:

$ pip install <package_name>

To deactivate the virtual environment, simply run:

$ deactivate

Conclusion

Congratulations! You’ve successfully installed Python 3.12 on your Ubuntu 24.04 LTS system. With the latest version of Python, you’re now equipped to take advantage of the newest features and improvements. Whether you’re developing applications, automating tasks, or exploring data science, Python 3.12 provides a robust and efficient platform to support your projects.

Found this article interesting? Follow Brightwhiz on Facebook, Twitter, and YouTube to read and watch more content we post.