Setting up Django and Python 2.7 on Red Hat Enterprise 6 the easy way

Recently, I needed to get Django installed with Python 2.7 on Red Hat Enterprise Linux 6. As this is not a directly supported activity, I wanted to document how I went about it. As you might imagine, the generally expected method for install would be to grab the Python 2.7 source tree and then build it. Obviously, that can be a lot of work; is not particularly repeatable; and, potentially, exposes you to more security flaws. As a result, I decided to try to leverage a “new’ish” technology developed (in the open) by Red Hat called Software Collections. An in depth discussion of Software Collections is for another post, for now we just need to know that Software Collections are rpms that contain all (or most) of their supporting libraries, install under /opt, are updatable through yum, and, the core software collections code (scl-utils) is supported by Red Hat. A number of collections have been created and released by the community at

OK, getting started. I created a new VM using a RHEL 6.3 image on an instance of RHOS (Red Hat Open Stack), which is still in preview status (and available to anyone who signs up here) but works well to try this out. First weirdness I ran into was that there was a sudoers file (/etc/sudoers) but sudo (the binary) wasn’t actually installed. So:

yum install sudo

and, back in action. Now we can add a repo for the Python 2.7 scl (short for “Software Collection”) by creating/editing a file in /etc/yum.repos.d. For the software collections, I prefer having one file, scl.repo, and adding all the repos I use to it (YMMV).

sudo vi /etc/yum.repos.d/scl.repo
c/p from (or follow the link from the Fedora SCL page) into the file

If you want to be all slick and one-liney…

sudo sh -c 'wget -qO- >> /etc/yum.repos.d/scl.repo'

I don’t want to actually include the repo file here as it may change over time. You could also just wget the file and put that in repos.d but I like having all the scl info in one place and so I create one repo file with all the repos in it.

Now we install very easily using:

sudo yum install python27

then we get:

scl -l
[me@localhost ~]$ scl enable python27 bash
[me@localhost ~]$ python -V
Python 2.7.3

Now, even though there is a software collection for mysql 5.5, Django works with any version of mysql after 5.0.3. As a result, we are going to stick with the one in RHEL 6 default.

sudo yum install mysql-server mysql

OK.. looks like everything should be installed correctly, now we can install Django
To install Django I want to use pip as that is what the Django folks recommend. You may need to pass the LD_LIBRARY_PATH to sudo because, depending on your configuration, sudo may be configured to strip any LD paths out of your sudo environment by default.

Easy way to check for this is to:

sudo sudo -V (as root, run sudo, with “version” as an option)

If you see “LD_*” or something similar under “Environment variables to remove” then you know you need to pass it. The error you will get is basically python being unable to find its linked libraries. You could also avoid all of this by backing up a bit, su’ing as root directly, and then running scl enable as root.

sudo LD_LIBRARY_PATH=$LD_LIBRARY_PATH easy_install pip
sudo LD_LIBRARY_PATH=$LD_LIBRARY_PATH pip install django

Django also requires a library called MySQLdb which is available as an rpm on RHEL 6. However, the normal installation method of this library is not sufficient for our needs. As a result, we need to install using pip. However, this is a binary install and requires not only gcc but also the mysql header files to build. The next two lines should get it installed for you.

sudo yum install gcc mysql-devel
sudo LD_LIBRARY_PATH=$LD_LIBRARY_PATH pip install MySQL-python

Now you are ready to proceed with Django. We won’t be covering how to use or run Django in this post as it is not really any different than it would be normally. You can find a number of docs that cover this subject. However, be sure that you have “scl enabled” python 2.7 before running any of these operations.
For example:

scl enable python27 bash startproject mytest_django
(modify to point at your database, enable admin, etc)
python runserver {insert your IP here}:8000

In a future post, we will cover running Django with Apache’s httpd.

  • sudo LD_LIBRARY_PATH=$LD_LIBRARY_PATH pip django
    should be
    sudo LD_LIBRARY_PATH=$LD_LIBRARY_PATH pip install django

    • Arggh, I do that all the time. Thanks for the fix!

      • Jan

        Here, too: sudo LD_LIBRARY_PATH=$LD_LIBRARY_PATH pip MySQL-python


        • I think that is what is there, am I missing something?

      • Indeed – as Jan mentions below the “pip MySQL-python” also needs to be “pip install MySQL-python”. But that’s just a nitpick, please accept my thanks for the article – got django installed on my server in a snap thanks to this! Very much appreciated

  • soo

    Thank you so much. I was looking for ways to install Django on RedHat Linux and simply did not know how to. I think I just got it installed… This is great post!

  • Pingback: instalar python 2.7 centos mediante yum()

  • Any ETA on that article covering how to serve Django with Apache’s httpd?

    As of today, Python 2.6 is end of life (, so those of us with a security background are very interested in deciding if RedHat Software Collections are the way forward.

    It would help me greatly to be able to create a proof of concept running Python 2.7, Django and Apache with mod_wsgi.


  • Pingback: DBA_MYSQL_LINUX_Perl » CentOS 6下快速安装Python27()

  • Thanks a lot man.

  • Pingback: Our Top 12 Blog Articles of 2013 | Red Hat Developer Blog()

  • Peter Lunge

    Hi Langdon, thanks for your very helpful blog. I’m new to RedHat and trying to set up a Django+Apache+MySQL stack on RedHat 6.5. Did you ever get to write a blog regarding “running Django with Apache’s httpd” as you mentioned at the end of this blog?

  • Peter Lunge

    Also, it’d be great if you could describe how to update MySQL from 5.0.x to 5.5 using similar approach as above. Thank you.

    • Do you mean “upgrade mysql” or do you mean “use the mysql 5.5 collection with django”? If the latter, it should be very similar to

      • Peter Lunge

        Hi Langdon,

        Thank you for the reply. I’ve been searching for a solution to upgrade MySQL from 5.0 (or whichever is shipped with RHEL 6.5) to 5.5. It’d be nice if you could post a short blog about that because in my case, I had to rely on other resources like . That one actually gave me an error that reads “mysql51-mysqld: unrecognized service”, so I had to find another help online and finally found one that works at .

        BUT THEN, when I try to use your approach above to install Django’s MySQLdb library, it gave me error like below:

        –> Finished Dependency Resolution
        Error: mysql55 conflicts with mysql-5.1.71-1.el6.x86_64
        Error: mysql55-libs conflicts with mysql-libs-5.1.71-1.el6.x86_64
        You could try using –skip-broken to work around the problem
        You could try running: rpm -Va –nofiles –nodigest

        I believe it’s because the webtatic blog, which I followed to upgrade MySQL, didn’t tell me remove/wipe out old MySQL instances. Now, I’m stuck in this trouble for a couple of days (and didn’t feel like going through the trouble to figure out anymore except to wait for an answer to my question on StackOverflow . If you could help me answer (in addition, to posting a blog about how to upgrade MySQL on RHEL 6.x and possibly showing how to install Django MySQLdb) how I might be able to resolve the above error, that’d be awesome! Thank you very much.

      • Peter Lunge

        Seems like this blog editor leaves out URLs. I’m going to try posting them again in order (not sure if it’ll work again. If not, sorry for spamming your blog).
        RedHat instructions that I tried to follow in the first place and got error in upgrade MySQL:

        Webtatic approach that worked for MySQL upgrade:

        Finally, my StackOverflow question:

        Thank you.

        • I think I am a little confused. I think you may have installed an external repo’s mysql55 (webtatic) and followed the instructions from the RHSCL version of mysql55? Or maybe the reverse? I think this might be getting a little long for the comments section on a blog post. I think maybe we should move the discussion to the Customer Portal Forums and benefit from the support engineers and the rest of the Red Hat Community.

          • Peter Lunge

            Thanks for the reply, Langdon. I followed the instructions from the RHSCL and that didn’t work. Then I tried webtatic’s approach and it seems to work (until I tried to install Python’s MySQLdb).

            I actually thought about asking it on customer portal forums. I even have an account registered on RedHat for that purpose, and when I go to customer portal forums, I don’t see a way to post my question (or maybe I cannot find it because my account’s level is not supposed to do so?). If you could let me know how, that’d be great. Thanks again for replying to my question.

  • Pingback: Red Hat Developer Newsletter – December 2013 | Red Hat Developer Blog()

  • Peter Lunge

    Hi Langdon,
    Is there a repo from RedHat to upgrade MySQL that comes with RHEL 6.5? I’ve been looking on the internet (Stackoverflow, #rhel on IRC channel) and tried everything I can get a hold of, but keep failing at upgrading MySQL as well as installing MySQLdb as you described above. Since I’m not a RedHat subscription user, I cannot post anything on Customer Portal (as you suggested in above comment). Where can I get help (ask questions) regarding this issue online? Or is paying for RedHat subscription is the only way to get help? Thank you.

  • Peter Lunge

    Another naive question: what if I’d like to avoid typing “scl enable python27 bash” every time before I use Python 2.7? In other words, if I’d like to use Python 2.7 as the only default Python (as opposed to Python 2.6 that comes with RHEL 6.5), how can I go about it? Thank you.

  • I have been trying to install Python 2.7.3 from the Software Collections Library using the instructions in this post. However, the command “sudo yum install python27” has been failing for me since at least January 8, 2014.

    The system seems to be aware of the repo but fails to download any packages from it.

    [root@localhost ~] yum repolist
    Loaded plugins: fastestmirror
    Loading mirror speeds from cached hostfile
     * base:
     * extras:
     * updates:
    scl_python27                                                    | 2.9 kB            00:00
    scl_python27/primary_db                                 | 23 kB             00:00
    repo id                        repo name                                                      status
    base                           CentOS-6 - Base                                            4,802
    extras                         CentOS-6 - Extras                                               12
    scl_python27             Python 2.7 Dynamic Software Collection              36
    updates                     CentOS-6 - Updates                                           236
    repolist: 5,086
    [root@localhost ~]# sudo yum install python27
    Loaded plugins: fastestmirror
    Loading mirror speeds from cached hostfile
     * base:
     * extras:
     * updates:
    Setting up Install Process
    No package python27 available. 
    Error: Nothing to do.
    [root@localhost ~]# 

    My system is a CentOS 6.4 i386 virtual machine on a Windows 8 (64-bit) host. What might be a possible cause of this problem?

    • Sounds to me like the rpms moved from the repo they were in. Obviously, I don’t know a whole lot about the current status of libraries in CentOS repos, but, as I recall, they were in a dev/test state. Maybe they moved to main? Maybe it is worth looking for the files using a web browser and see if they are still there? Check out /etc/yum.repos.d.

      • I’ve solved the problem. It wouldn’t install on i386 because the bkabrda repository you used only has x86_64 rpms for Python. It installed without problems on a 64-bit virtual machine.

        Thanks for your help.

  • Pingback: mysql connector installed in python 2.6, but not on 2.7 |

  • Jack

    On RHEL6, I can successfully install python, but after running scl enable python27 and typing python -V, I get “error while loading shared libraries: cannot open shared object file: no such file or directory”. Any suggestions?

    • Jack

      Hmm. Seems I had to add /opt/rh/python27/root/usr/lib64 to my LD_LIBRARY_PATH. That was unexpected from the post above.

  • hi Langdon White, its work, but how I can make the 2.7 version of python permanet?
    its because all the time with I reboot my system I need to recommand “scl enable python27 bash”
    thank you

    • I finally realized that we had not done a post on this. Please see “.”

  • marquessbr

    hi Langdon
    its work but How I can make the python 2.7 permanent in my system?

  • Bill

    Hi Langdon, great post. One questions though:

    Does this change the system version of python, or create an alternate version of python?

    • Creates an alternate version, however, when you run “scl enable” the app context is changed so the app “thinks” the system version has changed.

  • Mary

    Hi Langdon, It keeps installing pip to the default system Python, what did I miss?

    • Sounds like you didn’t do “scl enable python27 bash” (or similar) before running the pip line.

  • on RHEL 6.3 AWS I had to
    sudo yum install python-devel before pip installing mysql-python

  • I’m setting up django on a MediaTemple DV instance which via cat /proc/version identifies itself as Red Hat 4.4.6-4, but the SCL biz isn’t working quite as expected with sudo (LD_* is listed under env_vars to remove):

    $> echo $LD_LIBRARY_PATH
    $> sudo LD_LIBRARY_PATH=$LD_LIBRARY_PATH easy_install pip
    sudo: easy_install: command not found
    $> which python
    $> sudo LD_LIBRARY_PATH=$LD_LIBRARY_PATH which python

    I can work around it using sudo su and enabling the python27 SCL, but it’s annoying — if anyone is aware of a fix, let me know =)

    • Is it actually RHEL 4? I am surprised it is working at all :/

      Did you “scl enable python27 bash” before the easy_install? Kinda looks like you did, but just checking. Otherwise, maybe add more path vars to the sudo command? maybe try adding “-E” (preserve env)?

  • Pingback: Automated deployment of RPM-packaged django applications with Red Hat Software Collections | Red Hat Developer Blog()

  • Ollie Cook

    Hi, Any news on the article about running Django 1.7+ under Python 2.7 on CentOS 6? Django 1.6 is now out of extended support since April 1 2015 ( and does not support Python 2.6. Thank you!

    • Ollie Cook

      I should say, 1.7+ doesn’t support Python 2.6. Thank you.

  • arl

    python: error while loading shared libraries: cannot open shared object file: No such file or directory

    So when you build and _know_ the installation path /opt/rh/python27/root, and there are lib directories, like

    So why do you _not_ have linked the binary so that it includes library path?


    [root@arlslab-base-dev-linux foxfire-1.0]# ld –help | grep -i path
    -L DIRECTORY, –library-path DIRECTORY
    Add DIRECTORY to library search path
    Just link symbols (if directory, same as –rpath)
    -Y PATH Default search path for Solaris compatibility
    -rpath PATH Set runtime shared library search path
    -rpath-link PATH Set link time shared library search path
    -z nodefaultlib Mark object not to use default search paths
    -z nodefaultlib Mark object not to use default search paths
    -z nodefaultlib Mark object not to use default search paths

    (So that the thing would work without “scl enable python27 bash” hack)


  • rodomonte

    It will be interesting if you explain also how to set pip to work for python2.7 now
    It continues to look in the /usr/lib/python2.6/site-packages dir when check if a package it’s already insatlled and doesn’t find packages also if I mirror the folder like /usr/lib/python2.7/site-packages.

    Uninstall and reinstall pip doesn’t works also after “sudo yum install python27”

  • Igor

    Is any way to install pygobject3 to RHEL6?