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 http://bit.ly/fedora-scl.

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:

su
yum install sudo
exit

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 http://people.redhat.com/bkabrda/scl_python27.repo (or follow the link from the Fedora SCL page) into the file
:wq

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

sudo sh -c 'wget -qO- http://people.redhat.com/bkabrda/scl_python27.repo >> /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
python27
[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
django-admin.py startproject mytest_django
(modify settings.py to point at your database, enable admin, etc)
python manage.py runserver {insert your IP here}:8000

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


For more information about Red Hat Software Collections or Red Hat Developer Toolset, visit developer.redhat.com/RHEL.


This entry was posted in Software Collections and tagged , , , by Langdon White. Bookmark the permalink.

About Langdon White

Langdon brings nearly 15 years experience in software development and systems architecture to his role as Red Hat's Developer and Platform Evangelist for Red Hat Enterprise Linux. In his role as RHEL Evangelist, Langdon fights tirelessly for the users to bridge development agility with production stability. Prior to Red Hat, he held roles as a Chief Operating Officer at start up vrevo and as Principal of his own consulting firm, FishJump. He lives in Boston, Massachusetts, US with his wife, three children and his own personal basement cloud. Follow me on twitter @1angdon

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

  1. 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!

  2. Pingback: instalar python 2.7 centos mediante yum

  3. Pingback: DBA_MYSQL_LINUX_Perl » CentOS 6下快速安装Python27

  4. Pingback: Our Top 12 Blog Articles of 2013 | Red Hat Developer Blog

  5. 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?

  6. 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.

      • 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.

      • 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: https://access.redhat.com/site/documentation/en-US/Red_Hat_Enterprise_Linux/5/html/Deployment_Guide/ch-Migrating_from_MySQL_5.0_to_MySQL_5.5.html#ex-in-place_upgrade

        Webtatic approach that worked for MySQL upgrade:

        http://www.webtatic.com/packages/mysql55/

        Finally, my StackOverflow question:

        http://stackoverflow.com/questions/20980695/error-mysql55-conflicts-with-mysql-5-1-71-1-el6-x86-64-in-installing-djangos-m

        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.

      • 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.

  7. Pingback: Red Hat Developer Newsletter – December 2013 | Red Hat Developer Blog

  8. 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.

  9. 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.

  10. 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: mirror.sanctuaryhost.com
     * extras: mirror.ancl.hawaii.edu
     * updates: yum.singlehop.com
    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: mirror.sanctuaryhost.com
     * extras: mirror.ancl.hawaii.edu
     * updates: yum.singlehop.com
    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.

  11. Pingback: mysql connector installed in python 2.6, but not on 2.7 | StackAnswer.com

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

  13. Hi Langdon, great post. One questions though:

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

  14. 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
    /opt/rh/python27/root/usr/lib64
    $> sudo LD_LIBRARY_PATH=$LD_LIBRARY_PATH easy_install pip
    sudo: easy_install: command not found
    $> which python
    /opt/rh/python27/root/usr/bin/python
    $> sudo LD_LIBRARY_PATH=$LD_LIBRARY_PATH which python
    /usr/bin/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)?

  15. Pingback: Automated deployment of RPM-packaged django applications with Red Hat Software Collections | Red Hat Developer Blog

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s