Introduction
Python is a versatile and powerful programming language that has gained immense popularity among developers worldwide. Its simplicity, readability, and extensive library support make it an ideal choice for various applications, from web development to data analysis and machine learning.
This guide explains how to install Python on Ubuntu 24.04.
Prerequisites
Before you start:
- Deploy a Digital Ocean VPS server running Ubuntu 24.04.
- Create a non-root
sudo
user. See our guide on How to Create a Non-Root Sudo User on Ubuntu 24.04.
Install Python from the Deadsnakes PPA
The Deadsnakes PPA (Personal Package Archive) is a popular third-party repository that offers a wide range of Python versions for Ubuntu. DeadSnakes PPA offers more recent versions of Python, including both stable and pre-release versions.
DeadSnakes PPA offers the following benefits:
- Newer Python versions.
- Multiple Python versions.
- Compatibility with modern tools or libraries.
- Pre-release or beta versions of Python.
Follow the steps below to add the Deadsnakes PPA repository to your system and install Python.
-
Add the Deadsnakes PPA to your system.
CONSOLE$ sudo add-apt-repository ppa:deadsnakes/ppa
-
Update your system's package information index.
CONSOLE$ sudo apt update
-
Install
python3
the default Python version.CONSOLE$ sudo apt install python3
-
Ensure you have installed
python3
.CONSOLE$ python3 --version
Output:
CONSOLEPython 3.12.3
Install Specific Python 3.x Version
DeadSnakes PPA allows you to install and manage multiple versions of Python side-by-side. This is particularly useful especially if you want to test your Python code across different Python versions. To install a specific version of Python3, for instance Python 3.8
or Python 3.9
, follow the steps below.
-
Install
python3.8
. Replacepython3.8
with your specific version.CONSOLE$ sudo apt install python3.8
-
Verify that you've installed the correct Python version.
CONSOLE$ python3.8 --version
To install multiple Python versions repeat the above command with the desired versions.
Install pip
pip
is the most popular package manager for Python. It is a command-line tool that allows you to install and manage Python packages from the Python Package Index (PyPI) and other package repositories. You should always install pip
after installing Python to match your Python version to pip
.
-
Install pip for
python3
.CONSOLE$ sudo apt install python3-pip
-
Replace
3.8
if you installed a specific Python3.x
version.CONSOLE$ sudo apt install python3.8-pip
Create a Hello, World!
Application
A Hello, World!
application is a simple program that outputs the text Hello, World!
to the screen. It is often the first program that you should write and run as a beginner to verify that Python works. Now that you've installed Python create and run a simple Hello, World!
application.
-
Create a new
hello-world.py
file using thenano
text editor.CONSOLE$ nano hello-world.py
-
Add the following content to
hello-world.py
file.Pythonprint("Hello, World!")
-
Press Ctrl + X, then Y to save and close the file.
-
Run the file using Python.
CONSOLE$ python3 hello-world.py
If you installed a specific Python
3.x
version, replacepython3
withpython3.x
. For instancepython3.8
.CONSOLE$ python3.8 hello-world.py
Output:
CONSOLEHello, World!
The above output confirms that Python is working as expected.
Conclusion
In this guide, you've learned how to install Python on Ubuntu 24.04 using the Deadsnakes PPA, including installing specific Python 3.x versions, setting up pip
and running a simple Hello, World!
application. After following these steps, you have now set up a Python environment on your Ubuntu system. You can now start developing Python applications.