sodcalculator

10 Tech How-To

Install Python 3 on Ubuntu: Step-by-Step

Guide 10-28243 · Updated · Step by step

This guide walks you through installing Python 3 on Ubuntu, covering both the default repository method and the more up-to-date deadsnakes PPA. You’ll end up with a working Python 3 interpreter, pip, and venv, ready for scripting or development.

What You’ll Need

  • An Ubuntu system (desktop or server) with sudo access
  • An internet connection
  • Basic comfort with the terminal (Ctrl+Alt+T on desktop)

Step-by-Step

  1. Check if Python 3 is already installed. Ubuntu often includes Python 3 by default. Open a terminal and run: python3 --version. If it shows something like Python 3.12.3, you’re already set. If not, continue.
  2. Update your package lists. Run sudo apt update to refresh the repository index. This ensures you get the latest available versions.
  3. Install Python 3 from the official Ubuntu repository. Run sudo apt install python3. This installs the default Python 3 version for your Ubuntu release. It’s the safest option for system compatibility.
  4. Verify the installation. Run python3 --version again. You should see the version number you just installed. Also check pip with python3 -m pip --version (if pip isn’t installed, see the common mistakes section).
  5. Install pip and venv (recommended). For managing packages and creating isolated environments, run: sudo apt install python3-pip python3-venv. This gives you the tools to install libraries and manage project dependencies.
  6. Test a simple script. Create a file named test.py with print("Hello, Python!") and run it with python3 test.py. You should see the output.
  7. Optional: Install a newer Python version via deadsnakes PPA. If you need a version newer than what your Ubuntu release provides (e.g., Python 3.13 on Ubuntu 22.04), add the deadsnakes PPA: sudo add-apt-repository ppa:deadsnakes/ppa, then sudo apt update, then install with sudo apt install python3.13. Note: this installs alongside the system Python, so you’ll call it python3.13 specifically.
  8. Set up the python3 command (if you installed from PPA). The PPA version won’t replace your system’s python3 command. To use the new version as your default, you’d need to update your shell’s PATH or create an alias, but be cautious—many system tools depend on the system Python.

Common Mistakes

  • Using python instead of python3. On most Ubuntu systems, python points to Python 2 (or doesn’t exist). Always use python3.
  • Forgetting to install pip separately. On some Ubuntu versions, pip isn’t included with the base python3 package. You need sudo apt install python3-pip.
  • Running sudo pip install. This can interfere with system packages. Use python3 -m pip install --user or, better, create a virtual environment.
  • Mixing up PPA versions. If you install from deadsnakes, you must use the specific command like python3.13, not python3.

Tips

  • Always use virtual environments for projects: python3 -m venv myenv, then source myenv/bin/activate. This keeps dependencies isolated.
  • To upgrade pip, use python3 -m pip install --upgrade pip.
  • If you need multiple Python versions, consider using pyenv for more control, but the deadsnakes PPA is simpler for most users.
  • Check what version your Ubuntu release officially supports at https://packages.ubuntu.com to avoid compatibility issues.

Troubleshooting

  • “Command ‘python3’ not found” after installation. This is rare, but if it happens, try sudo apt install --reinstall python3. Also check that /usr/bin/python3 exists.
  • pip not found. Run sudo apt install python3-pip. If it’s still not found, try python3 -m ensurepip.
  • Permission errors when installing packages. Don’t use sudo. Use python3 -m pip install --user package-name or activate a virtual environment first.
  • Python 3.13 installed but python3 still shows old version. This is expected. Use python3.13 explicitly. To make it the default, you’d need to modify your PATH, but be careful—system scripts may break.
  • “add-apt-repository: command not found”. Install it with sudo apt install software-properties-common.
  • Errors about missing dependencies when installing from PPA. Run sudo apt --fix-broken install to resolve dependency issues.

Frequently Asked Questions

Is Python 3 preinstalled on Ubuntu?

Yes, most modern Ubuntu releases (20.04 and later) include Python 3 by default. You can check with python3 --version.

What’s the difference between python3 and python?

python3 explicitly points to Python 3.x. On Ubuntu, python may point to Python 2 (deprecated) or not exist at all. Always use python3.

How do I install a specific Python version like 3.10?

Use the deadsnakes PPA: sudo add-apt-repository ppa:deadsnakes/ppa, then sudo apt update, then sudo apt install python3.10.

Do I need to install pip separately?

On some Ubuntu versions, pip is not included with the base Python 3 package. Install it with sudo apt install python3-pip.

Can I uninstall the system Python?

No. Ubuntu’s system tools and GUI depend on it. Removing it can break your system. If you need a different version, install it alongside.

How do I run a Python script?

Save your code in a file (e.g., script.py), then run python3 script.py in the terminal from the same directory.

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 →