How to Install Python in Ubuntu: A Beginner’s Guide That Actually Works
Feb 03, 2026 5 Min Read 21 Views
(Last Updated)
Python ranks as the most popular programming language today according to the TIOBE Index, making it essential to know how to install Python in Ubuntu if you’re entering the world of programming.
You might be surprised to learn that Python 3 typically comes pre-installed on most Ubuntu versions, especially in 20.04, 22.04, and 24.04. However, understanding how to properly install, update, or manage different Python versions is still a valuable skill. Created by Guido van Rossum in 1990 and named after the comedy “Monty Python’s Flying Circus,” this ubiquitous object-oriented scripting language features an extensive ecosystem that powers countless applications worldwide.
This beginner-friendly guide will walk you through installing Python in Ubuntu, checking existing installations, and setting up your first Python environment. We’ll keep everything simple and straightforward, focusing on practical steps that actually work for beginners. Let’s begin!
Quick Answer:
Python usually comes pre-installed on Ubuntu, but learning how to check, install, and manage Python versions properly ensures a stable system and a smoother start to programming.
Table of contents
- Getting Started With Python in Ubuntu
- 1) Why Python is Pre-Installed on Most Ubuntu Versions
- 2) How to Check if Python is Installed
- How to Install Python the Right Way
- 1) Using apt to install Python 3
- 2) Installing a Specific Version Using PPA
- 3) Installing From Source (Advanced Users)
- Running Your First Python Program
- 1) Using the Terminal to Run Python Scripts
- 2) Using IDLE to Write and Run Code
- 3) Creating a Hello World Script
- Next Steps After Installation
- 1) Set up pip and Virtual Environments
- 2) Follow Python Style Guide (PEP 8)
- 3) Explore Learning Resources and Tutorials
- Concluding Thoughts…
- FAQs
- Q1. Is Python pre-installed on Ubuntu?
- Q2. How can I check which version of Python is installed on my Ubuntu system?
- Q3. What's the easiest way to install a different version of Python in Ubuntu?
- Q4. How do I run my first Python program on Ubuntu?
- Q5. What are some essential next steps after installing Python in Ubuntu?
Getting Started With Python in Ubuntu
When you first boot up your Ubuntu system, you might not realize that Python is already there, ready to use. Unlike many programming tools that require manual installation, Python comes bundled with your operating system. Let’s explore why this is the case and how to verify your Python installation.
1) Why Python is Pre-Installed on Most Ubuntu Versions
Python plays a crucial role in Ubuntu’s core functionality. Many system tools and applications depend on Python to operate properly. Ubuntu developers include Python by default because it powers numerous background processes essential to your operating system.
Each Ubuntu release comes with a specific Python version:
- Ubuntu 24.04 (Noble) includes Python 3.12
- Ubuntu 22.04 (Jammy) ships with Python 3.10
- Ubuntu 20.04 (Focal) includes Python 3.8
Most importantly, experts strongly recommend not tampering with the system’s pre-installed Python version. Modifying or removing this installation can potentially break critical operating system functionalities. As one document notes, “if you mess up Python, it might break Ubuntu”.
2) How to Check if Python is Installed
Before attempting to install Python, first verify whether it’s already available on your system. Checking is straightforward using the terminal:
Press Ctrl+Alt+T to open the terminal
Type one of these commands:
python3 –version
or the shorter version:
python3 -V
The terminal will display your Python version. For example, on Ubuntu 22.04, you might see:
Python 3.10.0
Additionally, you can check if a specific Python version is installed by explicitly mentioning the version number:
python3.12 –version
If Python is installed, these commands will return version information. If not, you’ll receive an error message indicating the command wasn’t found.
Furthermore, it’s worth noting that older Ubuntu installations might have both Python 2 and Python 3. To check for Python 2 (though it’s largely deprecated now), you can use:
python –version
Now that you’ve confirmed your Python installation status, you’re ready to move forward with installation if needed or jump straight to writing your first Python program.
How to Install Python the Right Way
While Ubuntu typically comes with Python pre-installed, there are several methods to install specific versions based on your needs. Let’s explore the most effective approaches to install Python in Ubuntu properly.
1) Using apt to install Python 3
The simplest way to install Python in Ubuntu is through the default package manager, apt. This method is ideal for beginners seeking the standard version available in Ubuntu repositories.
To install Python using apt:
sudo apt update
sudo apt install python3
After installation, verify by typing:
python3 –version
Moreover, you can install essential Python tools alongside:
sudo apt install python3-pip python3-venv
This approach is straightforward but has limitations – primarily that you’ll only get the default Python version for your Ubuntu release.
2) Installing a Specific Version Using PPA
For accessing newer or older Python versions than what’s available in standard repositories, the Deadsnakes PPA (Personal Package Archive) is an excellent solution.
Initially, install prerequisites:
sudo apt install software-properties-common
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt update
Then install your desired Python version:
sudo apt install python3.12
Replace “3.12” with your preferred version. Consequently, this method doesn’t overwrite your system Python, which is crucial for system stability. Each version installs side-by-side, accessible via its specific command (e.g., python3.12).
To switch between versions, you can use the update-alternatives command:
sudo update-alternatives –install /usr/bin/python3 python3 /usr/bin/python3.8 1
sudo update-alternatives –install /usr/bin/python3 python3 /usr/local/bin/python3.9 2
Followed by:
sudo update-alternatives –config python3
3) Installing From Source (Advanced Users)
Alternatively, advanced users might prefer compiling Python from source code for the latest features or specific optimizations.
First, install dependencies:
sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev wget
Download and extract the source:
wget https://www.python.org/ftp/python/3.12.1/Python-3.12.1.tgz
tar -xf Python-3.12.1.tgz
Configure and build:
cd Python-3.12.1
./configure –enable-optimizations
sudo make altinstall
The –enable-optimizations flag improves performance by 10-20%. Using altinstall preserves your system Python installation, avoiding potential system breakage.
To keep things interesting, here are a couple of simple facts about Python and Ubuntu that many beginners don’t realize:
Ubuntu Depends on Python Internally: Several core Ubuntu tools and system utilities are written in Python, which is why Python comes pre-installed and why removing it can break your system.
Multiple Python Versions Can Coexist Safely: Ubuntu allows you to install multiple Python versions side by side, letting developers test and run different projects without affecting the system Python.
These facts explain why Python feels so tightly integrated with Ubuntu and why managing it correctly is an important beginner skill.
Running Your First Python Program
After successfully installing Python in your Ubuntu system, the next exciting step is to run your first program. Let’s explore different methods to execute Python code on your Ubuntu machine.
1) Using the Terminal to Run Python Scripts
The terminal offers a straightforward approach to run Python scripts. First, you need to create a Python file with the .py extension. Once your file is ready, follow these simple steps:
- Open your terminal by pressing Ctrl+Alt+T
- Navigate to your file’s directory using the cd command
- Run your script with: python3 yourscript.py
For frequent usage, you can make your Python script executable:
chmod +x yourscript.py
Next, add a shebang line at the top of your script:
#!/usr/bin/env python3
Subsequently, you can run the script directly with:
./yourscript.py
2) Using IDLE to Write and Run Code
IDLE (Integrated Development and Learning Environment) is Python’s official beginner-friendly IDE. To install IDLE on Ubuntu:
sudo apt-get install idle3
Upon installation, launch IDLE by typing idle3 in your terminal or finding it in your applications menu. IDLE provides two main windows:
- The Shell Window: For interactive Python coding
- The Editor Window: For writing longer programs
To open a new file, select File > New File. After writing your code, save it with File > Save As, then run it by pressing F5 or selecting Run > Run Module.
3) Creating a Hello World Script
Creating your first “Hello World” program is remarkably simple. In your terminal or IDLE editor, create a file named hello.py and write:
print(“Hello, World!”)
This single line tells Python to display the text “Hello, World!” on your screen. The print() function outputs whatever is inside the parentheses.
Running this script will confirm your Python installation is working properly. From this starting point, you can gradually build more complex programs as you continue learning Python.
Next Steps After Installation
Congratulations on setting up Python! Once installed, it’s time to enhance your setup with tools that help you develop better code. Following these next steps will turn your basic installation into a complete Python development environment.
1) Set up pip and Virtual Environments
Pip is the package management system for installing Python libraries and tools from the Python Package Index (PyPI). To install pip, run:
sudo apt install python3-pip
Likewise, virtual environments are essential for creating isolated spaces where each project has its own dependencies. First, install the required package:
sudo apt install python3-venv
Create a virtual environment with:
python3 -m venv .venv
Activate it using:
source .venv/bin/activate
Virtual environments primarily help separate your development projects from the system Python installation, preventing potential conflicts.
2) Follow Python Style Guide (PEP 8)
PEP 8, written by Python’s creator Guido van Rossum and others, serves as the official style guide for Python code. It emphasizes readability through:
- Using 4 spaces for indentation (not tabs)
- Limiting lines to 79 characters maximum
- Following naming conventions (snake_case for variables/functions)
- Organizing imports logically
- Adding appropriate whitespace
Adhering to PEP 8 makes your code more professional and easier for others to read.
3) Explore Learning Resources and Tutorials
To continue your Python journey, check out these valuable resources:
- Python’s official website (python.org)
- HCL GUVI’s Python Course
- Core Python Learning Path on Pluralsight
- Python for Linux System Administration guides
- Interactive Python interpreters like bpython
These resources offer structured learning paths that build upon your new Ubuntu Python installation.
Concluding Thoughts…
Installing Python in Ubuntu might seem intimidating at first, especially for beginners. Nevertheless, as you’ve seen throughout this guide, the process can be straightforward when approached correctly. Python typically comes pre-installed on most Ubuntu distributions, which saves you considerable time and effort when starting your programming journey.
Beyond installation, your Python journey has only just begun. The “Hello World” script represents your first step into a vast ecosystem of possibilities. Python’s versatility allows you to explore web development, data science, artificial intelligence, and countless other fields.
Therefore, take what you’ve learned about installing Python in Ubuntu and build upon this foundation. The programming world awaits your creativity, and Python offers one of the most accessible entry points into this exciting field. Start small, practice regularly, and soon you’ll develop the skills to create increasingly complex applications on your Ubuntu system.
FAQs
Q1. Is Python pre-installed on Ubuntu?
Yes, Python typically comes pre-installed on most Ubuntu versions. For example, Ubuntu 22.04 ships with Python 3.10, while Ubuntu 20.04 includes Python 3.8. However, it’s important to note that you shouldn’t modify or remove the system’s pre-installed Python version as it could potentially break critical operating system functionalities.
Q2. How can I check which version of Python is installed on my Ubuntu system?
To check your Python version, open the terminal and type either “python3 –version” or “python3 -V”. This will display the version of Python currently installed on your system. You can also check for specific versions by using the command “python3.x –version”, replacing x with the version number you’re looking for.
Q3. What’s the easiest way to install a different version of Python in Ubuntu?
The simplest method to install a different Python version on Ubuntu is by using the Deadsnakes PPA (Personal Package Archive). First, add the PPA to your system, update the package list, and then install the desired Python version using apt. For example, to install Python 3.12, you would use the command “sudo apt install python3.12” after setting up the PPA.
Q4. How do I run my first Python program on Ubuntu?
To run your first Python program, create a file with a .py extension (e.g., hello.py) containing your Python code. Open a terminal, navigate to the directory containing your file, and run it using the command “python3 yourscript.py”. Alternatively, you can use IDLE, Python’s built-in development environment, to write and run your code.
Q5. What are some essential next steps after installing Python in Ubuntu?
After installing Python, it’s recommended to set up pip (Python’s package manager) and virtual environments. Install pip using “sudo apt install python3-pip”. For virtual environments, install the package with “sudo apt install python3-venv”. It’s also beneficial to familiarize yourself with the Python style guide (PEP 8) for writing clean, readable code. Lastly, explore various learning resources and tutorials to continue developing your Python skills.



Did you enjoy this article?