How to Install Python on Ubuntu

  • Post category:Ubuntu

Python is one of the most versatile and widely-used programming languages today. Whether you’re using it for web development, data science, or scripting, Python is an essential tool for developers. In this guide, we will walk you through the different ways to install Python on Ubuntu, from using the default package manager to compiling Python from source.

Method 1: Install Python via APT

APT is the simplest method for installing Python on Ubuntu, especially if you don’t need the latest version and are okay with the one available in the official repositories.

Step 1: Open Terminal

To begin, press Ctrl + Alt + T to open your terminal.

Step 2: Update Package Repository

Before installing anything, make sure your package list is up to date:

sudo apt update

Step 3: Install Python 3

To install Python 3 using APT, run the following command:

sudo apt install python3

Step 4: Verify Installation

To confirm that Python 3 was installed successfully, check the version:

python3 --version

Method 2: Install Python via Deadsnakes PPA

If you need a newer version of Python than what is available in the default Ubuntu repositories, you can use the Deadsnakes PPA.

Step 1: Install Required Software

First, make sure that you have the software-properties-common package installed:

sudo apt install software-properties-common

Step 2: Add Deadsnakes PPA

Add the Deadsnakes PPA to your system:

sudo add-apt-repository ppa:deadsnakes/ppa

Step 3: Update Package Repository

Update your package list to include the new PPA:

sudo apt update

Step 4: Install a Specific Version of Python

You can now install the desired version of Python. For example, to install Python 3.11:

sudo apt install python3.11

Step 5: Verify Installation

After the installation, check the installed version:

python3.11 --version

Method 3: Install Python from Source

Installing Python from source is useful if you want to customize your installation or if a specific version is not available through other means.

Step 1: Install Required Dependencies

Before you build Python from source, install the necessary dependencies:

sudo apt install build-essential libssl-dev libffi-dev python3-dev zlib1g-dev wget

Step 2: Download Python Source Code

Download the desired version of Python from the official website. Replace 3.x.x with the version you want:

wget https://www.python.org/ftp/python/3.x.x/Python-3.x.x.tgz

Step 3: Extract the Tarball

Extract the downloaded file:

tar -xf Python-3.x.x.tgz

Step 4: Navigate to the Extracted Directory

Navigate to the Python source directory:

cd Python-3.x.x

Step 5: Configure and Build Python

Configure the build with optimizations enabled:

./configure --enable-optimizations

Then, compile Python using:

make -j $(nproc)

Step 6: Install Python

To install Python, use the following command:

sudo make altinstall

Using altinstall ensures that the default system Python is not overwritten.

Step 7: Verify Installation

Finally, verify the installation by checking the version:

python3.x --version  # Replace x with your installed version number.

Conclusion

Whether you prefer the convenience of APT, need the latest version from Deadsnakes PPA, or want full control with a source installation, this guide provides all the methods you need to install Python on Ubuntu. Select the one that best fits your requirements and get coding!

For more tutorials and tips, visit CodeAllow.