How to Install Python on Ubuntu 26.04 and 24.04

Share

Install Python on Ubuntu 26.04 or 24.04 with APT, or use a separate interpreter when a project needs another version. Python is a programming language used for scripts, applications and automation.

This guide covers installation, virtual environments, pip, alternative versions, updates and troubleshooting. If something does not work, leave a comment or contact us.

Before you begin
  • Use Ubuntu 26.04 or 24.04, a Bash terminal, an internet connection and an account with sudo access.
  • Python is often already installed. Check before adding anything.
  • Do not replace or remove Ubuntu’s system Python. System tools can depend on it.
  • Optional methods install software from third parties or require you to maintain your own builds.

1. Check Ubuntu and Python

Open a terminal and check the Ubuntu release, Python version and the executable selected by your shell:

Terminal
cat /etc/os-release
python3 --version
command -v python3
/usr/bin/python3 --version

A typical Ubuntu 24.04 installation uses Python 3.12 and 26.04 uses 3.14. Patch numbers can differ. If the first Python command fails or selects a different path, the final command checks Ubuntu’s interpreter directly. A virtual environment or version manager may change what your shell finds first.

2. Install Python using APT

Ubuntu’s repository is the simplest choice for most users. Refresh the package list and install the interpreter plus virtual-environment support:

Terminal
sudo apt update
sudo apt install python3 python3-venv

APT reports when the packages are already installed. It updates these packages through Ubuntu’s repositories; it does not promise the newest Python release from python.org. Check the interpreter and run a small example:

Terminal
/usr/bin/python3 --version
/usr/bin/python3 -c "print('Hello from Python!')"

The second line should print Hello from Python!. To run a saved file later, use python3 filename.py. On Ubuntu, python may not exist outside a virtual environment; use python3 or an explicit interpreter path.

3. Create a project and install a package

A virtual environment keeps a project’s Python packages separate from Ubuntu and other projects. Create a new practice directory. If it already exists, choose a different name rather than using an existing project:

Terminal
mkdir "$HOME/python-example" && cd "$HOME/python-example" &&
/usr/bin/python3 -m venv .venv &&
source .venv/bin/activate &&
python --version && python -m pip --version

The prompt normally starts with (.venv); pip should report a path inside the project. If a step fails, stop and fix it before continuing. The environment uses the interpreter selected when it was created.

Install a small example library into the activated environment and test it:

Terminal
python -m pip install requests
python -c "import requests; print(requests.__version__)"

The command prints the installed Requests version. Use python -m pip so pip belongs to the selected interpreter. Install only packages you trust. To leave the environment, run deactivate; to return later, enter the project directory and run source .venv/bin/activate again.

If pip reports externally-managed-environment, create and activate a virtual environment instead of changing Ubuntu’s system packages. Avoid sudo pip and --break-system-packages on a normal host. For standalone Python command-line tools, consider pipx.

4. Choose another Python version

A project may require a newer or older minor version than Ubuntu provides. Check the project’s documented version and dependency support first. A newer Python release can require updates to libraries or native extensions.

uv manages separate user-owned interpreters and is a useful choice for development. A community PPA uses APT but adds a third-party repository and its maintenance policy. A source build offers build control but you must rebuild for security updates. None of these methods should replace /usr/bin/python3.

5. Install another version with uv

The official uv documentation offers a standalone installer. This downloads and runs third-party code as your user and may change shell startup files. Inspect the installer before running it:

Terminal
UV_INSTALLER=$(mktemp) && curl -LsSf https://astral.sh/uv/install.sh -o "$UV_INSTALLER" &&
less "$UV_INSTALLER"
Terminal
sh "$UV_INSTALLER"

Install curl with sudo apt install curl if needed. Quit less with q, then run the second block only if you trust the script, in the same terminal. Open a new terminal after installation if uv is not found. Select a version supported by your project; this example uses Python 3.15:

Terminal
uv --version
uv python install 3.15
uv python find --managed-python 3.15

uv obtains managed CPython builds from python-build-standalone, not Ubuntu’s package archive. Availability depends on version and CPU architecture. Create a separate environment in a new directory:

Terminal
mkdir "$HOME/python-315-example" && cd "$HOME/python-315-example" &&
uv venv --python 3.15 .venv &&
source .venv/bin/activate && python --version

Use a new directory if the example name exists. Check the printed version before installing project dependencies. To update uv when installed with its standalone installer use uv self update; check uv’s Python guide for managed interpreter updates and recreate project environments after changing interpreter versions.

6. Optional: Use a community PPA

The Deadsnakes PPA supplies some additional Python versions. It is community maintained, not an official Ubuntu repository, and does not guarantee timely security updates. Check the PPA page for your release, architecture and desired version before adding it. Do not use a repository for a different Ubuntu release.

If the required version is listed, add the repository and inspect the candidate. This example requests Python 3.11; replace the version only after checking availability:

