Wednesday, May 2, 2012

Installing NumPy 1.6 on Python 2.7 in Ubuntu 12.04

I have a fresh install of Ubuntu 12.04. I want to use NumPy, and I'm okay with using the default Python version that ships with Ubuntu 12.04, which is Python 2.7. Note that Ubuntu 12.04 also ships with Python 3 under the 'python3' namespace. 

Go to terminal, check python version

$ python --version
Python 2.7.3

Go into the Python interpreter:

$ python
Python 2.7.3 (default, Apr 20 2012, 22:44:07) 
[GCC 4.6.3] on linux2
Type "help", "copyright", "credits" or "license" for more information.

See if you have NumPy already installed:

>>> import numpy
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named numpy
Nope, it's not there, so we need to install. Exit the Python interpreter.
>>> exit()

Install NumPy using apt-get

Instead of browsing web forums and finding the right package to download and compile, just let Ubuntu's built-in package manager, apt-get, do the work:
$ sudo apt-get install python-numpy python-scipy
Let it have all the dependencies it wants (just type 'yes' when prompted).

See if it worked

To see if it worked, go back into the Python interpreter, import NumPy, create a NumPy array, and make sure it's a NumPy array:
$ python
>>> from numpy import *
>>> type(array([1,2,4,5]))
<type 'numpy.ndarray'>

Yup it worked.

Conclusion

In Ubuntu 12.04, just use apt-get to install NumPy using
$ sudo apt-get install python-numpy python-scipy
Everything just works.

26 comments:

  1. Hi Andrew,

    How did you make those code boxes?

    ReplyDelete
    Replies
    1. Check out http://alexgorbatchev.com/SyntaxHighlighter/

      Delete
  2. This comment has been removed by the author.

    ReplyDelete
  3. Worked great thanks! But where is this installed? When i look under usr/local/lib/python2.7/ its not under sitepackages or distpackages.

    ReplyDelete
  4. i dont know where to type this code"$ sudo apt-get install python-numpy python-scipy" as i'm new to ubuntu. please help me..............
    alos please help me in installing codeblock ide for ubuntu. i'll be your highly greateful.
    thank you.

    ReplyDelete
    Replies
    1. You need to type it in a terminal window. On a PC, press Ctrl-Alt-t, or open a terminal window by searching for in the program list. Then type the command you referenced.

      Delete
  5. but the problem for me I faced this message:
    E: unable to locate package python-numpy
    E: unable to locate package python-scipy

    ReplyDelete
    Replies
    1. I am also getting same type of error..
      It's seems due to proxy i am getting this. you check yours

      Delete
  6. It wasn't so smooth for me. After I typed in the sudo apt-get install python-numpy python scipy, I typed in the followings and I got:

    abcd@abcd-T3640:~$ from numpy import *
    from: can't read /var/mail/numpy

    What went wrong?

    ReplyDelete
    Replies
    1. You need to type that line in a Python interpreter. From your output it looks like you typed it at the shell prompt. Instead, open your Python interpreter by typing the command 'python' at the shell prompt, then do your Python command.

      Delete
  7. I am facing the following message:
    E : unable to locate package python-scipy
    Also I tried to install pandas using the following command:
    sudo easy_install pandas==0.7.3

    to which the output was:
    No local package or download links found for pandas==0.7.3
    error : could not find suitable distribution for Requirement.parse('pandas==0.7.3)

    ReplyDelete
  8. This is what I'm getting.

    >>> sudo apt-get install python-numpy python-scipy
    File "", line 1
    sudo apt-get install python-numpy python-scipy
    ^
    SyntaxError: invalid syntax

    What's the problem?

    ReplyDelete
    Replies
    1. You are running it on the python shell. you should exit first.
      exit()
      Then on the terminal type
      sudo apt-get install python-numpy python-scipy

      Delete
  9. This comment has been removed by the author.

    ReplyDelete
  10. This works for me.. Thanks andrew !

    ReplyDelete
  11. 1.
    python --version
    Python 2.7.2

    2.
    python
    Python 2.7.2 (default, May 28 2013, 11:08:58)
    [GCC 4.4.5] on linux2
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import numpy
    Traceback (most recent call last):
    File "", line 1, in
    ImportError: No module named numpy

    3. sudo apt-get install python-numpy python-scipy
    Reading package lists... Done
    Building dependency tree
    Reading state information... Done
    python-numpy is already the newest version.
    python-scipy is already the newest version.

    4. sudo apt-get install python
    Reading package lists... Done
    Building dependency tree
    Reading state information... Done
    python is already the newest version.

    Summary: NumPy is not found in Python interpreter but apt-get install claims all are the latest version. What to do ?
    David

    ReplyDelete
    Replies
    1. same error here. It says that I already have the newest version but...

      >>> from numpy import *
      Traceback (most recent call last):
      File "", line 1, in
      ImportError: No module named numpy

      Delete
    2. I got the same error also.

      How did you solve this >

      Delete
  12. This comment has been removed by the author.

    ReplyDelete
  13. On "import numpy" (after the install) I get the error:
    ImportError: libatlas.so.3gf: cannot open shared object file: No such file or directory
    Any ideas?

    ReplyDelete
  14. Thanks a lot... It perfectly working

    ReplyDelete