Python is a versatile and powerful programming language that has become incredibly popular for a range of software development projects, from web applications to data science. Its simplicity and readability make it an excellent choice for both beginners and experienced developers. If you are using a Linux system, installing Python can be straightforward. This guide will take you step-by-step through the process, ensuring you can get started with Python programming efficiently. Moreover, we’ll cover some post-installation steps to verify and configure your Python environment, providing a complete setup experience.
Installing Python on Linux
The Python package is generally included in most Linux distributions. However, the version installed might not be the latest one. Therefore, it is always a good idea to ensure you’re working with the latest stable Python release, or at least the version you need for your project.
Step 1: Check If Python Is Already Installed
Before you begin installing Python, it is wise to check whether it is already installed on your system and what version it is. Open your terminal and type the following command:
python3 --version
or
python --version
The output for this command might look something like:
Python 3.8.10
If you see a similar output indicating a Python 3.x version, Python is already installed on your system. However, if the version is not what you need, or if Python is not installed, you’ll need to proceed with the installation.
Step 2: Update and Install Prerequisites
Before installing Python, it’s a good practice to update your package list to ensure you have the latest information from the repositories. This can be done with the following commands:
sudo apt update
sudo apt upgrade
Next, install the prerequisites needed to compile Python source code. Use the following command:
sudo apt install build-essential libssl-dev zlib1g-dev libbz2-dev libreadline-dev libsqlite3-dev wget curl llvm libncurses5-dev libncursesw5-dev xz-utils tk-dev libffi-dev liblzma-dev python-openssl git
These packages are essential for building Python from source if you choose to go that route.
Step 3: Install Python Using the APT Package Manager
The Advanced Package Tool (APT) is a powerful package management system used by Debian-based Linux distributions. To install the latest version available from the distribution repositories, run the following command:
sudo apt install python3
To ensure the latest version specific to your distribution repositories is installed, you should also install ‘pip’ (Python’s package installer) to manage Python packages:
sudo apt install python3-pip
After the installation, confirm the installed version:
python3 --version
The terminal should reflect the version you installed, for example:
Python 3.9.7
Step 4: Alternative Installation Using pyenv
Sometimes, you might need to install multiple Python versions side-by-side to support different projects. `pyenv` is a great tool to manage multiple Python versions and switch between them as needed.
First, install dependencies:
curl https://pyenv.run | bash
Once installed, ensure that pyenv is set up correctly in your shell configuration file (e.g., .bashrc, .bash_profile, or .zshrc) by adding these lines:
export PATH="$HOME/.pyenv/bin:$PATH"
eval "$(pyenv init --path)"
eval "$(pyenv virtualenv-init -)"
Reload your shell configuration or restart your terminal. Then, you can install the desired Python version:
pyenv install 3.9.7
pyenv global 3.9.7
Again, check the Python version to confirm:
python --version
This method provides the flexibility for managing multiple versions and creating virtual environments tailored to project needs.
Post-Installation Steps
Verify Python Installation
To check whether Python and pip have been correctly installed, use the following commands:
python3 --version
pip3 --version
Ensure both commands return valid version numbers to confirm the successful installation.
Install Virtual Environment
It’s good practice to use virtual environments to manage dependencies specific to your Python projects:
sudo apt install python3-venv
Create a new virtual environment using:
python3 -m venv my_project_env
Activate the virtual environment with:
source my_project_env/bin/activate
In an active virtual environment, installed packages using pip will be contained within it, providing a clean and isolated environment for your project.
Conclusion
By following this guide, you have successfully installed Python on your Linux system. Whether you opted for APT or pyenv, you are now equipped with the tools necessary to explore Python’s extensive functionality. Maintaining an up-to-date Python environment and managing dependencies through virtual environments will serve you well as you develop Python applications. Happy coding!