×

How to Install Python 3.12 On Ubuntu 24.04

How to Install Python 3.12 On Ubuntu 24.04


In this blog post, we will show you how install python 3.12 on Ubuntu 24.04, including step-by-step instructions on how to compile and install Python 3.12.6 from source code.

Python is a high-level general-purpose programming language used in various domains including data science, games apps, and web and mobile app development.

Prerequisites

  • Pre Install Ubuntu 24.04 Instance
  • A Regular user with sudo rights
  • Internet connectivity

Install Pyhon 3.12 On Ubuntu 24.04 Using APT Command

In Ubuntu 24.04, Python 3.12 is available in the default apt package repositories. So, it’s installation is straight forward using following commands. Open the terminal and run beneath apt command.

$ sudo apt install python3.12 -y

After the successful installation, verify the python version using the command.

$ python3 --version

Default Python Version Check Ubuntu 24.04

Next, Install Pip using beneath command.

$ sudo apt install -y python3-pip

Install Python3 Pip On Ubuntu 24.04

Once installed, verify pip version

$ pip3 --version
pip 24.0 from /usr/lib/python3/dist-packages/pip (python 3.12)
$

At the time of writing this post, the latest Python 3 release is Python 3.12.6. To install this version, we need to download source code from the official web site of pyhon and then compile it as shown below.

Install Python 3.12.6 On Ubuntu 24.04 From Source

Install several packages required for building Python from source. Run the following command to install them:

$ sudo apt install -y pkg-config build-essential zlib1g-dev libncurses5-dev 
libgdbm-dev libnss3-dev libssl-dev libreadline-dev 
libffi-dev libsqlite3-dev wget libbz2-dev

Install Dependencies For Installing Python 3.12.6 On Ubuntu 24.04

Navigate to the official Python website and download the source code for Python 3.12.6. Alternatively, we can use the wget command to download it directly to your server:

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

Once the download is complete, extract the contents of the tarball using below tar command.

$ sudo tar -xvf Python-3.12.6.tgz

Change to the extracted directory:

$ cd Python-3.12.6/

Next, Configure the Python source using the ./configure script. This step prepares the build environment.

$ sudo ./configure --enable-optimizations

The –enable-optimizations flag optimizes the Python binary by running multiple tests. This may take some time but results in a more efficient binary.

Output :

Python 3.12 Configure From source Ubuntu

Next, compile and install python 3.12.6 by the running the command:

$ sudo make altinstall

Note: Using altinstall instead of install prevents overwriting the default python3 binary. This is useful if you want to keep the default Python version provided by Ubuntu.

Once the above command is executed successfully, we will get the output something like below:

Complie And Install Python 3.12.6 From Source On Ubuntu 24.04

To confirm that Python 3.12.6 has been installed successfully, run the beneath command.

$ python3.12 --version

Python Version Check Post Compile And Installation

Managing Multiple Python Versions

In some situations, you will have multiple Python versions installed on the same system, and you might be required to switch from one default version to another.

The update-alternatives command is used to set the priority of different versions of an application such as PHP or Python residing on Ubuntu. The version with the highest priority becomes the default version.

In the example below, we will set Python 3.12.6 as the default Python version by assigning it the highest priority value of 2  while Python3.10 is assigned the priority of 1.

$ sudo update-alternatives --install /usr/bin/python3 python3  /usr/bin/python3.10 1
$ sudo update-alternatives --install /usr/bin/python3 python3  /usr/bin/python3.12 2

Update Alternatives Python Version Ubuntu 24.04

To switch between different Python versions run the following command:

$ sudo update-alternatives --config python3

You will be prompted to provide the selection number of your preferred default Python version. You can press ENTER to keep the current choice or provide a selection number corresponding to the Python version.

Update Alternative Python3 Ubuntu 24.04

Conclusion

You have successfully installed Python 3.12 on your Ubuntu 24.04 system. You can now start using this version for your projects and enjoy the latest features and improvements. If you found this tutorial helpful, don’t forget to share it and subscribe to our mailing list.



Source link