Install Python on Ubuntu 20.04 or 18.04
This guide will show you how to install Python on Ubuntu 20.04 or 18.04. Python can be installed on Ubuntu either using apt via terminal or using source code.
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. Uninstalling the default python may cause the Graphical Display Manager failed.
You can have many versions of Python on your system. During software development, you can choose which version of Python to use.
Installing Python using apt via Terminal
- Start by updating the package information using the following command:
- Now, install Python3.9 using the following command:
- Verify the installed version of Python using the following command:
sudo apt update
sudo apt-get install python3.9
python3.9 --version
Installing Python using Source Code
- Start by installing the following prerequisites one by one:
- 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.
- After the download is complete, extract the gzipped tarball file.
- Next, navigate inside the Python directory and execute the script to enable Python optimizations as shown below:
- 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.
- 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.
- After the installation is complete, remove the downloaded tar file to free space.
- Next, verify if the Python is installed successfully.
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
sudo wget https://www.python.org/ftp/python/3.9.0/Python-3.9.0.tgz
tar -xf Python-3.9.0.tgz
cd Python-3.9.0
sudo ./configure --enable-optimizations
make -j 8
sudo make altinstall
cd ..
sudo rm -f Python-3.9.0.tgz
python3.9 --version
You must see the version of Python: