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
- 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. - Update your package list. Run
sudo apt updateto refresh the list of available packages from your repositories. This ensures you get the latest versions and avoids installation errors. - 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. - Verify the installation. Run
python3 --versionagain. You should see the installed version (e.g., Python 3.12.3). You can also runpython3 -c "print('Hello')"to test that it works. - 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-commonfirst, thensudo add-apt-repository ppa:deadsnakes/ppa. Press Enter when prompted. - Update again after adding the PPA. Run
sudo apt updateto include the new repository’s packages. - 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 useapt search python3.to list options). - Verify the new version. Run
python3.12 --versionto confirm. Note that the command ispython3.12, notpython3, because the system’s defaultpython3still points to the older version. - Set up pip (Python package installer). For the default Python, run
sudo apt install python3-pip. For a version from the PPA, usepython3.12 -m ensurepipor installpython3.12-distutilsif needed. Test withpip3 --version. - Optional: Install virtual environments support. Run
sudo apt install python3-venvto create isolated environments. This is good practice for project-specific dependencies.
Common Mistakes
- Using
pythoninstead ofpython3. On Ubuntu,pythonmay not be defined. Always usepython3or the specific version likepython3.12. - Overwriting the system Python. Do not replace or alias the default
python3to 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 pipinstead ofpip3.
Tips
- Use virtual environments. For any project, create a venv with
python3 -m venv myenvand activate it withsource 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-commonfirst:sudo apt install software-properties-common. - “Unable to locate package python3.12” – Make sure you ran
sudo apt updateafter 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 updateand try again. If it persists, check your internet connection or sources.list. - Python installs but
pipis missing – Install pip separately:sudo apt install python3-pipfor the default version, or usepython3.12 -m ensurepip --upgradefor PPA versions. - After installing a new version,
python3still 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
- Photonic Inference vs GPU Inference: Key Differences16,384.00 µs
- Android Low Storage Survival Tips That Actually Work
- How to Password Protect PDFs on iPhone
- iPhone Force Restart Not Working? Fixes
- Add or Remove Keyboard Layout in Windows 10
- Razer Barracuda V2 Pro Dongle: Store & Find