Bytes
Data Science

How to Install Python on Linux?

Published: 12th October, 2023
icon

Harshini Bhat

Data Science Consultant at almaBetter

Learn how to install Python on Linux effortlessly. Follow our step-by-step guide for Python installation on various Linux distributions and Get coding now!

Python, a high-level, versatile programming language, has become an integral part of the Linux ecosystem. Its importance cannot be overstated, as it plays a pivotal role in various fields, including web development, data analysis, machine learning, and system administration. In this comprehensive guide, we will explore the process of installing Python on Linux, catering to different Linux distributions, and ensuring you have the right Python version for your specific requirements.

Python's popularity on Linux systems from its simplicity, readability, and the vast ecosystem of libraries and frameworks that make it suitable for a wide range of tasks. Whether you're a software developer, data scientist, or system administrator, Python offers the tools and resources you need to get the job done efficiently.

The versatility of Python extends to its cross-platform compatibility, which makes it an excellent choice for Linux users. You can seamlessly run Python applications on various Linux distributions, ensuring your code remains portable and adaptable.We shall go through the detailed process on how to install Python on Linux.

Prerequisites

Before we begin  installing Python on Linux, it's essential to address a few prerequisites to ensure a smooth installation process.

1. Checking for Existing Python Installations

Before learning how to install python on linux, it's crucial to check if Python is already installed. This is important for several reasons:

Avoiding Conflicts: If multiple Python versions are installed on your system, it can lead to conflicts and unexpected behavior. Identifying existing installations helps you manage and choose the appropriate version.

Compatibility: Some Linux distributions come with Python pre-installed for system maintenance tasks. However, these system Python installations may not be suitable for development or specific applications. Identifying these installations allows you to differentiate between system Python and user-installed Python.

To check for existing Python installations, open your terminal and run the following command:

python --version

This command will display the installed Python version. If you see output, it means Python is already installed. You can also check for Python 3 with:

python3 --version

2. Basic System Requirements

Python is a lightweight and versatile language, making it compatible with a wide range of Linux systems. However, there are some basic system requirements you should be aware of:

Disk Space: Ensure you have enough disk space available for the installation, which typically requires a few hundred megabytes.

RAM: Python itself doesn't have strict memory requirements, but the applications you develop or run using Python may require varying amounts of RAM. Having at least 1 GB of RAM is recommended for most tasks.

Processor: Python is not CPU-intensive, and it can run on a wide range of processors. Any modern processor should be sufficient.

Internet Connection: If you plan to install Python using package managers like APT (Advanced Package Tool) or YUM, you'll need an active internet connection to download and install packages.

3. Opening a Terminal or Command-Line Interface

To proceed with the installation, you'll need to open a terminal or command-line interface (CLI) on your Linux system. The terminal is where you'll enter commands to interact with your system and install Python.

Here's how to open a terminal on some common Linux desktop environments:

  • Ubuntu: Press Ctrl + Alt + T to open a terminal window.
  • GNOME: Press Super (Windows key) and search for "Terminal."
  • KDE: Press Alt + F2, type "konsole," and press Enter.
  • XFCE: Click the "Applications" menu, navigate to "System," and choose "Terminal."
  • Kali Linux: Similar to Ubuntu, use Ctrl + Alt + T.

Other Distributions: The method for opening a terminal may vary slightly depending on your specific Linux distribution, but most use similar keyboard shortcuts or menu options.

With these prerequisites addressed, you're ready to begin the installation process for Python on your Linux system. Next, we'll cover how to install Python on Ubuntu, one of the most popular Linux distributions.

Installing Python on Ubuntu

Ubuntu is one of the most popular Linux distributions, known for its user-friendliness and versatility. In this section, we will cover how to install Python on Ubuntu, both Python 3.x and Python 2.7 for legacy purposes.

Installing Python 3.x on Ubuntu

Python 3 is the recommended version for all new projects as Python 2 has reached its end of life. Here's how to install Python 3.x on Ubuntu using the APT package manager:

Step 1: Update Package Repositories

Before installing any software, it's a good practice to update your package repositories to ensure you're getting the latest package information. Open your terminal and run:

sudo apt update

You may be prompted to enter your password.

Step 2: Install Python 3

Now, you can install Python 3 with the following command:

sudo apt install python3

You may also want to install python3-pip, the package installer for Python, with:

sudo apt install python3-pip

Step 3: Verify Python Installation

To verify that Python 3 is installed successfully, run:

python3 --version

This should display the installed Python 3 version, such as "Python 3.8.10." Python 3 is now installed on your Ubuntu system, ready for use in your projects.

Installing Python 2.7 on Ubuntu

Python 2.7 is deprecated and no longer maintained, but some legacy applications may still require it. Here's how to install Python 2.7 on Ubuntu:

Step 1: Install Python 2.7

Run the following command to install Python 2.7:

sudo apt install python2.7

Step 2: Verify Python Installation- is same as in above

Python 2.7 was officially deprecated and reached its end of life (EOL) on January 1, 2020. The Python community and maintainers have shifted their focus to Python 3, which includes significant improvements, bug fixes, and security updates. Continuing to use Python 2.7 for new projects is strongly discouraged due to the lack of ongoing support and security updates. However, legacy code and applications that rely on Python 2.7 may still require it.

With both Python 3.x and Python 2.7 installed on your Ubuntu system, you have the flexibility to work with a wide range of Python-based software and projects.

Installing Python on Kali Linux

Kali Linux is a specialized Linux distribution primarily used for cybersecurity and ethical hacking purposes. Python is a fundamental tool for scripting, automation, and penetration testing tasks in Kali Linux. In this section, we will discuss Kali Linux's specific requirements for Python installation and provide the necessary commands and steps.

Kali Linux's Specific Requirements for Python

Kali Linux typically comes with Python pre-installed, but you might need to install additional Python packages and libraries for your specific tasks. Additionally, Kali Linux may have some unique requirements due to its focus on cybersecurity:

Python Version: Kali Linux typically includes both Python 2 and Python 3. However, you may want to ensure that the latest Python 3 version is installed for compatibility with the latest Python libraries and tools.

Package Manager: Kali Linux uses the APT package manager, which is similar to Debian-based distributions like Ubuntu. You'll use APT to install and manage Python packages.

Installation Steps

To install or update Python on Kali Linux, follow these steps:

Step 1: Update Package Repositories

First, update the package repositories to ensure you're working with the latest package information:

sudo apt update

Step 2: Install Python 3

To install Python 3, Open your terminal in Kali Linux , run the following command :

sudo apt install python3

Step 3: Verify Python Installation

You can verify that Python 3 is installed successfully by running:

python3 --version

This should display the installed Python 3 version, such as "Python 3.8.10."

Additional Steps for Compatibility with Kali Linux Tools

Kali Linux includes a wide range of cybersecurity tools and libraries. To ensure compatibility with these tools, you might need to install additional Python packages and libraries. Here are some common packages that can be useful:

pip: The Python package installer is essential for installing Python packages and libraries. You can install it with:

sudo apt install python3-pip

Virtual Environments: Consider setting up virtual environments to isolate Python environments for different projects or tools. This can help prevent conflicts between different versions of Python packages.

Specific Python Packages: Depending on your cybersecurity tools and projects, you may need to install specific Python packages or libraries. Always refer to the documentation of the tools you intend to use for any additional requirements.

By following these steps and considering Kali Linux's specific requirements, you can ensure that Python is properly installed and configured for your cybersecurity and ethical hacking tasks. Make sure to keep your system and Python packages updated regularly to maintain security and compatibility with Kali Linux tools.

Managing Python Versions

