How to Install Python on Ubuntu: A Complete Setup Guide
Python is one of the most widely used programming languages in the world, and Ubuntu is one of the most popular Linux distributions for developers, data scientists, and system administrators. Getting Python running on Ubuntu is generally straightforward — but the right approach depends on your Ubuntu version, which Python version you need, and what you plan to do with it.
Does Ubuntu Already Have Python Installed?
Most Ubuntu installations come with Python pre-installed. On Ubuntu 20.04 and later, Python 3 is included by default. You can verify this by opening a terminal and running:
python3 --version If Python 2 is what you're looking for, be aware that Python 2 reached end-of-life in January 2020 and is no longer receiving security updates. Most modern Ubuntu releases no longer ship it by default, and new projects should avoid it entirely.
To check whether python (without the version number) points to anything on your system:
python --version On many Ubuntu systems, this command returns nothing or an error unless you've explicitly configured a default alias.
Method 1: Install Python via APT (The Standard Package Manager)
For most users, the APT package manager is the easiest and most reliable method.
sudo apt update sudo apt install python3 To also install pip — Python's package installer — run:
sudo apt install python3-pip And if you're working in virtual environments (which is strongly recommended for project isolation):
sudo apt install python3-venv This approach installs the version of Python that Ubuntu's official repositories have tested and approved for your specific Ubuntu release. It integrates cleanly with your system, receives security patches through normal system updates, and requires no manual maintenance.
The Trade-off With APT Versions
Ubuntu's repositories don't always carry the absolute latest Python release. For example, Ubuntu 22.04 LTS ships with Python 3.10, while Ubuntu 24.04 LTS ships with Python 3.12. If you need a cutting-edge feature or a very specific version, you may need a different approach.
Method 2: Install a Specific Python Version via the Deadsnakes PPA 🐍
The Deadsnakes PPA is a well-known third-party repository maintained by community contributors that provides multiple Python versions for Ubuntu systems.
sudo apt install software-properties-common sudo add-apt-repository ppa:deadsnakes/ppa sudo apt update sudo apt install python3.12 Replace python3.12 with the version number you need — such as python3.11 or python3.13.
This method is popular among developers who need to test code across multiple Python versions or use a release not yet included in Ubuntu's official repos. It's also useful when working on projects with strict version requirements.
One important note: packages installed this way don't always come with pip bundled. You may need to install it separately:
sudo apt install python3.12-distutils curl -sS https://bootstrap.pypa.io/get-pip.py | python3.12 Method 3: Build Python From Source
Advanced users sometimes compile Python directly from the official source code at python.org. This gives you complete control over the version and build configuration, but it comes with meaningful complexity.
Key steps involved:
- Install build dependencies via APT
- Download and extract the source tarball
- Run
./configure,make, andmake install
This method makes sense in specialized environments — embedded systems, custom server configurations, or situations where you need a build flag that pre-packaged versions don't include. For most development work, it's unnecessary.
Setting Up a Virtual Environment After Installation
Regardless of which installation method you use, working in a virtual environment is considered best practice for Python development on Ubuntu. It keeps project dependencies isolated and prevents conflicts between packages.
python3 -m venv myenv source myenv/bin/activate Once activated, any packages you install with pip stay contained within that environment and don't affect the system Python installation.
Key Variables That Affect Your Approach
| Factor | Why It Matters |
|---|---|
| Ubuntu version | Determines which Python version APT provides out of the box |
| Python version needed | Older or newer versions may require Deadsnakes or source builds |
| System Python vs. project Python | Modifying system Python can break Ubuntu tools that depend on it |
| Number of projects | Multiple projects often means multiple virtual environments |
| Internet access on the machine | Affects whether APT or source builds are more practical |
A Note on Not Overwriting System Python ⚠️
Ubuntu itself uses Python internally — various system tools and package scripts depend on it. Replacing or removing the system Python installation can break your Ubuntu environment. This is why installing an additional version alongside the system one (rather than replacing it) is the safer approach when you need a specific version.
The python3 command that ships with Ubuntu should generally be left as-is. Use virtual environments or version-specific commands like python3.12 to run alternative versions.
Which Python Version Should You Use?
The Python Software Foundation maintains a clear support schedule. Any version marked "security fixes only" or "end of life" on the official Python status page is worth upgrading away from if your workload allows it. Active development versions receive the most attention from library maintainers, which matters when you're working with third-party packages.
That said, the newest version isn't always the right version — some popular libraries and frameworks lag behind the latest Python release by months, meaning a slightly older stable version may offer broader compatibility with your specific tooling stack.
Your Ubuntu version, the Python ecosystem your project depends on, and how much maintenance overhead you're comfortable managing are all factors that point toward meaningfully different setups.