Ubuntu is an open-source Linux distribution that supports several programming languages such as Python, Java, C++, etc. Python is a multiple-purpose programming language with enriched features and can be installed easily on Ubuntu. This blog post will demonstrate the complete process of Python installation on Ubuntu 22.04.
How to Install Python on Ubuntu?
Python can be installed on Ubuntu machines using the Official Python Repository, Deadsnakes PPA, and from the Source Code.
Let’s discuss each method with stepwise instructions.
Method 1: Install Python on Ubuntu From the Official Python Repository
To install Python from the official Python repository, follow the steps listed below:
- Update the Packages.
- Install Python.
- Use Python.
- Verify the Installation.
- Quit Python Session.
Step 1: Update the Packages
It is recommended to update system packages before installing any new software/package. Use the command given below to update the system.
$ sudo apt update
It can be noticed that the system packages are updated successfully.
Step 2: Install Python
Now use the following command to install Python on Ubuntu 22.04 LTS.
$ sudo apt install python3
In our scenario, it’s already installed
Step 3: Use Python
To verify Python installation, use the below-listed command:
$ python3
It can be seen that Python3 is installed and working perfectly fine.
Step 4: Verify the Installation
Now check/verify the Python version installed on your Ubuntu machine using the command:
$ python3 --version
In our scenario, it’s 3.10.12
Step 5: Quit Python Session
To end the Python session, execute the following command:
$ quit()
Method 2: Using Deadsnakes PPA
Deadsnakes PPA(Personal Package Archives) allows you to install multiple Python packages on Ubuntu.
It offers packages for various Python versions.
The step-by-step procedure to install Python on Ubuntu using the Deasnakes PPA is given below:
Step1: Manage the Distributions
We first have to manage the distributions. It is required to manage your software packages from independent sources. Use the following command to do so:
$ sudo apt install software-properties-common
Step2: Add Deadsnakes PPA
Now we have to add deadsnakes PPA to Ubuntu’s repository:
$ sudo add-apt-repository ppa:deadsnakes/ppa
Step 3: Update the System
Now update the system using the command given below:
$ sudo apt update
Step 4: Install Python
Now use the command for Python installation.
$ sudo apt install python3
In our scenario, it is already installed so the terminal displays “Python is already to the newest version”.
Method 3: Python Install Using the Source Code
To install Python in Ubuntu from the source code, follow the step-by-step procedure shown below:
Step 1: Update the System
First, update the packages using the update command given below:
$ sudo apt update
Step 2: Install Dependencies
Now to build Python in Ubuntu from the source code, we have to install the required dependencies, and to do so, execute the below command.
$ sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev wget
Step 3: Create a Python Directory
Now create a new folder named Python and move within the folder. Use the following command to do so:
$ sudo mkdir /python && cd /python
It can be seen that a new folder is created
Step 4: Download Python Using wget
Now download the latest version of Python with the help of the wget command as shown below:
$ sudo wget https://www.python.org/ftp/python/3.12.0/Python-3.12.0a1.tgz
It will take some time to download the Python package.
Step 4: Extract the “.tgz” File
Now extract the downloaded file with the help of the tar command, and to do that, run the command:
$ sudo tar -xvf Python-3.12.0a1.tgz
Now move within the extracted folder with the help of the command listed below:
$ cd Python-3.12.0a1
Step 5: Turn on Optimizations
It is essential to turn on optimizations to build Python in Ubuntu, and this can be done by executing the command.
The Python is now built on Ubuntu.
Step 6: Install Python
Now for Python installation, use the command.
$ sudo apt install python3
Check the version of Python by executing the command:
$ python3 -version
Conclusion
Ubuntu is an open-source Linux distribution that supports multiple programming languages such as Python, Java, etc. We can install Python on Ubuntu using multiple methods such as from the official Python repository, from the deadsnakes PPA, from the source code, etc. First, update the, then execute the sudo apt install python3 command to install Python on Ubuntu.