Best way to run python 3.7 on Ubuntu 16.04 which comes with python 3.5

Solution 1:

This should get you up and running with Python 3.7 on Ubuntu 16.04

# Install requirements
sudo apt-get install -y build-essential \
checkinstall \
libreadline-gplv2-dev \
libncursesw5-dev \
libssl-dev \
libsqlite3-dev \
tk-dev \
libgdbm-dev \
libc6-dev \
libbz2-dev \
zlib1g-dev \
openssl \
libffi-dev \
python3-dev \
python3-setuptools \
wget

# Prepare to build
mkdir /tmp/Python37
cd /tmp/Python37

# Pull down Python 3.7, build, and install
wget https://www.python.org/ftp/python/3.7.0/Python-3.7.0.tar.xz
tar xvf Python-3.7.0.tar.xz
cd /tmp/Python37/Python-3.7.0
./configure
sudo make altinstall

Then you would just call Python like so:

python3.7 ./yourScript.py

This is a screenshot of multiple versions of Python co-existing in a docker container and how they can be distinguished:

How to call Python different versions

Pip should have been installed with this installation as well. To install packages use this format:

pip3.7 --version

Solution 2:

I would not recommend manually fiddling around with source code installations and paths. Use pyenv and save yourself the trouble.

All you have to do is:

  • Run the pyenv installer
  • Follow the instructions
  • Install the Python versions you need
  • Choose which Python version you want to use for a given directory, or globally

For example, to install 3.7, check which versions are available:

pyenv install -l | grep 3.7

Then run:

pyenv install 3.7.1

Now, you can choose your Python version:

pyenv global 3.7.1

This switches your python to point to 3.7.1. If you want the system python, run:

pyenv global system

To check which Python versions are available, run pyenv versions.