Installing Python on Ubuntu | Step-by-Step Guide

Python is a popular programming language known for its readability and versatility. If you're using Ubuntu, installing Python is straightforward. This guide will walk you through the process of installing Python on Ubuntu, including checking for pre-installed versions, updating packages, and setting up virtual environments.

Step 1: Update Package List

Before installing any new software, it's a good idea to update the package list on your Ubuntu system. Open the Terminal and run:

Code Example

sudo apt update

Step 2: Check if Python is Already Installed

Ubuntu comes with Python pre-installed. To check if Python is already installed, use the following command:

Code Example

python3 --version

Output

Python 3.x.x

If you see a version number like "Python 3.x.x", it means Python is already installed. If not, follow the next step to install it.

Step 3: Install Python 3

If Python 3 is not installed, you can install it using the following command:

Code Example

sudo apt install python3

Step 4: Verify the Installation

Once the installation is complete, you can verify it by running:

Code Example

python3 --version

Output

Python 3.x.x

Step 5: Install pip (Python Package Installer)

pip is essential for installing Python packages. To install pip, use the following command:

Code Example

sudo apt install python3-pip

Output

pip 21.x.x

Step 6: Setting Up a Virtual Environment (Optional)

To keep your Python projects isolated, you can create a virtual environment using the following command:

Code Example

sudo apt install python3-venv

Then, create a virtual environment with:

Code Example

python3 -m venv myenv

To activate the virtual environment, run:

Code Example

source myenv/bin/activate

Output

(myenv) your-ubuntu:~$

To deactivate the virtual environment, use:

Code Example

deactivate

Step 7: Removing Python (If Needed)

If you need to remove Python, you can do so by using the following command:

Code Example

sudo apt remove python3

You've successfully installed Python on your Ubuntu system. Now you're ready to start your Python journey. Feel free to explore various Python libraries and tools to enhance your programming skills!