Generally, Python comes pre-installed in Ubuntu, but if it’s not available on your Linux distro for some reason, you can install Python in Ubuntu in a few steps. If you’re a developer, Python is essential to build various software and websites. Other than that, a lot of Ubuntu’s software relies on Python, so you must have it for the smooth functioning of the OS. On that note, let’s go ahead and learn how to install Python in Ubuntu.
In this tutorial, we have included three ways to get Python on Ubuntu. But before that, let’s check if Python is already installed on your system and update it accordingly.
- Check If Python is Already Installed on UbuntuInstall Python in Ubuntu from Official RepositoryInstall Python in Ubuntu from Deadsnakes PPABuild Python in Ubuntu from the Source Code
Check If Python is Already Installed on Ubuntu
Before you install Python in Ubuntu, you should check whether it’s already installed on your system. This allows you to update the existing Python installation without having to install it from scratch again. It should also come in handy if you ever wish to downgrade to a different Python version. With that said, here are the steps to follow.
python3
You can also run the below command to check the Python version on your Ubuntu installation.
If an older version of Python is installed, run the below command to update Python to the latest version on your Linux distro.
Install Python in Ubuntu from Official Repository
Python is available in Ubuntu’s official repository, so you don’t have to do much other than execute a simple command to seamlessly install it on your system. Here is how to do it.
- Next, install Python in Ubuntu by running the below command. This will automatically install Python on your machine.
sudo apt install python3
Install Python in Ubuntu from Deadsnakes PPA
Apart from the official repository, you can also pull newer versions of Python from Deadsnakes PPA, which is popular for hosting new and old versions of Python. If Ubuntu’s official repository (APT) was unable to install Python on your system, this should certainly work. Here are the steps to follow.
Next, run the below command to add Deadsnakes PPA to Ubuntu’s repository. When prompted, press Enter to continue.
Now, update the package list and run the next command to install Python.
You can also choose to install a specific version (old or new) of Python from Deadsnakes PPA. It also hosts nightly builds of Python (experimental), so you can install them as well. Run the commands in the following fashion:
sudo apt install python3.12
or
Build Python in Ubuntu from the Source Code
If you wish to go the extra mile and build Python directly in Ubuntu from the source code, you can do so. But bear in mind, it’s a slightly lengthier process and might require more than 15 minutes to compile Python, depending on your hardware specs. Here are the steps you need to follow.
After that, run the next command to install the required dependencies to build Python in Ubuntu.
Next, create a “python” folder and move to the said folder. If you get a “Permission denied” error, run the command with sudo.
sudo mkdir /python && cd /python
Following that, use wget to download the latest version of Python from the official website. Here, I am downloading Python 3.12.0a1.
Now, extract the downloaded file using the tar command and move to the extracted folder.
After that, run the below command to turn on optimizations before building Python in Ubuntu. This will reduce the time for Python compilation.
Finally, execute the below command to build Python in Ubuntu. This will take anywhere between 10 to 15 minutes to complete the process.
Once it’s done, run the python3 –version command to check the Python version number. And you are done.