Ubuntu 18 how to install latest python 3.7 and list all python versions

Python is installed by default in Ubuntu and Linux Mint. The version which is installed could be a few versions behind the latest stable release. It is a good idea to list all installed versions on your machine and install the latest ones. The latest versions gives many advantages like:

  • new features
  • fixed issues
  • new packages and dependencies

The following article is tested on Ubuntu 18 and Linux Mint 18. The examples are based on Linux Mint.

In this post:

List all installed python versions

If you want to see where and what python versions are installed on your machine you can do it by following commands. Check where python is installed:

which python

and the result is:

/usr/bin/python

Then you can list all python installations there by:

ls -ls /usr/bin/python*

the result of this command is:

   0 lrwxrwxrwx 1 root root       9 Nov 24  2017 /usr/bin/python -> python2.7
   0 lrwxrwxrwx 1 root root       9 Nov 24  2017 /usr/bin/python2 -> python2.7
3412 -rwxr-xr-x 1 root root 3492656 Dec  4  2017 /usr/bin/python2.7
   0 lrwxrwxrwx 1 root root      33 Dec  4  2017 /usr/bin/python2.7-config -> x86_64-linux-gnu-python2.7-config
   0 lrwxrwxrwx 1 root root      16 Nov 24  2017 /usr/bin/python2-config -> python2.7-config
   0 lrwxrwxrwx 1 root root       9 Apr 10 19:43 /usr/bin/python3 -> python3.5
4360 -rwxr-xr-x 2 root root 4464400 Nov 28  2017 /usr/bin/python3.5
   0 lrwxrwxrwx 1 root root      33 Nov 28  2017 /usr/bin/python3.5-config -> x86_64-linux-gnu-python3.5-config
4360 -rwxr-xr-x 2 root root 4464400 Nov 28  2017 /usr/bin/python3.5m
   0 lrwxrwxrwx 1 root root      34 Nov 28  2017 /usr/bin/python3.5m-config -> x86_64-linux-gnu-python3.5m-config
   0 lrwxrwxrwx 1 root root      16 Mar 23  2016 /usr/bin/python3-config -> python3.5-config
   0 lrwxrwxrwx 1 root root      10 Apr 10 19:43 /usr/bin/python3m -> python3.5m
   0 lrwxrwxrwx 1 root root      17 Mar 23  2016 /usr/bin/python3m-config -> python3.5m-config
   0 lrwxrwxrwx 1 root root      16 Nov 24  2017 /usr/bin/python-config -> python2.7-config

Selection_007

Another way to list python installations is by:

whereis python

and the result is:

python: /usr/bin/python2.7-config /usr/bin/python3.5 /usr/bin/python3.5m-config /usr/bin/python /usr/bin/python2.7 /usr/bin/python3.5-config /usr/bin/python3.5m /usr/lib/python3.5 /usr/lib/python2.7 /etc/python3.5 /etc/python /etc/python2.7 /usr/local/lib/python3.5 /usr/local/lib/python2.7 /usr/include/python3.5 /usr/include/python2.7 /usr/include/python3.5m /usr/share/python /usr/share/man/man1/python.1.gz

Alternatively you can see the alternatives with:

update-alternatives --list python

or

update-alternatives --list python3

As you can see from both results we get python 2.7 and 3.5. The latest version of python 3 is 3.7 so if you want to installed it you can check next section. For more information about python 3.7 check here: Python 3.7 new features

Install latest python 3.7 on Ubuntu 18/Linux Mint 18

There are several ways of installing the latest python on Ubuntu:

Install by using PPA repository

There is a PPA which offers many versions of Python:
New Python Versions

This is the description of the repo:

This PPA contains more recent Python versions packaged for Ubuntu.

Disclaimer: there's no guarantee of timely updates in case of security problems or other issues. If you want to use them in a security-or-otherwise-critical environment (say, on a production server), you do so at your own risk.

If you want to install 3.7 in this way you can do it by:

sudo add-apt-repository ppa:deadsnakes/ppa
sudo apt-get update
sudo apt-get install python3.7

After the installation is better to not change the default version for the OS because you can meet many problems and unexpected behavior. Better is to change the version of your IDE or specific programs.

Install by downloading and building from source

First you will need to install some dependencies for the new python version:

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

After that you need to download the version which you want to install. In this case we download Python 3.7 source code from python.org. You can also download the file by visiting the site: python.org donwloads

wget https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tar.xz

Now extract the downloaded package(add sudo if needed).

tar xvf Python-3.7.0.tar.xz

Now enter the newly extracted source directory, configure the build environment and install.

cd Python-3.7.0
sudo ./configure
sudo make altinstall

Note : If you use make altinstall instead of make install you will keep the default python version (otherwise you can face problems).
Step 4 – Check the Python Version

Check the python version.

Python version can be checked in several ways:

python3 -V
python3 --version

or for python 2:

python -V
python --version

for the newly installed 3.7 you can check it by:

python3.7 -V
python3.7 --version

List all python packages

The new versions of python can be delivered with new packages and versions. So you can check the packages by using these commands:

pip freeze
pip list

result

apt-clone==0.2.1
apt-xapian-index==0.47
apturl==0.5.2
bleach==1.5.0
blinker==1.3
Brlapi==0.6.4
chardet==2.3.0

If you are using conda or miniconda you can run:

conda list

If you want to do the same with python code then you can execute:

help('modules')

Related Article