Install Python, NumPy, and matplotlib in Your Home Linux Directory

Written: 07/28/13

Last Updated: 07/28/13

I was recently presented with the challenge of needing to create a python environment without having admin privileges; moreover, the system I was using ran CentOS 5. These two things caused this to become quite the challenge. This techtorial is designed to show you how to install Python along with NumPy and matplotlib in your home directory, directly from the source code. It will probably be easier to use easy_install or pip, if it is compatible with your system. I am by no means an expert in this subject, so if you have suggestions let me know in the comments.

Before we begin, there is a prerequisite. You must first have GCC installed. I will not cover how to install it, but if you do not have admin privileges, you will have to install it in your home directory. This should be doable by using the “‐‐prefix” option.

There are also some things to note. We will be creating the directory “~/.local”, which will be used as the default install directory for everything. This directory was selected as Python 2.6 and newer uses it to search for packages. These directions were specifically designed for use with Python 2.7.5. With that in mind, these directions may not work with other versions of Python.

If the server you are using has CPU throttling enabled, you will not be able to install ATLAS. Unless you are the system administrator, there is no way to get around this, as you will most likely need to change BIOS settings (or at the very least require you to run commands as root). This means that it will not be possible to install SciPy, as it requires ATLAS.

Now that we are ready to begin, the first thing we need to do is edit our .bashrc. This file is used to load environment variables, among other things. We are going to add aliases for what will become our Python and easy_install paths. We are also going to add a path to our PATH environment variable. While these steps do not need to occur now, I have found it easier to deal with this before starting the installation process. Append the below code to ~/.bashrc file using your favorite technique. Note that these changes will cause the Python we are going to install to become your default Python build. If you had any previous aliases to Python, you must remove them.

# Set aliases for Python
alias python='~/.local/bin/python2.7'
alias easy_install='~/.local/bin/easy_install-2.7'

# Add local installs to PATH
PATH=$PATH:~/.local/bin

Now “refresh” bash to utilize the updated .bashrc file. This will cause the changes to become instant.

. ~/.bashrc 

CD to your home directory and download Python 2.7.5.

cd ~
wget http://www.python.org/ftp/python/2.7.5/Python-2.7.5.tgz

Extract the contents of the tarbell and CD into the extracted directory.

tar xvfz Python-2.7.5.tgz
cd Python-2.7.5

Build and install Python at the ~/.local directory.

./configure
make altinstall prefix=~/.local exec-prefix=~/.local

CD back to your home directory and remove the installation files.

cd ~
rm -rf ~/Python-2.7.5*

Python 2.7.5 should now be installed. Since we already configured our .bashrc file for the path where we installed Python, simply typing “python” should result in the Python interpreter appearing. If a different version of Python loaded, or Python did not load, execute “which python” and ensure that the path is “~/.local/bin/python2.7”.

Now we are going to install setuptools. These tools contain easy_install. easy_install is not used in this guide; however, setuptools is a dependency for NumPy. Make sure you are in your home directory and then download the latest version of setuptools (0.9.8 at the time of this writing).

cd ~
curl -O https://pypi.python.org/packages/source/s/setuptools/setuptools-0.9.8.tar.gz

Extract the contents of the tarbell.

tar xvfz setuptools-0.9.8.tar.gz

Install setuptools. Note that we did not use the “‐‐user” flag. This installation should choose the correct path by default; however, in general we will run “python setup.py ‐‐user”, which will install the packages in ~/.local.

python setuptools-0.9.8/setup.py install

Remove the installation files.

rm -rf setuptools-0.9.8*

Now we can install NumPy. Make sure you are in your home directory and download the latest version of NumPy (1.7.0 at the time of this writing).

cd ~
wget http://sourceforge.net/projects/numpy/files/NumPy/1.7.0/numpy-1.7.0.tar.gz

Extract the contents of the tarbell.

tar xvfz numpy-1.7.0.tar.gz

Install NumPy.

python numpy-1.7.0/setup.py install

Remove the installation files.

rm -rf numpy-1.7.0*

matplotlib has a couple of other dependencies that we must install – libpng and FreeType. Feel free to explore what those packages include.

Ensure that you are in your home directory and download the latest version of libpng (1.6.3 at the time of this writing).

cd ~
wget http://sourceforge.net/projects/libpng/files/libpng16/1.6.3/libpng-1.6.3.tar.gz

Extract the contents of the tarbell.

tar xvfz libpng-1.6.3.tar.gz

CD into the extracted directory and install libpng.

cd libpng-1.6.3
./configure --prefix=$HOME/.local
make check
make install

CD back to your home directory and remove the installation files.

cd ~
rm -rf libpng-1.6.3*

Ensure you are in your home directory and download the latest version of FreeType (2.5.0 at the time of this writing).

cd ~
wget http://sourceforge.net/projects/freetype/files/freetype2/2.5.0/freetype-2.5.0.tar.gz

Extract the contents of the tarbell.

tar xvfz freetype-2.5.0.tar.gz

CD into the extracted directory and install FreeType.

cd freetype-2.5.0
./configure --prefix=$HOME/.local
make
make install

CD back into your home directory and remove the installation files.

cd ~
rm -rf freetype-2.5.0*

Ensure you are in your home directory and download the latest version of matplotlib (1.2.0 at the time of this writing).

cd ~
wget https://github.com/downloads/matplotlib/matplotlib/matplotlib-1.2.0.tar.gz

Extract the contents of the tarbell.

tar xvfz matplotlib-1.2.0.tar.gz

This installation is not as obvious as the previous ones. We now need to edit the file setupext.py, located in the extracted directory, e.g. “matplotlib-1.2.0/setupext.py”. Open that file in your favorite text editor and find the line that looks something like the following:

basedir = defaultdict(lambda: ['/usr/local', '/usr'], {

In the lambda dictionary, add the path to your .local directory, i.e. $HOME/.local. Make sure you use the full path. Below is an example of what it should look like when you are done.

basedir = defaultdict(lambda: ['/usr/local', '/usr', '/home/james/.local'], {

Once you have made the correct changes to that file, be sure to save and close it. Now we can install matplotlib. This time we must explicitly state the “‐‐user” parameter.

python matplotlib-1.2.0/setup.py install --user

Make sure that you are not in any of the installation directories and then remove the installation files.

cd ~
rm -rf matplotlib-1.2.0*

If all went well, you should now have Python 2.7.5 installed with support for NumPy and matplotlib. One more thing to keep in mind is that in any of the python files you create, you should add the appropriate shebang, as this will allow you to directly run the script, e.g. ./my_awesome_script.py. The shebang should point to where Python is installed. Below is an example of a Python script utilizing the shebang.

#!/home/james/.local/bin/python2.7
# Place the rest of your Python code below the shebang ^^.
# Make sure you change the path to be for your user.
# Remember to make your script executable by doing a "chmod +x your_script.py"
# before trying to execute it with "./your_script.py".
print "Success! (-:"

Discussion (3)

There are 3 responses to “Install Python, NumPy, and matplotlib in Your Home Linux Directory”.

  1. Saleem responded:

    · Reply

    Thank you very much…
    I was so frustrated for not getting this issue resolved…
    Finally here you have posted the exact solution to my worry…
    Thank you..

  2. Thank you. This post is wonderful.

  3. Mary responded:

    · Reply

    Thank you for the detailed steps. Everything went fine until I reached this step for installing freetype-2.8.1 (this is the latest I believe). The step, “./configure –prefix=$HOME/.local”, gives me an error saying:
    builds/toplevel.mk:43: *** FreeType’s build system needs a Make program which supports $(value). Stop.

    Any suggestion how to proceed?

Leave a Reply