Skip to main content

Installing Python on Linux Mint

Do you want to Python program on Linux Mint? Welcome! Python is a programming language that's widely used for web development, data analysis, artificial intelligence, scientific computing, and more. Linux Mint, being a derivative of Ubuntu, shares its package management system, which makes installing Python a breeze. In this tutorial, we'll walk through the process of installing Python on your Linux Mint system, ensuring you have everything you need to start coding in no time.

Introduction to Python

Python is a high-level, interpreted programming language known for its readability and simplicity. It was created by Guido van Rossum and first released in 1991. Python's design philosophy emphasizes code readability with its notable use of significant whitespace.

Why Python?

  • Easy to Learn: Python has a simple syntax that is easy to grasp, making it an excellent choice for beginners.
  • Versatile: It can be used for a wide range of applications, from web development to machine learning.
  • Large Community: Python has a vast and active community, which means a wealth of libraries, frameworks, and resources are available.
  • Cross-Platform: Python can run on various operating systems, including Windows, macOS, and Linux distributions like Linux Mint.

Python 2 vs Python 3

When talking about Python, it's essential to understand the difference between Python 2 and Python 3. Python 2 was officially discontinued in 2020, so it's highly recommended to use Python 3 for all new projects. Linux Mint typically comes with Python 3 pre-installed, but we'll verify this and ensure you have the latest version.

Step 1: Checking for Existing Python Installations

Before installing Python, let's check if it's already installed on your system. Open a terminal window (you can search for "Terminal" in your applications menu) and type the following commands:.

python --version
python3 --version

You should see output similar to this:.

Python 3.8.10
Python 3.9.1

The first command checks for Python 2, while the second checks for Python 3. If Python 3 is installed, you'll likely see a version number like 3.x.y. If Python 3 is not installed or you see an error message, proceed to the next step to install it.

Step 2: Updating System Packages

It's always a good idea to update your package lists and upgrade existing packages before installing new software. Run the following commands to ensure your system is up to date:.

sudo apt update
sudo apt upgrade

These commands refresh your local package index and upgrade all of your system's packages to their latest versions.

Step 3: Installing Python 3

Even if Python 3 is pre-installed, you might want to install the latest version or additional Python packages. To install Python 3 and its essential development packages, execute:.

sudo apt install python3 python3-pip python3-dev

This command installs Python 3, pip (Python's package installer), and the development headers needed for compiling Python modules.

Step 4: Using Python Virtual Environments

Virtual environments are a best practice in Python development. They allow you to manage dependencies for different projects separately. To create a virtual environment, you'll need the python3-venv package:.

sudo apt install python3-venv

Now, let's create a virtual environment for a new project:.

python3 -m venv myprojectenv

Replace myprojectenv with the name you want for your virtual environment. To activate the virtual environment, run:.

source myprojectenv/bin/activate

Your command prompt should now indicate that you are working inside the virtual environment (e.g., (myprojectenv) $). To deactivate the virtual environment and return to the system's default Python interpreter, simply run:.

deactivate

Step 5: Verifying the Installation

With Python installed, let's verify that everything is working correctly. Run the Python interpreter with the following command:.

python3

You should see the Python interactive shell, which looks something like this:.

Python 3.x.y (default, Jun 08 2021, 10:04:34)
[GCC 9.3.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>>

You can exit the interactive shell by typing exit() or pressing Ctrl+D.

Step 6: Installing Additional Packages with pip

pip is the package manager for Python. You can use it to install packages from the Python Package Index (PyPI). For example, to install the popular requests library for making HTTP requests, run:.

pip3 install requests

Always ensure you're using pip3 to install packages for Python 3.

Step 7: Keeping Python Updated

To keep Python and pip updated, you can use the following commands:.

sudo apt update
sudo apt install --only-upgrade python3 python3-pip

This will upgrade Python 3 and pip to their latest versions available in the Linux Mint repositories.

Usage Examples

Now that you have Python installed on your Linux Mint system, let's explore a few usage examples to get you started.

Running Python Scripts

To run a Python script, open the terminal and navigate to the directory where your script is located. Use the cd command to change directories. For example, if your script is in the Documents directory, run:

cd Documents

Once you are in the correct directory, execute the script using the python3 command followed by the script name. For instance, to run a script named hello.py, enter the following command:

python3 hello.py

Interactive Python Shell

You can also use Python interactively by launching the Python shell in the terminal. Type python3 in the terminal and press Enter. This will open the Python shell, where you can experiment with Python code and execute it line by line.

python3
print("Hello, World!")

Press Ctrl + D or type exit() to exit the Python shell.

Troubleshooting Common Issues

  • Permission Denied: If you encounter permission issues when installing packages with pip, ensure you're not using the system's Python interpreter. Instead, work within a virtual environment or use sudo cautiously.
  • SSL Certificate Errors: If you run into SSL certificate errors with pip, you may need to update your Certificates. You can do this by running sudo update-ca-certificates.

You now have Python 3 installed on your Linux Mint system, along with the tools and knowledge to manage virtual environments and Python packages. Attempt to always use virtual environments for your projects to avoid dependency conflicts.

For further reading and resources, check out the official Python documentation. Thanks and Enjoy your Python journey on Linux Mint!