List of content you will read in this article:
Python is among the most popular languages with a wide range of applications, from software and web development to data analysis and visualization. Many professionals and students mostly prefer learning Python as it opens up various job opportunities.
If you are among the individuals wishing to learn and practice Python, you will definitely need Python installed on your computer system. But what if you want to practice Python on Ubuntu? Don’t worry! This article will guide you on how to install Python on Ubuntu.
Though most Linux distributions come with Python in the default system packages, you may not find it for some reason.
We will help you with our guide on installing Python on Ubuntu 18.04. However, before jumping straight into the tutorial, let us introduce you to the renowned programming language.
What is Python? [Definition]
Python is an open-source programming language developed in 1991 by Von Hansen in Holland. It is a general-purpose, high-level, and object-oriented scripting language.
The major design philosophy of the language emphasizes code readability. Also, the language has an elegant and simple syntax that uses minimal English keywords. This makes it an easy-to-learn language. Besides ease of use, Python comes with a comprehensive library, and hence sometimes referred to as batteries included.
Unlike other common programming languages, where code blocks are defined in the parenthesis, Python uses whitespace indentation to specify blocks of code. This makes Python code look uncluttered and readable. Indentation means many identical spacing characters at the beginning of each block's lines, and this number increases in the inner code blocks. This way, blocks of code are automatically organized.
Python isn't pre-installed on Windows, although it doesn't rule out the possibility that Windows users will find the lightweight programming language helpful. It's not as simple as upgrading to the most recent version, so be sure you've got the tools for the task.
What Version Would Users Require?
Regrettably, a major upgrade to Python many years ago resulted in a large break among Python releases. It may be a little perplexing for beginners, but don't worry. We'll demonstrate how to set up all of the major ones.
You'll notice the separation immediately when you go to the Python for Windows downloads tab. The registry always asks whether you want the latest Python 2 or Python 3 upgrade at the core, square, and center. Installing and running both versions simultaneously is recommended to get the most out of Python. However, if you already know which version you need for your goals, then stick with that one.
However, using the latest stable version of Python is always better, as it comes with bug fixes, new features, and improvements to existing features.
How to Install Python on Ubuntu?
As stated above, most Linux distributions come with Python in their system packages. So, let us first see whether your Ubuntu system has Python installed or not. If yes, we shall also check its version.
How to Check Whether Python is Installed or Not?
To check whether your Ubuntu system has Python installed or not, press Ctrl + Alt + T to open the terminal.
Now, type python3 and hit Enter. If you receive “bash: python3: command not found” on your screen, it indicates that your system System does not have Python installed.
On the flip side, if there is no such error message and you find ‘>>>’ on your screen, it means that your system has Python installed.
Now, check for the Python version using the following command:
This command provides information about which version of Python your system has.
Let us now look at different methods to install Python on Ubuntu.
Install Python on Ubuntu Using APT
APT stands for Advanced Package Tool. It is a default package manager of Ubuntu. This package manager comes with a Python package that you can download and install on your system. To do so, follow the below steps:
1. Press Ctrl + Alt + T to open the terminal window.
2. Type the following command to update the repository list of the local system.
sudo apt update
3. Use the following command to download the latest Python version.
sudo apt install python3
When you run this command, APT will automatically find the Python package and install it on your system.
Install Python 3 on Ubuntu Using Deadsnakes PPA
Sometimes, the above method of installing Python may not work. If this is the case, you can install Python 3 using the Deadsnakes PPA.
The Deadsnakes PPA allows you to install multiple versions of Python Ubuntu. It consists of more recent Python versions compiled for Ubuntu. Meanwhile, PPA is an acronym for Personal Package Archives. It is a repository specially designed for Ubuntu.
You can add the Deadsnakes PPA to your system’s repository list and install Python. To do so, follow the below steps. However, it is important to note that you cannot directly add the Deadsnakes PPA to your system’s repository. You first need to download the "software-properties-common" package. This package helps you add PPAs on Ubuntu.
1. Download the "software-properties-common" package on your system by using the following command:
sudo apt install software-properties-common
2. Now, add the official link of the Deadsnake PPA to your system’s repository using the following command.
sudo add-apt-repository ppa:deadsnakes/ppa
3. Update your system’s packages.
sudo apt update
4. Now, download the latest version of Python on Ubuntu.
sudo apt install python3
Install Python 3 on Ubuntu From Source Code
Visit the official website and navigate to the “Downloads” section to get Python. You can find out the most recent Python version for Ubuntu. Go ahead and download it.
Untar the file after downloading the tarball. You will see a couple of directories after untarring the file. You'll want to look at the readme file because it contains instructions for installing Python on an Ubuntu VPS.
Please note: All the commands written below must be entered with root access.
Adjust the directory of the untarred Python file and execute the command below in the terminal.
cd ~/<python untarred folder>
Install python command:
sudo make install
It will make the installation of Python as Python3.
If you get an error message or warning when running sudo ./configure, such as no compiler found, install the below library to get rid of it:
apt-get install build-essential
Also, if you get an error message or warning when running, like make not found, j run the below command to install make:
sudo apt install make
After you've installed the above libraries, such as create and build-essential, you should be able to use the install python command.
Another way to install Python is to use the apt-get commands as seen below:
In the terminal, type the following commands:
sudo apt-get update
It will ensure that Ubuntu's repositories are up to date. Then, run the following command to install Python 3:
sudo apt-get install python3
What is the Most Efficient Way to Execute Python Code?
Simply run the following command to run Python code.
Installation of Extra Python Packages
Run the command pip3 list to watch what modules are installed in the application. It will show a list of all the modules currently installed in the environment. If you choose to install additional modules in the environment, such as the request library, you can do so with pip3 install requests. Now, run the pip3 list to see if this request lib is installed in this setting.
Directory as a Package for Distribution
Within the Python project or directory, a file named init.py is used as a delivery kit. As a result, you can make this file with a single-touch button. This file does not need to contain any data; it simply needs to be present in the directory to function as a package.
Utilizing Various Python Versions
You’d have two separate versions of Python on your framework if you used the alt install tool. Each installation comes with its own set of instructions.
Execute commands on every older Python 2.x version on your machine with the python button. Consider the following scenario:
Using python3 to execute a script in the newer version. Consider the following scenario:
You can install different major (3.x or 2.x) versions of Python on your device. Use the second digit to specify which version you choose to use if you have both Python 3.7.x and Python 3.8.x installed:
How to Uninstall Python on Ubuntu?
In case you no longer wish to have Python installed on your Ubuntu 18.04 system, here’s what you need to do:
First, find out what version of Python you have installed by typing:
Let’s say that you have Python 3.9 installed. You can uninstall it using the following command:
sudo apt autoremove python3.9
This was all about installing Python on Ubuntu using different methods. We recommend running all versions simultaneously on Ubuntu if you want to get the most out of Python. It helps us use the most modern Python version while operating older Python scripts and testing functionality for newer projects. However, if you're sure you only need one Python version, you may install just Python 2 or Python 3.
Also, if you wish to use Python on Windows instead, check out our detailed tutorial on how to install Python on Windows.
People Are Also Reading:
One OF my major goals is getting new experiences about ICT and what’s more making progress through this field.