How to Install Python on Ubuntu - Python Tutorial | Logic Practice
Python Course / Basics / How to Install Python on Ubuntu

How to Install Python on Ubuntu

BLUF: This lesson on How to Install Python on Ubuntu provides a comprehensive guide to understanding and implementing this concept in Python. Whether you're a beginner or looking to refresh your knowledge, you'll find clear explanations and interactive code examples here.
Key Concept: How to Install Python on Ubuntu

Mastering How to Install Python on Ubuntu is essential for building efficient Python applications. Focus on the syntax and the best practices highlighted in this tutorial.

Python is a robust programming language extensively utilized for tasks such as web development, data analysis, automation, and machine learning. While Ubuntu includes Python by default, it frequently features an older iteration.

This tutorial demonstrates the process of installing the most recent version of Python 3 on Ubuntu.

Looking for other platforms?

  • Install Python on Windows
  • Install Python on Mac
  • Check Current Python Version

Prior to installation, verify whether Python is already present on your system:

Example

python3 --version

If Python is installed, you'll see output like:

Example

Python 3.10.6

Installing Python on Ubuntu

Method 1: Using APT Package Manager (Recommended)

This technique installs Python directly from the official repositories of Ubuntu.

Step 1: Update Package List

Example

sudo apt update

Step 2: Install Python 3

Example

sudo apt install python3

Step 3: Install pip (Python Package Manager)

Example

sudo apt install python3-pip

Step 4: Verify Installation

Example

python3 --version
pip3 --version

Method 2: Install Specific Python Version from Deadsnakes PPA

The Deadsnakes PPA offers more recent versions of Python that are not currently available in the default repositories of Ubuntu.

Step 1: Add Deadsnakes PPA

Example

sudo apt update
sudo apt install software-properties-common
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt update

Step 2: Install Specific Python Version

For example, to install Python 3.12:

Example

sudo apt install python3.12

Step 3: Install pip for the New Version

Example

sudo apt install python3.12-distutils
curl -sS https://bootstrap.pypa.io/get-pip.py | python3.12

Step 4: Verify Installation

Example

python3.12 --version
pip3.12 --version

Method 3: Build from Source (Advanced)

To obtain the most current version or tailored setups, you have the option to compile Python from its source code.

Step 1: Install Dependencies

Example

sudo apt update
sudo apt install build-essential zlib1g-dev libncurses5-dev libgdbm-dev libnss3-dev libssl-dev libreadline-dev libffi-dev libsqlite3-dev wget libbz2-dev

Step 2: Download Python Source

Access https://www.python.org/downloads/source/** to obtain the most recent version, or utilize wget:

Example

wget https://www.python.org/ftp/python/3.12.2/Python-3.12.2.tgz

Step 3: Extract and Navigate

Example

tar -xf Python-3.12.2.tgz
cd Python-3.12.2

Step 4: Configure and Install

Example

./configure --enable-optimizations
make -j $(nproc)
sudo make altinstall

Attention: make altinstall stops the default system python3 from being replaced.

Step 5: Verify Installation

Example

python3.12 --version

Set Python 3 as Default (Optional)

To use python instead of python3:

Example

sudo apt install python-is-python3

Or create an alias in ~/.bashrc:

Example

echo "alias python=python3" >> ~/.bashrc
echo "alias pip=pip3" >> ~/.bashrc
source ~/.bashrc

Test Python Installation

  1. Open Terminal.
  2. Start Python interactive shell:
Example

python3
  1. Run a test:
Example

print("Hello, World!")

Output:

Output

Hello, World!
  1. Exit the shell:
Example

exit()

Installing Virtual Environment (Recommended)

Virtual environments assist in the independent management of project dependencies.

Example

sudo apt install python3-venv

Create a virtual environment:

Example

python3 -m venv myenv
source myenv/bin/activate

You can now install packages independently of the system Python:

Example

pip install package_name

Deactivate the environment:

Example

deactivate

Uninstalling Python (If Needed)

If you installed via APT:

Example

sudo apt remove python3.x
sudo apt autoremove

Replace 3.x with the version number.

In the event that you performed a source installation, please remove the files located in /usr/local/bin/ and /usr/local/lib/ manually.

Conclusion

Python has been successfully installed on your Ubuntu system. You are now ready to begin developing Python applications, installing libraries, and creating projects.

How to Install Python on Ubuntu 1.FAQs

1. Does Ubuntu come with Python?

Indeed, Ubuntu comes with Python 3 pre-installed. Nevertheless, the version may not be the most recent. You can upgrade it using the previously mentioned methods.

2. What is the latest Python version for Ubuntu?

The most recent stable release of Python can be found at https://www.python.org/downloads/**.. You have the option to install it via the Deadsnakes PPA or compile it from the source.

3. How do I check the Python version on Ubuntu?

Run:

Example

python3 --version

4. What is the difference between python and python3?

  • python may point to Python 2 (deprecated) or not exist.
  • python3 points to Python 3 (recommended).

You can set python to point to python3 by installing python-is-python3.

5. How do I install pip on Ubuntu?

Example

sudo apt install python3-pip

6. Should I use APT or build from source?

  • APT: More convenient, quicker, advised for the majority of users.
  • Deadsnakes PPA: Suitable for more recent releases.
  • Compile from source: Intended for experienced users requiring tailored setups or the most up-to-date version.
  • 7. How do I update Python on Ubuntu?

Using APT:

Example

sudo apt update
sudo apt upgrade python3

Utilize the Deadsnakes PPA to install the latest version and modify your scripts to incorporate it.

8. What are virtual environments and why should I use them?

Virtual environments establish separate Python environments for various projects, thereby avoiding dependency conflicts. It is essential to utilize virtual environments during project development.

Input Required

This code uses input(). Please provide values below:

Logic Practice
Install Logic Practice
Add to home screen for a faster app-like experience