Unable to install TA-Lib on Ubuntu

Question:

I’m trying to install Python Ta-Lib in Ubuntu,but when I run:

pip install TA-Lib

I get this error:

Command "/usr/bin/python -u -c "import setuptools, tokenize;__file__='/tmp/pip-build-YfCSFn/TA-Lib/setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('rn', 'n');f.close();exec(compile(code, __file__, 'exec'))" install --record /tmp/pip-swmI7D-record/install-record.txt --single-version-externally-managed --compile" failed with error code 1 in /tmp/pip-build-YfCSFn/TA-Lib/

I already installed:

sudo apt-get install python3-dev

and installed Ta-lib

How can I fix this?

Asked By: Filipe Ferminiano

||

Answers:

Seem like other people had this problem.

To quote the accepted answer:

Seems that your PiP can’t access Setuptools as per the “import
setuptools” in the error. Try the below first then try running your
pip install again.

> sudo pip install -U setuptools

Or if it doesn’t work to quote his comment:

Try this ‘sudo -H pip install TA-Lib’

As Filipe Ferminiano said in comment if this still doesn’t fix it then you can try what is said on this link .

To quote the accepted answer once again:

Your sudo is not getting the right python. This is a known behaviour of sudo in Ubuntu. See this question for more info. You need to make sure that sudo calls the right python, either by using the full path:
sudo /usr/local/epd/bin/python setup.py install

or by doing the following (in bash):

alias sudo='sudo env PATH=$PATH'
sudo python setup.py install

Here is the question he’s talking about

Please give credit to the one of the accepted answer if it fix your problem.

Answered By: Eric Godard

I am able to load in python3.

Steps:

  1. download from http://prdownloads.sourceforge.net/ta-lib/ta-lib-0.4.0-src.tar.gz

  2. untar tar -xvf ta-lib-0.4.0-src.tar.gz

  3. cd /../ta-lib

  4. ./configure --prefix=/usr

  5. make

  6. sudo make install

  7. sudo apt upgrade

  8. pip install ta-lib or pip install TA-Lib

  9. Check import talib

Answered By: Partha Sen

This has always been a tricky one, but I had made a script that has served me loyally in several Ubuntu physical, VM, and server instances (including GitHub Actions).

It’s a little long but it’s comprehensive and has worked in every Ubuntu instance I’ve needed it for. It includes a few precautionary steps that have previously caused errors.

sudo apt update && sudo apt upgrade -y && sudo apt autoremove -y
sudo apt install wget -y
sudo add-apt-repository ppa:deadsnakes/ppa -y
sudo apt-get install build-essential -y
sudo apt install python3.10-dev -y
sudo apt-get install python3-dev -y

wget http://prdownloads.sourceforge.net/ta-lib/ta-lib-0.4.0-src.tar.gz
tar -xzf ta-lib-0.4.0-src.tar.gz
cd ta-lib

wget 'http://git.savannah.gnu.org/gitweb/?p=config.git;a=blob_plain;f=config.guess;hb=HEAD' -O './config.guess'
wget 'http://git.savannah.gnu.org/gitweb/?p=config.git;a=blob_plain;f=config.sub;hb=HEAD' -O './config.sub'
./configure --prefix=/usr
make
sudo make install

sudo rm -rf ta-lib
sudo rm -rf ta-lib-0.4.0-src.tar.gz

pip install ta-lib

It consists of many steps…

  1. Update built-in packages using apt.
  2. Add the deadsnakes repo.
  3. Install build-essential and python-dev (python3-dev, python3.10-dev).
  4. Download the TA-Lib tarball with wget.
  5. Download the updated make recognizer files to prevent common issues with make and make install.
  6. make TA-Lib and make install it.
  7. Clean up the mess.
  8. Install with pip to make sure everything worked out. (pip will install the latest version of TA-Lib, 0.4.24, even though we can only download the source for 0.4.0. This works fine.)

Because I use this frequently, I turned it into a gist, for the purpose of accessing the script directly with curl.

Just grab a raw link from the Gist page and use it like below.

curl https://gist.githubusercontent.com/preritdas/bunchofrandomstuffhere/install-talib-ubuntu.sh | sudo bash

Make sure you’re sudo activated before running the command to prevent issues. It will run all the above commands as a script and install TA-Lib in about 4-5 minutes (average, in my experience).

Here’s a shell recording of this working on a fresh server instance of Ubuntu 22.04.

asciicast

All in all, I hope this helps; for me, it made a once frustrating and volatile process easy.

Answered By: preritdas
Categories: questions Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.