Let’s explore Python and Pip in this post and learn “How to install python in Linux“. We will discuss the latest version available today for Python (3.9.1) and pip (20.3.3). Also, I will explain about risks of uninstalling Python packages shipped by default with ubuntu 20.04.
I am confident, even if you are new to Python, you will be able to install it easily, following this post step by step. So let’s deep dive in.
What are Python and pip
Python is a simple, Object-oriented, interpreted, high-level programming language with dynamic semantics. Due to its non-complicated syntax and fast edit-test-debug cycle, the Programmer community loves it. Python also reduces the cost of software management due to its simple nature.
Pip is a package manager for python, it allows you to install and manage additional libraries and dependencies that are not distributed or part of the standard library.
Pre-requisite to install Python 3.9
To install Python 3.9.1 in the Linux system, below mentioned are the pre-requisite
- A system with Ubuntu 20.04
- Internet access
- A user account with sudo root access
How to check Python and pip version
Approximately all Linux versions are shipped with python by default. Earlier it was Python2.x but now everyone is moving towards Python3. Depending upon your distro, it may be the n-1 or older version of Python. I will recommend you to check the existing version of Python already installed in ubuntu 20.04.
The same doesn’t apply to the pip package. You may need to install pip in Ubuntu 20.04 or your Linux distro. Let’s first start with checking the existing version of python and pip as follows –
1.) Open Terminal in Ubuntu
Press <Ctrl + Alt + t> on your keyboard to open terminal.
2.) Run python3 command to check the default version
Run python3 –version command in ubuntu terminal.
$ python3 --version
As you can see in image, Python 3.8.5 is already installed in my Ubuntu 20.04.
3.) Check for existing pip version in ubuntu 20.04
Run pip –version command to check, whether pip is also installed.
$ pip --version OR $ pip3 --version
So pip is not installed by default in Ubuntu. The error reported as “command pip not found” or command ‘pip3’ not found in our example.
While writing this article, Python 3.9.1 is the latest version available. I will show you the correct way to install Python 3.9.1 and pip 20.3.3 in Linux.
How to install Python on Linux using apt
Step1- Install supporting additional packages
Install software-properties-common package, which provides an abstraction of used apt repositories. It allows you to manage distribution and independent software vendors by adding or removing PPAs.
$ sudo apt install software-properties-common
Step2- Add deadsnakes ppa repository to install latest Python 3.9
Deadsnakes repository contains the latest Python version. Run add-apt-repository command to add deadsnakes ppa to ubuntu 20.04.
$ sudo add-apt-repository ppa:deadsnakes/ppa
Press Enter to continue adding the repository.
Step3- Update ubuntu repository
Run apt command to update or refresh ubuntu repository after adding new deadsnakes ppa.
$ sudo apt update
Step4- Install latest Python 3 (Version 3.9.1)
$ sudo apt install python3.9
Type “Y” and press Enter to install the package.
Step5- Check python version
$ python3.9 --version
Step6- Open python and run your first program
Open python and type print(“My First Python Program”) in python3.9 console and check the output. It will print the statement “My First Python Program“.
$ python3.9
Python 3.9.1 (default, Dec 8 2020, 02:26:20)
[GCC 9.3.0] on linux
Type “help”, “copyright”, “credits” or “license” for more information.
print(“My First Python Program”)
My First Python Program
Type exit() or press <Ctrl + D> to come out from python shell.
How to install the latest python3-pip in Linux
By default, python3-pip is not installed in Ubuntu 20.04 and installing it from apt will install the old pip package. So let’s see step by step installation of the latest python3-pip version (20.3.3). It will be a two-step process, first, we will install pip 20.0.2 using the apt repository. Then, download and install the latest pip package i.e. 20.3.3 version.
Step7- Install python3-pip package using apt command
$ sudo apt install python3-pip
Step8- Check python3-pip version
Here pip version 20.0.2 got installed and we need to upgrade it to version 20.3.3. Installing package 3.8 from apt will help to meet all dependent packages and libraries which will be required for pip 20.3.3. If you will skip this step, you may get dependent modules error.
Step9- Install curl command first.
If curl is already installed in your system, you can skip this step. Most of the Ubuntu 20.04 don’t have curl installed by default. So use apt install command to install it. Curl is required to execute step 10.
$ sudo apt install curl
Step10- Download pip from bootstrap.pypa.io website
Now you need to download get-pip from the bootstrap.pypa.io website using the curl command as shown in the image.
$ curl https://bootstrap.pypa.io/get-pip.py -o get-pip.py
Step11- Upgrade python3-pip version to pip-20.3.3
Run the python3.9 command to execute the “get-pip.py” package file you downloaded. It will automatically download and install the latest pip in your Ubuntu.
$ sudo python3.9 get-pip.py
Step12 (optional)- Add pip3.9 directory to PATH.
This can be achieved by editing /etc/environment files using your favourite editor. Otherwise, exporting and appending the “PATH” variable for the local user profile will also do the trick. Make sure you add ~/.local/bin/ in PATH variable.
$ export PATH=~/.local/bin/:$PATH
Step13- Check pip version
$ pip3.9 --version
Congrats!! till this point, you have installed the latest Python 3.9.1 and pip 20.3.3 successfully.
Further, I want to draw your attention to the problems of uninstalling the default python package. So keep reading, it will definitely save your from breaking your system in future.
How to uninstall Python3 from Ubuntu 20.04
I always provide uninstallation steps of all software in my posts. But I will ask you to avoid this in the case of python. If you will try removing the default python shipped with ubuntu, it will break your system.
Below mentioned is the list of dependent packages shown in the image. These packages will also remove with the uninstall of default Python 3.8. Which is shipped with Ubuntu 20.04 and it will break our system.
$ sudo apt-get remove python3.8
You can keep more than one version of python in your Linux distro. So there is no problem keeping the default Python3.8 and the latest Python3.9 version. You can simply change the default path or alias if required. Additionally, if you work on different projects which, require a different version of Python. I will recommend using a virtual environment (Python–venv).
To avoid all kinds of mess with your system and to perform testing like me. I will suggest you install VMware workstation player in your system and install ubuntu 20.04 on it.
How to Fix or restore, if you have broken your system already while removing the default python package?
In case you have tried removal of python using the “apt-get remove python” command and broke your system. Follow these steps to restore –
- Press <Ctrl + Alt + F1> to get shell or boot from ubuntu iso image into rescue mode with networking.
- Then try reinstalling python and gnome again.
-
$ sudo apt-get install python3-all
-
$ sudo apt-get install python3-all
- Try reinstalling ubuntu-desktop again, in case still facing issues due to package mess up.
sudo apt-get install --reinstall ubuntu-desktop
Video Tutorial
If you don’t like reading, watch out for this quick video on “How to install the latest Python and pip in Ubuntu 20.04“.
Conclusion
You have learned “How to install Python in Linux“. We used ubuntu 20.04 as a Linux host and installed the latest Python 3.9.1 and PIP 20.3.3. Also learned about risks and aftermath of uninstalling default Python shipped with ubuntu 20.04. Nowadays, Microsoft also recommends installing development software on WSL (windows subsystem for Linux).
Happy Programming.