As a developer or data scientist, you may need to install Python on your Ubuntu Linux machine. Python is a popular programming language that is used for various purposes such as web development, data analysis, artificial intelligence, and more. In this article, I will guide you through four different methods to install Python on Ubuntu Linux.
Check If Python is Already Installed on Ubuntu
Before you start the installation process, it’s important to check if Python is already installed on your Ubuntu Linux system. To do this, open a terminal window and type the following command:
python --version
If Python is already installed, you will see its version number printed on the screen. If it’s not installed, you will see an error message.
Install Python in Ubuntu from Official Repository
The easiest way to install Python on Ubuntu is by using the official repository. Ubuntu comes with Python pre-installed, but it may not be the latest version. To install the latest version of Python, follow these steps:
- Open a terminal window and update the package list:
sudo apt-get update
- Install Python by running the following command:
sudo apt-get install python3
- Verify the installation by typing the following command:
python3 --version
You should see the version number of the installed Python.
Install Python in Ubuntu from Deadsnakes PPA
If you want to install a specific version of Python that is not available in the official repository, you can use the Deadsnakes PPA. This PPA provides packages for various versions of Python, including the latest stable release.
- Open a terminal window and add the PPA to your system:
sudo add-apt-repository ppa:deadsnakes/ppa
- Update the package list:
sudo apt-get update
- Install the desired version of Python by running the following command:
sudo apt-get install python3.x
Replace “x” with the desired version number.
- Verify the installation by typing the following command:
python3.x --version
You should see the version number of the installed Python.
Build Python in Ubuntu from the Source Code
If you want complete control over the installation process or need to customize Python, you can build it from the source code. Building Python from the source code requires some knowledge of the Linux command line and may take some time.
- Download the source code of the desired version of Python from the official website.
- Extract the source code to a directory of your choice.
- Open a terminal window and navigate to the directory where you extracted the source code.
- Install the required dependencies by running the following command:
sudo apt-get install build-essential libssl-dev zlib1g-dev libncurses5-dev libncursesw5-dev libreadline-dev libsqlite3-dev libgdbm-dev libdb5.3-dev libbz2-dev libexpat1-dev liblzma-dev tk-dev libffi-dev
- Configure the build process by running the following command:
./configure --enable-optimizations
This command configures the build process with optimizations enabled.
- Build Python by running the following command:
make -j$(nproc)
This command compiles Python from the source code. The “-j$(nproc)” option specifies the number of CPU cores to use for the build process.
- Install Python by running the following command:
sudo make altinstall
This command installs Python without replacing the system Python, which is important for stability.
- Verify the installation by typing the following command:
python3.x --version
You should see the version number of the installed Python.