Skip to content

Install python to separate directory on linux in 5 easy steps

by Ben Hepworth on May 18th, 2016

In some cases you may not want to touch the system-wide python installation on a linux box. I recently ran into this where we had a need to install python in it’s own directory. To install it in a separate directory, it is quite simple. Several years ago I wrote about how to install perl in it’s own directory in this post. Surprisingly enough, the first page of Google did not point me in the direction that I wanted. The use cases for this are:

  1. You are developing custom software and need a newer version of python than your server has. You don’t want to mess with the system-wide python that other programs and scripts may depend on.
  2. You want to install some custom python modules and don’t want to touch the system-wide python. I know you can create your own virtaulenv using pip, but what if you need newer functionality or a module is not compatible with the version of python that you have?
  3. The install for this is quite simple. The key is to use the –prefix= option in your configure command. DONE.

    So, for example, my home directory is /home/bhepworth. Let’s say I want to install the latest python, which at the time of writing this is 3.5.1. My python install choice is /home/bhepworth/python3. You can technically install it to any directory you prefer, I’m just using /home/bhepworth as the example. Here are the commands, starting from in my home directory of /home/bhepworth:

    Step 1: Download Python
    wget https://www.python.org/ftp/python/3.5.1/Python-3.5.1.tgz

    Step 2: Uncompress the binaries
    tar -zxvf Python-3.5.1.tgz

    Step 3: Go into the directory with the Python source code
    cd Python-3.5.1

    Step 4: Configure (change /home/bhepworth/python3 to whatever directory you want to install python to)
    ./configure --prefix=/home/bhepworth/python3

    Step 5: Compile and install
    make
    make test (Optional)
    make install

    Verification
    Run python with the -V flag and you will see the version:
    $ /home/bhepworth/python3/bin/python3 -V
    Python 3.5.1

    In my case, the system-wide python is in /usr/bin/python and is on still on the older version, which I can verify by running the same command and using the system-wide python:
    $ which python
    /usr/bin/python
    $ python -V
    Python 2.4.3

    IMPORTANT: Referencing your new and shiny python in your scripts.

    Make sure at the top of any python scripts you write, that you reference your new python and not /usr/bin/python – the first line should look like this:

    #!/home/bhepworth/python3/bin/python3

    Good luck, and happy python-ing!

From → Technology, Work

4 Comments
  1. Jason Page permalink

    Excellent post! I too ran into this issue just the other day while working with my boss. Too bad this post had not yet been written at the time or it really would have saved us some time.

  2. Stop shopping for those products. They arre shortcut
    traps.

  3. Wheen you’re questioning why you should think about doing this with yor own car,
    listed here are some good causes that might tip the
    scales for you.

  4. This excellent website really has all of the information and facts I wanted about this subject and didn’t
    know who to ask. profiles/xgw01/blog/post/167944

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS