Introduction
Python is a widely used programming language celebrated for its versatility and powerful features. It is particularly popular among developers for its ease of use and extensive libraries that support everything from web development to data analysis and machine learning.
This guide details the steps to install Python on Ubuntu 22.04.
Prerequisites
Before you begin:
- Deploy a VPS server running Ubuntu 22.04.
- Set up a non-root
sudo
user.
Install Python from the Deadsnakes PPA
The Deadsnakes PPA (Personal Package Archive) is a trusted repository that provides multiple versions of Python for Ubuntu. It includes the latest releases, stable versions, and even pre-release versions.
Benefits of the Deadsnakes PPA:
- Availability of the latest Python versions.
- Support for multiple Python versions.
- Compatibility with modern tools and libraries.
- Access to pre-release or beta versions of Python.
Follow the steps below to add the Deadsnakes PPA and install Python.
-
Add the Deadsnakes PPA to your system.
CONSOLE$ sudo add-apt-repository ppa:deadsnakes/ppa
-
Refresh your system's package list.
CONSOLE$ sudo apt update
-
Install the default Python version.
CONSOLE$ sudo apt install python3
-
Verify the new Python version.
CONSOLE$ python3 --version
Output:
CONSOLEPython 3.10.4
Install a Specific Python 3.x Version
The Deadsnakes PPA lets you install and manage multiple Python versions concurrently, which is beneficial for testing your code across different versions. Here's how to install a specific Python 3.x
version, such as Python 3.8
or Python 3.9
.
-
Install a specific Python version. Replace
3.8
with your desired version.CONSOLE$ sudo apt install python3.8
-
Confirm the new Python version.
CONSOLE$ python3.8 --version
To install multiple Python versions, repeat the steps with the desired versions.
Installing pip
pip
is the go-to package manager for Python, allowing you to install and manage Python packages from the Python Package Index (PyPI) and other repositories. Always install pip
after installing Python to ensure compatibility with your Python version.
-
Install pip for the default Python version.
CONSOLE$ sudo apt install python3-pip
If you installed a specific Python
3.x
version, replace3.8
with your version.CONSOLE$ sudo apt install python3.8-pip
Create a Hello, World!
Application
A Hello, World!
application is a simple program that prints Hello, World!
to the screen. It's often the first program you write to confirm that Python works correctly.
-
Create a new
hello-world.py
file using thenano
text editor.CONSOLE$ nano hello-world.py
-
Add the following code to
hello-world.py
file.Pythonprint("Hello, World!")
-
Save and close the file by pressing Ctrl + X, then Y.
-
Execute the file using Python.
CONSOLE$ python3 hello-world.py
If you installed a specific Python
3.x
version, replacepython3
withpython3.x
, such aspython3.8
.CONSOLE$ python3.8 hello-world.py
Output:
CONSOLEHello, World!
This output verifies that Python works as expected.
Conclusion
This guide shows you how to install Python on Ubuntu 22.04 using the Deadsnakes PPA, including instructions for installing specific Python 3.x versions, setting up pip
, and running a basic Hello, World!
application. After following these steps, you now have a fully functional Python development environment on your Ubuntu system.