Terminal
sudo apt update
sudo apt install software-properties-common
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt update
apt-cache policy python3.11 python3.11-venv

Proceed only if both packages have suitable candidates for your Ubuntu release and architecture. Review the repository and installation transaction:

Terminal
sudo apt install python3.11 python3.11-venv
/usr/bin/python3.11 --version

Use /usr/bin/python3.11 -m venv .venv in a new project directory to select that interpreter. Keep Ubuntu’s default /usr/bin/python3 unchanged. APT updates PPA packages while the repository remains enabled, subject to its maintainer publishing updates.

7. Optional: Build Python from source

Use this method when you need custom build settings. It takes more time and disk space, and APT will not update the result. This example installs Python 3.15.2 into its own prefix. Check the current source releases and the matching release checksum before choosing another version.

Terminal
sudo apt update
sudo apt install build-essential curl xz-utils libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev libffi-dev liblzma-dev libncurses-dev libgdbm-dev libgdbm-compat-dev uuid-dev tk-dev

Create a fresh build directory and download the source. The checksum below is specific to the 3.15.2 XZ archive and should match the official release page. Stop if the check fails; a checksum checks the file against that published value, and signed verification provides a stronger independent authenticity check.

Terminal
mkdir "$HOME/python-build-3.15.2" && cd "$HOME/python-build-3.15.2" &&
curl -fLO https://www.python.org/ftp/python/3.15.2/Python-3.15.2.tar.xz &&
echo "8b5536d280142ecf6cdfe15d12a21ba1c65e300bbf7b28a8d0cdb849575797de  Python-3.15.2.tar.xz" | sha256sum -c

Continue only if the output says OK. If the build directory exists, choose another name and enter it. Check that /usr/local/python-3.15.2 does not exist before continuing. The install step needs sudo:

Terminal
test ! -e /usr/local/python-3.15.2 && test ! -L /usr/local/python-3.15.2 &&
tar -xf Python-3.15.2.tar.xz && cd Python-3.15.2 &&
./configure --prefix=/usr/local/python-3.15.2 --with-ensurepip=install &&
make -j2 && sudo make altinstall

Stop if any command fails. Do not run the remaining steps if the prefix check fails. The private prefix and altinstall leave Ubuntu’s system interpreter in place. Verify the result and important optional modules:

Terminal
/usr/local/python-3.15.2/bin/python3.15 --version
/usr/local/python-3.15.2/bin/python3.15 -c "import ssl, sqlite3, bz2, lzma, ctypes; print('Modules available')"

A missing module usually means its development headers were absent at configure time. Install the appropriate headers and rebuild. To update a source build, verify the new release and build it into a new, version-specific prefix; migrate and test projects before removing the old one.

8. Update and remove Python safely

For Ubuntu and enabled PPA packages, refresh package information with sudo apt update and use sudo apt upgrade after reviewing the proposed system-wide upgrades. For a project, activate its environment before updating a dependency with python -m pip install --upgrade PACKAGE. Review compatibility and test the project first.

To remove a uv-managed interpreter, first list and inspect installations with uv python list --only-installed, then use uv python uninstall 3.15 only when no project needs it. Existing environments may stop working and need recreation. Consult uv’s command reference for exact matches.

To remove the PPA example, review sudo apt remove python3.11 python3.11-venv and the packages APT proposes to remove before accepting. Disable the PPA only after checking whether other installed packages depend on it. For a source install, back up anything needed and inspect its exact private prefix before removing it. Do not remove Ubuntu’s python3 package or delete shared system directories.

Troubleshooting

1   Python command not found

Try /usr/bin/python3 --version. If it is missing, install python3 with APT. If only python is missing, use python3 or activate your project environment.

2   venv reports ensurepip is missing

Install the matching venv package, such as python3-venv for Ubuntu’s default interpreter or the versioned PPA package for its interpreter. Then retry in a fresh environment directory.

3   Wrong version or package not found

Run command -v python3, python3 --version and, within an activated environment, python -m pip --version. Activate the intended project environment and check the interpreter used to create it.

4   APT cannot locate a PPA package

Check apt update errors, the Ubuntu codename, architecture and the PPA’s actual package list. Do not switch to another release’s repository.

Frequently Asked Questions

Is Python already installed on Ubuntu?

Many Ubuntu installations include Python 3. Run python3 –version to check your machine.

Does APT install the latest Python release?

APT installs the version available for your Ubuntu release and enabled repositories. It may not be the newest upstream version.

Why does pip say the environment is externally managed?

Ubuntu protects its system Python packages. Create and activate a virtual environment for project dependencies.

Can I install multiple Python versions?

Yes. Use a separate managed interpreter, a suitable PPA package, or a private source prefix, and select it for each project.

Should I change the system python3 command?

No. Ubuntu tools can depend on its packaged interpreter. Use an explicit path or a project virtual environment instead.

Related guides: Visual Studio Code on Ubuntu | Install curl on Ubuntu | Install Node.js on Ubuntu.