Python, a versatile and widely used programming language, is essential for a variety of applications, from web development to data science. With the release of Python 3.12, many developers are eager to upgrade or install it on their systems. This article provides a detailed, step-by-step guide to installing Python 3.12 on Ubuntu 22.04 and 20.04.

Advertisement

Prerequisites

Before you begin, ensure you have:

  • A machine running Ubuntu 22.04 or 20.04
  • Sudo privileges

Step 1: Update Your Package List

It’s always a good practice to start by updating your package list to ensure you’re installing the latest versions of software packages. Open a terminal and run the following command:

sudo apt update 

Enter your password when prompted.

Step 2: Install Essential Dependencies

Python 3.12 requires some essential build tools and libraries. You can install them by running the following command:

sudo apt install -y software-properties-common build-essential libffi-dev 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 libssl-dev 

This command will install the necessary dependencies for Python compilation and other libraries required for Python to function properly.

Step 3: Add DeadSnakes PPA

Python 3.12 is not available in the default Ubuntu repositories. To get the latest Python versions, you can use the “DeadSnakes” Personal Package Archive (PPA). Add the PPA to your system by running the following command:

sudo add-apt-repository ppa:deadsnakes/ppa 

You will be prompted to press Enter to continue. Do so to add the PPA to your system.

Step 4: Install Python 3.12

After adding the PPA, update the package list once more to include the new repository:

sudo apt update 

Now that you have added the DeadSnakes PPA and updated your package list, you can install Python 3.12 using the following command:

sudo apt install -y python3.12 python3.12-venv  

This command will install Python 3.12 and all its related packages.

Step 5: Verify the Installation

Check the installed Python version:

python3.12 --version 

You should see the Python version displayed in the terminal, confirming that Python 3.12 is installed on your system.

Step 6: Set Python 3.12 as the Default (Optional)

If you want to make Python 3.12 your default Python version, you can use the update-alternatives command. However, be cautious when changing the default Python version, as it may affect system scripts and applications. To set Python 3.12 as the default, run the following commands:

sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.12 311 
sudo update-alternatives --config python3 

Select the number corresponding to Python 3.12 from the list, and press Enter.

Verify the change with python –version.

Step 7: Install Pip for Python 3.12

Pip is a package manager for Python. Install it by running:

sudo apt install python3.12-distutils 
wget https://bootstrap.pypa.io/get-pip.py 
sudo python3.12 get-pip.py 

Check the Pip version with pip3 --version.

Step 8: Clean Up (Optional)

After the installation, you may clean up unnecessary packages:

sudo apt autoremove 

Conclusion

You’ve successfully installed Python 3.12 on your Ubuntu 22.04 or 20.04 system. This setup opens a pathway to explore the latest features and improvements of Python 3.12. Whether for development, data analysis, or scripting, you’re now ready to take advantage of Python’s powerful capabilities on your Ubuntu machine.

Remember to stay updated with Python and Ubuntu releases for a smooth programming experience.

Share.

Comments are closed.

Exit mobile version