In this guide, we will show you how to install Python 3 on Ubuntu 24.04.
Python is a robust and versatile programming language that has gained immense popularity among developers worldwide. It has a simple syntax that is easy to understand and write, making it an excellent choice for beginners.
It is freely available for everyone to use and distribute, even for commercial purposes. Its source code is open, allowing anyone to contribute to its development.
As a high-level language, Python abstracts away most of the complex details of the computer, allowing you to focus on programming logic rather than hardware specifics.
In addition, its readability and extensive library support make it an ideal choice for a wide range of applications, from web development to data analysis and machine learning.
With Python, you can write simple scripts and complex programs that do different operations. Python comes preinstalled in Ubuntu. However, follow this guide if you have deleted or want to reinstall Python on your Ubuntu system.
Prerequisites
- An Ubuntu 24.04 system
- Local User with sudo rights
- An active internet connection
Update the System
Before the installation, update your repository to ensure all packages are up to date.
$ sudo apt update -y && sudo apt upgrade -y
Check Python versions
To check if there is any Python 3 version installed on your Ubuntu 24.04, run the following command:
$ python3 -V
If you get an empty output, it indicates your system has no Python 3 version.
Install Python 3 on Ubuntu 24.04 with APT
APT (Advanced Package Tool) is a command line tool for managing Linux software packages. This is the simplest way you can install Python 3 because the package is by default included in the Ubuntu 24.04 repository.
To install Python 3, run the following command:
$ sudo apt install python3 -y
Install Python 3 from Source
You can also install Python 3 from the source code. This process involves creating binaries and other non-source files.
With this method, you can install any Python 3 version that you want. In the previous method, we installed Python 3, version 3.12.3, because it was included by default in the Ubuntu 24.04 repository.
To get started, you will need to install the necessary build tools and libraries:
$ sudo apt update $ sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev curl libbz2-dev
Switch to the /tmp directory:
Using /tmp as a workspace for downloading and compiling source code helps keep your home directory or other system directories clean and organized.
Open your terminal and navigate to the /tmp directory:
$ cd /tmp
At the moment of writing this guide, the latest stable Python 3 version is 3.12.4. To download this version, run the following command:
$ wget https://www.python.org/ftp/python/3.12.4/Python-3.12.4.tgz
Next, extract the downloaded file using tar command.
$ tar -xf Python-3.12.4.tgz
Enter the Python directory:
$ cd Python-3.12.4
Now, run the configure script to prepare the build environment. You can enable optimizations for better performance:
./configure --enable-optimizations
Once the optimization step is finished, you can proceed with installing Python 3.12.4.
$ sudo make altinstall
The altinstall option prevents overwriting the default Python binary.
To verify if the installation is successful, run the following command:
$ python3.12 -V
Install an Older Version of Python
If you need an older version of Python, you can use the deadsnakes PPA. It is a third-party repository for Ubuntu that offers a couple of simple steps to install Python 3.
To get started, first install the supporting software for the PPA.
$ sudo apt install software-properties-common -y
Next, add the Deadsnakes PPA repository:
$ sudo add-apt-repository ppa:deadsnakes/ppa
Once you’ve added the repo, update the Ubuntu system:
$ sudo apt update -y
The PPA repository allows you to install the Python 3 version you want. For instance, to install Python 3.10, you can simply run the following command:
$ sudo apt install python3.10
To verify the installation, run the following command:
$ python3.10 -V
Switch Between Python Versions
As a user of several Python versions, it’s crucial to know how to switch your default Python version to suit different tasks and needs. Using the update-alternatives command, you can switch between different python versions. This method sets up symbolic links and manages different Python executable.
First, ensure that all the Python versions you want to switch between are added to the update-alternatives system. Run the following commands for each Python version:
$ sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.10 10 $ sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.12 20
To select the default Python version, run the following command:
$ sudo update-alternatives --config python
You’ll see the default python versions. The current default version is marked with an asterisk (*). Enter the number corresponding to the version you want to use as the default and press Enter.
Conclusion
These are some of the methods you can use to Install Python 3 on Ubuntu 24.04. Thanks for using this guide for installing the Python programming language on the Ubuntu 24.04 system. For additional help, you can always reach out to us via the comment section.