Python's flexibility and versatility often require developers to work with multiple Python versions for various projects. In this section, we'll explore how to manage multiple Python versions on your Linux system, including the benefits of doing so and how to set a default Python version.

Installing Multiple Python Versions

Benefits of Having Multiple Python Versions

Having multiple Python versions installed on your system offers several advantages:

Compatibility: Different projects and applications may require specific Python versions. By having multiple versions available, you can ensure compatibility with various software.

Legacy Support: Some older projects or libraries may only work with older Python versions like Python 2.7. Installing and managing multiple Python versions allows you to maintain support for legacy code.

Testing: You can test your code against different Python versions to ensure cross-version compatibility and identify potential issues early in development.

Using pyenv or virtualenv

Two common tools for installing and managing multiple Python versions on your Linux system are pyenv and virtualenv:

pyenv: Pyenv is a tool that allows you to easily switch between multiple Python versions and set a specific Python version for a particular project or shell session. It's a great choice for managing different Python versions system-wide.

Installation: Install pyenv by following the instructions in its official documentation.

Usage: Use pyenv to install and switch between Python versions. For example, to install Python 3.9 and set it as the global version, you can use:

pyenv install 3.9.7
pyenv global 3.9.7

virtualenv: Virtualenv is a tool for creating isolated Python environments within your system. It allows you to create project-specific environments with their own Python version and packages.

Installation: Install virtualenv using pip, which should be available if you've installed Python. Run:

pip install virtualenv

Usage: Create a virtual environment and specify the Python version like this:

virtualenv -p python3.9 myenv

Activate the environment:

source myenv/bin/activate

This isolates your project from the system's Python installation.

Setting a Default Python Version

Using Symbolic Links and Alternatives

When you have multiple Python versions installed, setting a default Python version can be beneficial for simplifying your workflow. One common approach is to use symbolic links and the update-alternatives system on Linux:

Symbolic Links: You can create a symbolic link called python or python3 that points to your preferred default Python version. For example, to set Python 3.9 as the default:

sudo ln -s /usr/bin/python3.9 /usr/bin/python

This command creates a symbolic link from /usr/bin/python to /usr/bin/python3.9.

update-alternatives: Some Linux distributions offer the update-alternatives command for managing default versions of commands. You can use it to set the default Python version:

sudo update-alternatives --install /usr/bin/python python /usr/bin/python3.9 1

This command adds Python 3.9 to the alternatives and sets it as the default with a priority

You can use “update-alternatives” to switch between Python versions later if needed.

By following these steps and using symbolic links or alternatives, you can set a default Python version that aligns with your development needs while still having the flexibility to work with multiple Python versions when necessary.

Check out our latest guide "How to Uninstall MySQL in Ubuntu"

Conclusion

In this comprehensive guide, we've explored the essential steps on how to install Python on Linux and various Linux distributions, including Ubuntu and Kali Linux, and managing multiple Python versions using tools like pyenv and virtualenv. The key takeaway is the importance of choosing the right Python version for your specific needs, whether it's Python 3.x for modern development or Python 2.7 for legacy compatibility. Python's versatility and popularity make it an indispensable tool for a wide range of tasks, from web development to cybersecurity. We encourage readers to dive into Python, explore its vast ecosystem of libraries and frameworks, and embark on coding adventures on their Linux systems. Choosing the right Python version is your first step toward unlocking the full potential of this powerful language. Happy coding!

Related Articles

Top Tutorials

AlmaBetter
Made with heartin Bengaluru, India
  • Official Address
  • 4th floor, 133/2, Janardhan Towers, Residency Road, Bengaluru, Karnataka, 560025
  • Communication Address
  • 4th floor, 315 Work Avenue, Siddhivinayak Tower, 152, 1st Cross Rd., 1st Block, Koramangala, Bengaluru, Karnataka, 560034
  • Follow Us
  • facebookinstagramlinkedintwitteryoutubetelegram

© 2024 AlmaBetter