Knowledge Base

How to configure and use a newer version of Python

This article describes how to configure and use a newer Python version. You may want to do this if:

  • You have an account on a managed server, and you want to use a version of Python newer than what is installed on the server.
    This applies to all hosting accounts that have cPanel, including shared and reseller servers.
  • You have a semi-managed server, and the package repositories for your Linux distribution contain an older version of Python.

Configuring managed servers

Depending on the managed server where your account is located, there may be an older version of Python installed. If you want to run a newer version of Python, you can compile it from the source code.

Although we have tested and run this configuration, it is not supported.

To compile Python from the source code, follow these steps:

  1. Log in to your account using SSH.
  2. At the command prompt, type the following commands:
    cd ~
    wget https://www.python.org/ftp/python/3.5.2/Python-3.5.2.tgz
    tar xvzf Python-3.5.2.tgz
    cd Python-3.5.2
    ./configure --prefix=$HOME
    make
    make install
    
    These commands demonstrate how to install Python version 3.5.2. To install a different version, please visit https://www.python.org/ftp/python to view the versions that are available for download. After you select a version, run the previous commands with the version number you selected, instead of 3.5.2.
  3. To configure your shell environment to use the new compiled executable, type the following commands. Replace username with your own A2 Hosting account username:

    echo 'alias python3="/home/username/bin/python3.5"' >> ~/.bashrc
    source ~/.bashrc
    
  4. To confirm your account is configured to use the new version, type the following command:

    python --version
Installing virtualenv

If you want to create virtual environments using your new installed Python version, you must first install the virtualenv program. To do this, type the following commands:

cd ~/bin
python3.5 -m pip install virtualenv

You can then create a virtual environment by running the virtualenv program in the bin directory. For example, to create a virtual environment named testenv, type the following command:

~/bin/virtualenv testenv

To activate the new environment, type the following command:

source testenv/bin/activate

Configuring semi-managed servers

The package repositories for your Linux distribution may contain an older version of Python. If you want to run a newer version of Python, you can compile it from the source code. To do this, follow these steps:

  1. Log in to your account using SSH.
  2. As the root user, at the command prompt, type the following commands:
    cd ~
    wget https://www.python.org/ftp/python/3.5.2/Python-3.5.2.tgz
    tar xvzf Python-3.5.2.tgz
    cd Python-3.5.2
    ./configure
    make
    make install
    
    These commands demonstrate how to install Python version 3.5.2. To install a different version, please visit https://www.python.org/ftp/python to view the versions that are available for download. After you select a version, run the previous commands with the version number you selected, instead of 3.5.2.
  3. To configure the shell environment to use the new compiled executable, type the following commands. Replace executable_path with the path to the compiled Python executable (for example, /usr/local/bin):

    echo 'alias python3="/executable_path/python3.5"' >> ~/.bashrc
    source ~/.bashrc
    
  4. To confirm your account is configured to use the new version, type the following command:

    python --version

More Information

For more information about Python, please visit https://www.python.org.