Install Python on Ubuntu 20.04 | 18.04

Follow the steps below to install Python, pip, and virtualenv on Ubuntu:

Installing Python on ubuntu

Installing Python on Ubuntu is very simple and will only take a few minutes. If you already have other versions of python installed then it is recommended that you do not remove them, as there are many system packages of Ubuntu, such as Graphical Display Manager that provides graphical login is dependent on the default python 2. Unstalling the default python may cause the Graphical Display Manager failed.

You can have many versions of Python on your system. You can choose which version of Python to use during software development.

  1. Start by installing prerequisites
        sudo apt-get install build-essential checkinstall
        sudo apt-get install libreadline-gplv2-dev libncursesw5-dev libssl-dev libsqlite3-dev tk-dev libgdbm-dev libc6-dev libbz2-dev libffi-dev zlib1g-dev
  3. Use wget Command to download the latest stable version of Python from the Python's official site. At the time of writting this tutorial, the latest stable version of Python was 3.9.
        sudo wget
  5. After the download is complete, extract the gzipped tarball file.
        tar -xf Python-3.9.0.tgz
  7. Next, navigate inside the Python directory and execute the script to enable Python optimizations as shown below:
        cd Python-3.9.0
        sudo ./configure --enable-optimizations
  9. Configure the number of processors to make the Python build time faster. This is done by modifying the -j as shown below. My system has 8 cores so I am using 8. You can find the number of cores your system has by typing nproc.
        make -j 8
  11. After the configuration is complete, install Python binaries. Do not use the standard make install command as it will override the default system binary of python 3.
        sudo make altinstall
  13. After the installation is complete, remove the downloaded tar file to free space.
    cd ..
    sudo rm -f Python-3.9.0.tgz
  15. Next, verify if the Python is installed successfully.
        python3.9 --version

    You must see the version of Python:

    Python 3.9.0
  17. After installation of Python, you must install pip for Python3.9. Pip is a package management utility tool that simplifies installation and management of software packages from Python Package Index (PyPI) and other package indexes. To install pip for Python3.9, execute the following commands one by one:
        sudo apt update
        sudo apt install python3-pip
        pip3 --version
  19. After the installation of the Pip, you must next install the virtual environment . The Virtual Environment tool helps to create an isolated environment for a project. Use the following command to install virtual environment for Python:
        pip3 install virtualenv
  21. Verify the installation of the virtual environment by using the following command:
        virtualenv --version

    You will see the installed version of the virtual environment if it is installed on your system.