Please note that Python 3 is already preinstalled with Ubuntu 18.04. Nevertheless here and instructions on how to install, update and uninstall it.
How to install the latest python
Before installing the latest version of python check the version of python currently installed on your machine. To do so you can run:
python3 -V
or
python3 --version
If there are several versions installed on your machine you can run the following command to view all versions:
apt list --installed | grep python
If you want to install the latest version of python3 on your Ubuntu18 machine you can run:
sudo apt-get install python3
or if it’s already preinstalled, you can run the command below to update to the latest version.
sudo apt-get upgrade python3
However, if you want to install it manually you can use the source code. To do so, follow the instruction below.
How to install python from source
To install the latest python from source, get the download link of the latest version from python’s official website and download it to your Ubuntu machine. To do so you can run:
wget https://www.python.org/ftp/python/3.7.2/Python-3.7.2.tgz
Once it is downloaded, extract the archive using the command below:
tar -xvf Python-3.7.2.tgz
Change your directory to the newly created Python3.6.5 folder by running:
cd Python-3.7.2
and run the configure script to check the build
./configure
Once the check has been completed run the following commands in the mentioned order to install python 3.6.5
sudo make
sudo make install
However, if there wasn’t need to use zlib1g-dev
package before, you’ll get an error of it’s absence, which will look like this:
zipimport.ZipImportError: can't decompress data; zlib not available Makefile:1099: recipe for target 'install' failed make: *** [install] Error 1
To install the missing package run:
sudo apt install zlib1g-dev
After package installation run the sudo make
and sudo make install
commands once more. After installation is completed check the installed version with the following command:
python3.7 -V
Output must look like this:
Python 3.7.2rc1
How to update python to latest version
To update python to the latest version, you can run:
sudo apt-get upgrade python3
or if you have installed manually from source, you must uninstall the previous version and install the latest version or the one you want. The steps of how to do so are described in the section below.
How to uninstall python
To uninstall python from your Ubuntu 18 machine, run:
sudo apt remove python3.7
Note that you cannot mention major python3 version as it is being used by the system, but you can uninstall minor version which is preinstalled on your machine.
If you have installed python from source and need to update it, you must uninstall the previous one and repeat the installation steps which are described above in this article. As this method if installation locates python files in /usr/local/bin
path, you must remove python files from that path. To do so you can run the following commands:
sudo rm /usr/local/bin/py* sudo rm /usr/local/bin/pip* sudo rm /usr/local/bin/idle* sudo rm /usr/local/bin/2to3* sudo rm /usr/local/bin/easy_install-3.7
Once all files are removed you can proceed to the installation of newer version.