sodcalculator

10 Tech How-To

How to Install Python on Ubuntu Linux

Guide 10-35151 · Updated · Step by step

This guide walks you through installing Python on Ubuntu Linux, covering the built-in version, adding the latest release via the deadsnakes PPA, and verifying the setup. You’ll need a terminal and sudo access to complete the steps.

What You’ll Need

  • An Ubuntu system (20.04, 22.04, 24.04, or similar) with internet access.
  • A user account with sudo privileges (administrator rights).
  • Basic comfort with the terminal (Ctrl+Alt+T opens it).

Step-by-Step

  1. Check if Python is already installed. Open a terminal and run python3 --version. Ubuntu usually includes Python 3, but it may be an older version. If you see a version number, it’s installed; if you get “command not found,” proceed to install it.
  2. Update your package list. Run sudo apt update to refresh the list of available packages from your repositories. This ensures you get the latest versions and avoids installation errors.
  3. Install Python from the default Ubuntu repository. Run sudo apt install python3. This installs the default Python 3 version that comes with your Ubuntu release. It’s stable and integrates well with the system, but it may not be the newest release.
  4. Verify the installation. Run python3 --version again. You should see the installed version (e.g., Python 3.12.3). You can also run python3 -c "print('Hello')" to test that it works.
  5. Install the latest Python (optional but recommended). If you need a newer version than what Ubuntu provides, add the deadsnakes PPA (Personal Package Archive). Run sudo apt install software-properties-common first, then sudo add-apt-repository ppa:deadsnakes/ppa. Press Enter when prompted.
  6. Update again after adding the PPA. Run sudo apt update to include the new repository’s packages.
  7. Install the specific Python version you want. For example, to install Python 3.12, run sudo apt install python3.12. Replace “3.12” with the latest version available (check the deadsnakes PPA page or use apt search python3. to list options).
  8. Verify the new version. Run python3.12 --version to confirm. Note that the command is python3.12, not python3, because the system’s default python3 still points to the older version.
  9. Set up pip (Python package installer). For the default Python, run sudo apt install python3-pip. For a version from the PPA, use python3.12 -m ensurepip or install python3.12-distutils if needed. Test with pip3 --version.
  10. Optional: Install virtual environments support. Run sudo apt install python3-venv to create isolated environments. This is good practice for project-specific dependencies.

Common Mistakes

  • Using python instead of python3. On Ubuntu, python may not be defined. Always use python3 or the specific version like python3.12.
  • Overwriting the system Python. Do not replace or alias the default python3 to a newer version. System tools rely on it. Instead, use version-specific commands.
  • Forgetting to update after adding the PPA. If you skip sudo apt update, the system won’t know about the new packages and will fail to install.
  • Installing pip for the wrong version. If you have multiple Python versions, ensure you’re using the matching pip, e.g., python3.12 -m pip instead of pip3.

Tips

  • Use virtual environments. For any project, create a venv with python3 -m venv myenv and activate it with source myenv/bin/activate. This avoids dependency conflicts.
  • Check the deadsnakes PPA for the latest versions. It usually has the newest Python releases within days of their official launch.
  • Keep the system Python for system tasks. Use the PPA version for development to avoid breaking system scripts.
  • Install build dependencies if you plan to compile Python from source. But using the PPA is simpler and recommended.

Troubleshooting

  • “add-apt-repository: command not found” – Install software-properties-common first: sudo apt install software-properties-common.
  • “Unable to locate package python3.12” – Make sure you ran sudo apt update after adding the PPA. Also, check the exact version number available.
  • “E: Package ‘python3’ has no installation candidate” – Your package list may be outdated. Run sudo apt update and try again. If it persists, check your internet connection or sources.list.
  • Python installs but pip is missing – Install pip separately: sudo apt install python3-pip for the default version, or use python3.12 -m ensurepip --upgrade for PPA versions.
  • After installing a new version, python3 still points to the old one – This is normal. Use the version-specific command (e.g., python3.12) or create an alias in your shell config if you prefer.

Frequently Asked Questions

Is Python pre-installed on Ubuntu?

Yes, most Ubuntu versions include Python 3 by default. You can check with python3 --version.

What’s the difference between python and python3?

On Ubuntu, python may not exist or might point to Python 2 (deprecated). Always use python3 to invoke Python 3.

Should I install Python from the default repo or the PPA?

Use the default repo for stability and system compatibility. Use the deadsnakes PPA if you need a newer version for development.

How do I set the default Python version?

You can use update-alternatives to manage symlinks, but it’s not recommended because system tools may break. Instead, use version-specific commands or virtual environments.

Can I have multiple Python versions installed?

Yes, they coexist fine. Each has its own command (e.g., python3.10, python3.12) and its own pip.

How do I uninstall Python?

Use sudo apt remove python3.12 for a specific version, but be careful not to remove the system’s default Python as it may break your OS.

Related

General steps for current mainstream versions; menus can differ by device and version. See the disclaimer or report an error.

More in this bed

The whole Tech How-To bed →