Python is a versatile and popular programming language known for its ease of use and wide range of applications. Whether you are a beginner or an experienced developer, learning how to install Python on Linux/Ubuntu is essential. This guide will walk you through the process of installing Python on your Linux/Ubuntu system.
1. Checking Pre-installed Python
Most Linux distributions, including Ubuntu, come with Python pre-installed. To check if Python is already installed on your system, open the terminal and run:
python3 --version
If Python is installed, you will see the version number. If not, follow the steps below to install it.
2. Installing Python on Ubuntu
There are several methods to install Python on Ubuntu. We will cover the most common ones.
Method 1: Using the APT Package Manager
The APT package manager is the easiest way to install Python on Ubuntu.
Step 1: Update the Package List
First, update the package list to ensure you have the latest information on the newest versions of packages and their dependencies:
sudo apt update
Step 2: Install Python
To install Python, run the following command:
sudo apt install python3
You can verify the installation by checking the Python version:
python3 --version
Method 2: Using deadsnakes PPA
The deadsnakes PPA (Personal Package Archive) allows you to install multiple versions of Python.
Step 1: Add the deadsnakes PPA
sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt update
Step 2: Install the Desired Python Version
For example, to install Python 3.9, run:
sudo apt install python3.9
Verify the installation:
python3.9 --version
Method 3: Building Python from Source
Building Python from source is useful if you need the latest version or specific build options.
Step 1: Install Dependencies
First, install the required dependencies:
sudo apt update
sudo apt install build-essential libssl-dev zlib1g-dev libncurses5-dev libnss3-dev libgdbm-dev libreadline-dev libffi-dev curl libbz2-dev
Step 2: Download Python Source Code
Go to the official Python website and download the latest source code. For example, to download Python 3.10.2:
curl -O https://www.python.org/ftp/python/3.10.2/Python-3.10.2.tgz
tar -xf Python-3.10.2.tgz
cd Python-3.10.2
Step 3: Configure and Install
Configure the build environment and install Python:
./configure --enable-optimizations
make -j 4
sudo make altinstall
Verify the installation:
python3.10 --version
3. Managing Multiple Python Versions
If you have multiple Python versions installed, you can use the update-alternatives
command to manage them.
Step 1: Update Alternatives
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.8 1
sudo update-alternatives --install /usr/bin/python3 python3 /usr/bin/python3.9 2
Step 2: Choose the Default Version
sudo update-alternatives --config python3
Select the desired version from the list.
Conclusion
Installing Python on Linux/Ubuntu is a straightforward process, whether you use the APT package manager, the deadsnakes PPA, or build from source. By following this guide, you can ensure that you have the appropriate Python environment set up for your development needs.