TechCacheKB.com

Technical News and Knowledge Base Articles along with How to Step by Step Guides for SMB and Enterprise


Getting Started with Python on Ubuntu 24.04


Python is a versatile programming language widely used in various fields, including web development, data science, automation, and more. This guide will walk you through the steps to get started with Python on Ubuntu 24.04, from installation to creating and running your first Python program, and finally, compiling it into a single executable installer.

Installing Python on Ubuntu 24.04

Ubuntu 24.04 comes with Python pre-installed. However, you might want to install a specific version, such as Python 3.11. Follow these steps to install Python 3.11 using the terminal:

Step 1: Update the Package List

Open your terminal and update the package list to ensure you have the latest information about the available packages:

sudo apt update

Step 2: Install Python 3.11

Install Python 3.11 and its components by running the following commands:

sudo apt install python3.11
sudo apt install python3.11-dev
sudo apt install python3.11-venv
sudo apt install python3.11-distutils

Step 3: Verify the Installation

Verify that Python 3.11 is installed correctly by checking the version:

python3.11 --version

You should see output similar to:

Python 3.11.x

Installing and Using pip

pip is the package installer for Python. It allows you to install and manage additional libraries and dependencies that are not included in the standard library.

Step 1: Install pip

Install pip for Python 3.11 using the following command:

sudo apt install python3-pip

Step 2: Verify pip Installation

Check the pip version to ensure it is installed correctly:

pip3 --version

Creating Your First Python Program

Now that you have Python installed, let’s create a simple Python program.

Step 1: Create a Python File

Open your preferred text editor or IDE and create a new file named hello.py. Add the following code to the file:

print("Hello, World!")

Step 2: Save the File

Save the file in a directory of your choice.

Step 3: Run the Python Program

Navigate to the directory where you saved the hello.py file and run the program using the terminal:

python3.11 hello.py

You should see the following output:

Hello, World!

Compiling a Python Program into a Single Executable Installer

To distribute your Python program as a single executable, you can use tools like PyInstaller.

Step 1: Install PyInstaller

Install PyInstaller using pip:

pip3 install pyinstaller

Step 2: Compile the Python Program

Navigate to the directory containing your hello.py file and run PyInstaller:

pyinstaller --onefile hello.py

This command will create a dist directory containing the executable file hello.

Step 3: Run the Executable

Navigate to the dist directory and run the executable:

./dist/hello

You should see the same output as before:

Hello, World!

Step 4: Create a Debian Package

To create a Debian package for easy installation, follow these steps:

  1. Install the required tools:
   sudo apt install dpkg-dev
  1. Create the necessary directory structure:
   mkdir -p hello-1.0/DEBIAN
  1. Create a control file inside the DEBIAN directory with the following content:
   Package: hello
   Version: 1.0
   Section: base
   Priority: optional
   Architecture: amd64
   Depends: python3.11
   Maintainer: Your Name <your.email@example.com>
   Description: A simple Hello World program
  1. Copy the executable to the package directory:
   mkdir -p hello-1.0/usr/local/bin
   cp dist/hello hello-1.0/usr/local/bin/
  1. Build the Debian package:
   dpkg-deb --build hello-1.0

This will create a hello-1.0.deb file, which you can install using dpkg:

sudo dpkg -i hello-1.0.deb

You can now run the hello program from anywhere in the terminal:

hello

Conclusion

In this guide, we’ve covered the basics of installing Python 3.11 on Ubuntu 24.04, using pip to manage packages, creating and running a simple Python program, and compiling it into a single executable installer. With these steps, you should be well on your way to developing and distributing Python applications on Ubuntu.

Article Links

Python Programming Language Homepage

Python 3 Documentation

Python 3 PIP