Installing a custom version of Python 3

Overview

Python3 is only available on servers running Ubuntu 18 (Bionic). If your server is still running Ubuntu 14 (Trusty), you can install it manually using a Shell user, however, it's recommended that you always install a custom version if you're going to be installing packages as this isolates the environment from the server.

Visit the following link for information on how to confirm the version your server is running.

The following describes how to install Python on Ubuntu 14 (Trusty),  Ubuntu 18 (Bionic), and Debian servers under your website user.

To run the following commands, you must log into your server via SSH with your Shell user. View the following articles for more information:

Installing Python 3 on Ubuntu 18 or Debian

  1. Choose the version you wish to install from python.org.
  2. Log into your server via SSH, and then run the following commands one at a time:
    [server]$ cd ~
    [server]$ mkdir tmp
    [server]$ cd tmp
    [server]$ wget https://www.python.org/ftp/python/3.8.1/Python-3.8.1.tgz
    [server]$ tar zxvf Python-3.8.1.tgz 
    [server]$ cd Python-3.8.1
    [server]$ ./configure --prefix=$HOME/opt/python-3.8.1 [server]$ make [server]$ make install
    • These commands install your local version of python to /home/<username>/opt/python-3.8.1.
  3. Navigate back to your user's home directory:
    [server]$ cd ~
  4. View the creating and editing a file via SSH article for instructions on how to edit your existing .bash_profile. To use the new version of Python over the system default, enter the following line to your .bash_profile:
    export PATH=$HOME/opt/python-3.8.1/bin:$PATH
  5. Save and close the file, and then return to your shell. Run the following command to update this file:
    [server]$ . ~/.bash_profile
  6. Check which version of Python you're now using by entering the following command:
    [server]$ which python3
    /home/username/opt/python-3.8.1/bin/python3

    You can also check the version:

    [server]$ python3 --version
    Python 3.8.1

    If there is no response then the newly downloaded copy is not being used. Most often this is due to the .bash_profile not being updated correctly. Try logging out and back in again. If necessary, repeat the steps above.

Installing Python 3.7.1+ on an Ubuntu 14 server (old VPS machines)

These instructions have been tested up to version 3.8.1.

If you are planning on creating a Django project with this custom version of Python, you will need to migrate to a server running Ubuntu 18 or Debian. Contact DreamHost support in order to migrate your site to a server running one of these operating systems.

Important note about version 3.7.1+ on Trusty servers

If your server is running Ubuntu 14 (Trusty) and you wish to install Version 3.7.1+ of Python, you will need to install a custom version of OpenSSL. This is because newer Python versions require OpenSSL (1.0.2 or newer) which the older Ubuntu servers do not provide. As such, you'll need to also install OpenSSL locally, then configure Python to use this local version of OpenSSL. View the following link for details:

This states the following:

Changed in version 3.6: OpenSSL 0.9.8, 1.0.0 and 1.0.1 are deprecated and no longer supported. In the future, the ssl module will require at least OpenSSL 1.0.2 or 1.1.0

This only applies if your server is running Ubuntu 14. You can tell by running the following command via SSH:

[server]$ cat /etc/issue
Ubuntu 14.04.5 LTS \n \l

If your server is running version 18 (Bionic), you do not need to install OpenSSL and you can follow the directions above instead.

[server]$ cat /etc/issue
Ubuntu 18.04.1 LTS \n \l

Installing OpenSSL

If your server is running Ubuntu 14 (Trusty), view the following article for instructions on installing OpenSSL locally under your Shell user:

Installing

  1. Choose the version you wish to install from python.org.
  2. Log into your server via SSH, and then run the following commands one at a time:
    [server]$ cd ~
    [server]$ mkdir tmp
    [server]$ cd tmp
    [server]$ wget https://www.python.org/ftp/python/3.8.1/Python-3.8.1.tgz
    [server]$ tar zxvf Python-3.8.1.tgz 
    [server]$ cd Python-3.8.1
  3. Change into the 'Modules' directory.
    [server]$ cd Modules
  4. Edit the file titled 'Setup' (or Setup.dist). The section you're looking for is around line #211. View the Editing a file using vim article for instructions.
    [server]$ vim +211 Setup
  5. You'll need to edit line #211-214 by removing the # sign in front of those lines. You must also edit the first line to the path where you installed OpenSSL locally under your user. This assumes you followed the article and installed it in a directory named openssl. Make sure to change username to your Shell user.
    SSL=/home/username/openssl
    _ssl _ssl.c \
            -DUSE_SSL -I$(SSL)/include -I$(SSL)/include/openssl \
            -L$(SSL)/lib -lssl -lcrypto
  6. Change back to the Python-3.8.1 directory. You can now configure and install Python.
    [server]$ cd ..
    [server]$ ./configure --prefix=$HOME/opt/python-3.8.1
    [server]$ make
    [server]$ make install
    • These commands install your local version of python to /home/<username>/opt/python-3.8.1.

    Some users may want to use the --enable-optimizations option when configuring Python. This only functions on machines running Ubuntu 18 (Bionic). If your server is running Ubuntu 14 (Trusty), this will fail to install.

    Additionally, if you're on a Shared web server, this option will cause your user to hit memory limits. Before running on a Shared server, make sure to contact support so they may temporarily remove your memory limitation. This will allow you to complete the install using this option. The installation will also take over 1 hour to complete if this option has been enabled.

  7. Navigate back to your user's home directory:
    [server]$ cd ~
  8. View the creating and editing a file via SSH article for instructions on how to edit your existing .bash_profile. To use the new version of Python over the system default, enter the following line to your .bash_profile:
    export PATH=$HOME/opt/python-3.8.1/bin:$PATH
  9. Save and close the file, and then return to your shell. Run the following command to update this file:
    [server]$ . ~/.bash_profile
  10. Check which version of Python you're now using by entering the following command:
    [server]$ which python3
    /home/username/opt/python-3.8.1/bin/python3

    You can also check the version:

    [server]$ python3 --version
    Python 3.8.1

    If there is no response then the newly downloaded copy is not being used. Most often this is due to the .bash_profile not being updated correctly. Try logging out and back in again. If necessary, repeat the steps above.

Troubleshooting

pip3 error

It's possible you'll see the following error after installing version 3.8.1 when running pip.

pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available.

This means that the local version of OpenSSL you installed was not configured correctly before you configured Python. View the following article for instructions on how to install OpenSSL locally.

Then follow the directions above to reinstall Python3. If you're still seeing the error, contact support.

libssl error when installing Python 3

You may see the following error when configuring Python 3.

checking for X509_VERIFY_PARAM_set1_host in libssl... no

If you run make you'll see the following errors:

libssl is too old and does not support X509_VERIFY_PARAM_set1_host()

Could not build the ssl module! Python requires an OpenSSL 1.0.2 or 1.1 compatible libssl with X509_VERIFY_PARAM_set1_host().

Adding the following line when installing a custom version of OpenSSL to your .bash_profile resolves this.

export LDFLAGS="-L/home/username/openssl/lib -Wl,-rpath,/home/username/openssl/lib"

If you're using Django

If you're using Django, make sure to view the following articles:

See also

Did this article answer your questions?

Article last updated PST.

Still not finding what you're looking for?