Manim (Mathematical Animation) is a Python library for creating … well mathematical animations. It was developed by Grant Sanderson to help him create awesome math education / exposition videos for his 3blue1brown YouTube channel.
A small community of admirers has developed around manim (we now have a subreddit dedicated to the library as well) who are trying to use the library for in their own projects, or may be just because of the sheer fun of it. I decided to try and install the library on my rather fresh install of Linux Mint 19 Tara (MATE edition). I started at around 3.30pm and was done by 8.30pm. Bulk of this was staring at terminal as it downloaded the seemingly never ending dependencies. Don’t let this discourage you though. I have a very slow internet, your download time is gonna be much shorter.
Here is how it went down.
0. Installing Pre-requisites
As I mentioned above, I was installing this on Linux Mint 19 Tara (MATE edition). The process should be same for Ubuntu, and similar for other distributions. I also had Anaconda-5.3.0 installed.
Credit to bhowell4 for the blog-post which details install process for Mac. I used that post as a building block to start things off.
So let’s install the pre-reqs:
sudo apt install cairo sudo apt install libcario2 libcairo2-dev sudo apt install pkg-config sudo apt install ffmpeg sudo apt install virtualenv
1. Cloning manim git repo and creating virtual environment
Now we are ready to clone manim repository. So let’s go ahead and do it (I used the 2nd-3rd Feb, 2019 version):
git clone https://github.com/3b1b/manim.git
I cloned in the so called “home” folder i.e. path to my manim directory is just ~/manim. IMO this keeps things simple. Now, we need to create a virtual environment inside that directory so we can install python libraries without fear of breaking our system python or libraries packaged with anaconda.
cd manim virtualenv venv --python=python3.7 source venv/bin/activate
2. Installing Python libraries
Now that our environment has been setup we can install python libraries used by manim:
pip3 install sox pip3 install ffmpeg pip3 install latex pip3 install pycairo pip3 install -r requirements.txt
In an ideal world, this should have been it. And we should be able to run following command to render an example scene.
python -m manim example_scenes.py SquareToCircle -pl
But at least in my case it failed with an error.
ImportError: /home/yaseen/manim/venv/lib/python3.7/site-packages/cairo/_cairo.cpython-37m-x86_64-linux-gnu.so Symbol not found: cairo_tee_surface_index
3. Battling libcairo “missing symbol” monster
Digging a little deeper I found that libcairo bundled with Anaconda is not correctly built (at least not for our purpose) and has some missing symbols.
I also found out that my system had 2 installations of libcairo.
libcairo bundled with Anaconda:
/home/yaseen/anaconda3/lib/libcairo.so.2 /home/yaseen/anaconda3/lib/libcairo.so.2 /home/yaseen/anaconda3/lib/libcairo.so.2.11400.12
system libcairo, that we installed in pre-req part:
/usr/lib/x86_64-linux-gnu/libcairo.so.2 /home/yaseen/anaconda3/lib/libcairo.so.2 /home/yaseen/anaconda3/lib/libcairo.so.2.11510.0
Now we can verify which of these installations has the missing symbols: (Run following in manim directory)
strings /venv/lib/python3.7/site-packages/cairo/_cairo.cpython-37m-x86_64-linux-gnu.so | grep cairo_tee_surface_index
returns nothing.
However, doing:
strings /usr/lib/x86_64-linux-gnu/libcairo.so.2.11510.0 | grep cairo_tee_surface_index
returns the correct symbol.
Now, we only need to find a way to use the system libcairo library and bypass anaconda. Luckily, we can do this rather easily. But first we will need to install another package which will help us in doing that:
sudo apt install chrpath
Now go to your virtual environment’s library directory and do this to verify that it links to Anaconda’s libcairo:
chrpath --list venv/lib/python3.7/site-packages/cairo/_cairo.cpython-37m-x86_64-linux-gnu.so
This should return
manim/venv/lib/python3.7/site-packages/cairo/_cairo.cpython-37m-x86_64-linux-gnu.so: RPATH=/home/yaseen/anaconda3/lib/
which confirms that current library is tied to anaconda. We can change it to point to system libcairo as follows:
cd /venv/lib/python3.7/site-packages/cairo/ chrpath --replace /usr/lib/x86_64-linux-gnu _cairo.cpython-37m-x86_64-linux-gnu.so
and to confirm whether the change took effect, run:
chrpath --list _cairo.cpython-37m-x86_64-linux-gnu.so
which should return
manim/venv/lib/python3.7/site-packages/cairo/_cairo.cpython-37m-x86_64-linux-gnu.so: RPATH=/usr/lib/x86_64-linux-gnu
Now go ahead and try to run manim scene again, it should work fine now:
python -m manim example_scenes.py SquareToCircle -pl
The rendered video goes in manim/media directory.
4. Installing LaTeX rendering support
Basic graphics rendering is now working fine. But, we need to do additional work to allow manim to render LaTeX formulas in the videos:
Install the following packages to get basic LaTeX system:
sudo apt install texlive-latex-base sudo apt install texlive-latex-extra sudo apt-get install texlive-fonts-extra sudo apt-get install xzdec
What we are doing in the following commands is use TexLive Manager (tlmgr) to install some packages. Don’t ask me how I came up with the list. What would happen is that I’d try to run code and manim would throw an error in my face, and I’d have to figure out what’s missing. (If you run into a problem running tlmgr command, check this)
tlmgr install tipa tlmmgr install wasysym tlmmgr install physics tlmmgr install ctex
After installing these 4 packages there was still no sign of errors going away. I grew somewhat frustrated, then I came across this answer on StackOverflow and installed the recommended packages:
tlmgr install collection-fontsrecommended tlmgr install collection-latexrecommended
That’s it. Try:
python -m manim example_scenes.py WriteStuff -pl
and it should work now.
Congratulations!
Be First to Comment