How to Install Python on Ubuntu

Python is a high-level, general-purpose programming language that is widely used throughout the IT Industry. It is also one of the most popular in use today. It’s renowned for its versatility and beginner-friendly syntax and has become a cornerstone of modern software development, data science, and automation.

So many everyday applications are programmed in Python that it’s essential to have the Python runtime installed on your Linux server.

This guide provides a comprehensive walkthrough of the different methods for installing Python on Ubuntu, empowering you to choose the approach that best suits your needs and technical expertise. From the simplicity of APT to the flexibility of compiling from source, we’ll explore how to use each method, ensuring a smooth and successful Python installation.

Python

Procedure: Installing Python on Ubuntu

Objective: To outline the various methods for installing Python on an Ubuntu system.

Prerequisites:

  • An Ubuntu system with an active internet connection.
  • Access to a terminal window.
  • Administrative privileges (sudo access).

Using APT to Install:

This is the recommended and simplest method for installing Python.

Step 1: Update the package repository.

APT is the Advanced Package Tool used by Ubuntu-type distributions.

Bash
sudo apt update

Step 2: Install Python

Install the desired Python version. Replace [version] with the specific version number (e.g., 3.10, 3.11).

If you are not sure which version you want to install, you can find further information on the Python website.

Bash
sudo apt install python3

Step 3: Verify the installation.

Now we need to verify the installation has worked correctly

Bash
python3 --version

Installing from Source Code:

This method provides more control over the installation process and is useful for specific needs or when the desired version is not available in the repositories.

Step 1: Update local repositories.

This command refreshes the list of available software packages in your system’s package manager. It ensures you have the latest information about available software, including any updates or new versions.

Bash
sudo apt update

Step 2: Install supporting software.

This installs essential tools and libraries required to build software from source code. Python relies on these components for various functionalities. Here’s a brief overview:

  • build-essential: Provides core tools like a compiler (gcc), make, and other utilities necessary for compiling code.
  • zlib1g-dev: Library for data compression.
  • libncurses5-dev: Library for terminal-based interfaces.
  • libgdbm-dev: Library for database management.
  • libnss3-dev: Libraries for network security services.
  • libssl-dev: Libraries for secure communication.
  • libreadline-dev: Library for command-line editing.
  • libffi-dev: Library for foreign function interfaces.
  • libsqlite3-dev: Library for the SQLite database.
  • wget: Tool for downloading files from the internet.
  • libbz2-dev: Library for bzip2 compression

Bash
sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev libsqlite3-dev wget libbz2-dev

Step 3: Use wget to download Python

Download the desired Python version’s source code from the official Python website. This example with download Python 3.13 Source Tarball

Bash
wget https://www.python.org/ftp/python/3.13.0/Python-3.13.0.tgz

Step 4: Extract the downloaded archive.

Now use the tar command to extract the archive file

Bash
tar -xzf Python-3.13.0.tgz

Step 5: Configure the build.

This script prepares the Python source code for compilation on your specific system. –enable-optimizations tells it to optimize the Python build for better performance.

Bash
./configure --enable-optimizations

Step 6: Compile the source code.

This command compiles the Python source code into machine-readable instructions, creating the necessary executable files.

Bash
make

Step 7: Install Python.

This installs the compiled Python binaries into your system. altinstall is used to avoid overwriting the default Python installation that might already exist on your system. This allows you to have multiple Python versions installed side-by-side.

Bash
sudo make altinstall

Step 8: Verify the installation.

This checks that Python was installed correctly and displays the version number of the Python you just installed.

Bash
python[version] --version

Post-Installation:

  • pip: Ensure pip is installed, as it is the recommended package installer.

Bash
sudo apt install python[version]-pip

  • Virtual Environments: Consider using virtual environments to isolate project dependencies and avoid conflicts.

Bash
python[version] -m venv [environment name]

Note: Replace [version] with the specific Python version you are installing.

This procedure provides a comprehensive guide to installing Python on Ubuntu using different methods. Choose the method that best suits your needs and follow the steps carefully.

Check out our other Tech Quicky content here.

Elsewhere On TurboGeek:  How to Secure Clou­d Infrastructure

Richard.Bailey

Richard Bailey, a seasoned tech enthusiast, combines a passion for innovation with a knack for simplifying complex concepts. With over a decade in the industry, he's pioneered transformative solutions, blending creativity with technical prowess. An avid writer, Richard's articles resonate with readers, offering insightful perspectives that bridge the gap between technology and everyday life. His commitment to excellence and tireless pursuit of knowledge continues to inspire and shape the tech landscape.

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *

Translate »