James Friend - osx https://jamesfriend.com.au/tags/osx en Installing Pygame on Mac OS 10.8 Mountain Lion https://jamesfriend.com.au/installing-pygame-python-mac-os-108-mountain-lion <div class="field field-name-body field-type-text-with-summary field-label-hidden"><div class="field-items"><div class="field-item even" property="content:encoded"><p>I decided to install and play around with Pygame today, mainly as an excuse to write some Python for a minor departure from all the Javascript/Coffeescript I've been writing lately. Unfortunately the process wasn't entirely frictionless, due to Pygame not yet accounting for Apple's move to XQuartz as the recommended X11 implementation for Mac OS as of 10.8 Mountain Lion. As a result I ran into some compilation errors while Pygame was building it's native extensions, which fortunately were not too hard to fix as I had some familiarity with changes to X11 on Mountain Lion.</p> <p><strong>TL;DR</strong> <a href="https://jamesfriend.com.au/installing-pygame-python-mac-os-108-mountain-lion#node_6_tldr">you need to set some environment variables before installing Pygame</a></p> <h3>What didn't work</h3> <p>Running <code>pip install pygame</code> would fail as follows:</p> <pre><code class="language-nohighlight">building 'pygame.display' extension cc -fno-strict-aliasing -fno-common -dynamic -I/usr/local/include -DNDEBUG -g -O3 -Wall -Wstrict-prototypes -Ddarwin -I/Library/Frameworks/SDL.framework/Versions/Current/Headers -I/usr/local/Cellar/python/2.7.3/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c src/display.c -o build/temp.macosx-10.8-x86_64-2.7/src/display.o In file included from src/display.c:30: /Library/Frameworks/SDL.framework/Versions/Current/Headers/SDL_syswm.h:58:10: fatal error: 'X11/Xlib.h' file not found #include &lt;X11/Xlib.h&gt; ^ 1 error generated. error: command 'cc' failed with exit status 1 </code></pre> <p>While building the native extention 'pygame.display', Xlib.h (a particular X11 header, required by SDL) is not found, as XQuartz's <code>/opt/X11/include</code> directory has not been specified to search for headers.</p> <p>By setting some environment variables, we can configure the Pygame build process to build correctly. I found these in a few other blog posts, but they specified only <code>-arch i386</code> for CFLAGS, LDFLAGS and ARCHFLAGS, which allowed the build process to complete, but <code>import pygame</code> fails (when running 64bit Python):</p> <pre><code>&gt;&gt;&gt; import pygame Traceback (most recent call last): File "&lt;stdin&gt;", line 1, in &lt;module&gt; File "/usr/local/lib/python2.7/site-packages/pygame/__init__.py", line 95, in &lt;module&gt; from pygame.base import * ImportError: dlopen(/usr/local/lib/python2.7/site-packages/pygame/base.so, 2): no suitable image found. Did find: /usr/local/lib/python2.7/site-packages/pygame/base.so: mach-o, but wrong architecture </code></pre> <h3><a id="node_6_tldr"></a>What actually got Pygame working</h3> <p>The environment variables (gcc flags) required to allow Pygame to correctly build fat binaries (for both 32bit and 64bit Python) are:</p> <pre><code>export CC='/usr/bin/gcc' export CFLAGS='-isysroot /Applications/Xcode.app/Contents/Developer/Platforms/MacOSX.platform/Developer/SDKs/MacOSX10.8.sdk -I/opt/X11/include -arch i386 -arch x86_64' export LDFLAGS='-arch i386 -arch x86_64' export ARCHFLAGS='-arch i386 -arch x86_64' </code></pre> <p>Then <code>pip install pygame</code> and <code>python -c 'import pygame'</code>. If no error is returned by Python, then you should now have a working installation of Pygame.</p> <h3>Additional details</h3> <p>I'm using homebrew-installed Python/pip, and I've set my $PATH with <code>/usr/local/bin</code> before the system-installed Python.</p> <p>If you haven't installed Python/pip via homebrew (you're using the system-installed Python), you would likely need to run <code>sudo pip install pygame</code>.</p> <p>If you're using another install method, such as easy_install, this fix should still work.</p> <p>Before running <code>pip install pygame</code>, I had also installed <a href="https://developer.apple.com/downloads/index.action">Command Line Tools for XCode</a> (<a href="http://docwiki.embarcadero.com/RADStudio/XE4/en/Installing_the_Xcode_Command_Line_Tools_on_a_Mac">walkthrough</a>), as well as <a href="http://xquartz.macosforge.org/landing/">XQuartz</a>, and the following homebrew packages: <code>brew install sdl sdl_image sdl_mixer sdl_ttf smpeg portmidi</code>.</p> <p>If homebrew fails to install <code>smpeg</code> you might need to do the following:</p> <pre><code class="language-bash">brew tap homebrew/headonly brew install --HEAD smpeg </code></pre> </div></div></div><div class="field field-name-field-tags field-type-taxonomy-term-reference field-label-above"><div class="field-label">Tags:&nbsp;</div><div class="field-items"><div class="field-item even" rel="dc:subject"><a href="/tags/python" typeof="skos:Concept" property="rdfs:label skos:prefLabel" datatype="">python</a></div><div class="field-item odd" rel="dc:subject"><a href="/tags/homebrew" typeof="skos:Concept" property="rdfs:label skos:prefLabel" datatype="">homebrew</a></div><div class="field-item even" rel="dc:subject"><a href="/tags/pygame" typeof="skos:Concept" property="rdfs:label skos:prefLabel" datatype="">pygame</a></div><div class="field-item odd" rel="dc:subject"><a href="/tags/osx" typeof="skos:Concept" property="rdfs:label skos:prefLabel" datatype="">osx</a></div><div class="field-item even" rel="dc:subject"><a href="/tags/mac" typeof="skos:Concept" property="rdfs:label skos:prefLabel" datatype="">mac</a></div></div></div> Wed, 07 Aug 2013 08:34:15 +0000 James 6 at https://jamesfriend.com.au Setting up a new Mac for Node.js and Drupal development https://jamesfriend.com.au/content/setting-new-mac-nodejs-and-drupal-development <div class="field field-name-body field-type-text-with-summary field-label-hidden"><div class="field-items"><div class="field-item even" property="content:encoded"><p><strong>UPDATE:</strong> I am now of the opinion that you're probably better off installing homebrew with a prefix of somewhere inside your user directory, eg. <code>~/.homebrew/</code>. I mention this below as an alternative, but now this would be my primary recommendation. Some parts, however, such as configuring the Apple-included Apache installation, and the custom DNS resolvers, are system-wide changes, so you might not want to couple them to a particular user account. Perhaps consider <code>brew install</code>ing a user-local copy of Apache, but use a system level installation of dnsmasq.</p> <p><strong>Original article text follows:</strong></p> <p>So you've got a fresh, clean Mountain Lion install and you need to get up and running for local development. Recently I spent a day doing just that, so I thought I'd write it all down, to save me from having to look all this stuff up again.</p> <h4>Install Xcode Command Line Tools</h4> <p>Available from the <a href="https://developer.apple.com/downloads/index.action">Apple Developer downloads area</a>. You'll need an Apple ID.</p> <p>This package includes llvm-gcc, which is a (mostly) compatible replacement for gcc. It will work for most homebrew formulas which build with gcc.</p> <h4>Install homebrew</h4> <p><a href="http://mxcl.github.io/homebrew/">homebrew</a> can be conveniently installed with one line in the Terminal. As with all curl-to-sh style install scripts, you should read the script first to make sure it's not going to do anything bad. Don't say I didn't warn you :)</p> <pre><code>ruby -e "$(curl -fsSL https://raw.github.com/mxcl/homebrew/go)" </code></pre> <p>The most convenient approach to using homebrew is to make <code>/usr/local</code> writable to admin group, if you don’t mind a (sort of) security risk. Bear in mind, <code>/Applications</code> is writable by that group by default anyway, so unless you've locked that down also, this won't really make your system much more insecure than it already is.</p> <pre><code>sudo chmod -R g+w /usr/local </code></pre> <p>Unfortunately without doing this, homebrew doesn't work particularly well in its default install location. Alternatively install somewhere user-local like <code>~/homebrew</code> if you're really concerned about security and locking down user permissions on your local machine.</p> <h4>Install Node.js</h4> <p>Node in particular is easier to install from the nodejs.org installer package, not homebrew. Due to some past disagreement between the maintainers of npm and homebrew, the homebrew formula for node doesn’t properly install npm. The official installer, however, works pretty well.</p> <p>To get <code>npm install -g</code> working (without sudo) you may also want to change the global node_modules directory to have a group id of 'admin':</p> <pre><code>sudo chgrp -R admin /usr/local/lib/node_modules </code></pre> <p>You may also need to give g+w permissions to that directory tree, if you installed node after running the chmod command above. This comes with the same caveats as homebrew. If you're really security conscious, install node and npm somewhere in your user directory, rather than globally in <code>/usr</code>.</p> <h4>Install Redis &amp; MongoDB (optional)</h4> <pre><code>brew install redis redis-server brew install mongodb mongod </code></pre> <h4>Install MySQL</h4> <pre><code>brew install mysql </code></pre> <p>Then follow the instructions depending on whether you want MySQL to run as your user account or a different one, where data should be stored and whether you want to start the daemon manually or run at login.</p> <p>Eg. to set up databases to run as your user account, with data in <code>/usr/local/var/mysql</code> and running at login:</p> <pre><code>unset TMPDIR mysql_install_db --verbose --user=`whoami` --basedir="$(brew --prefix mysql)" --datadir=/usr/local/var/mysql --tmpdir=/tmp </code></pre> <p>To have launchd start mysql at login:</p> <pre><code>ln -sfv /usr/local/opt/mysql/*.plist ~/Library/LaunchAgents </code></pre> <p>Then to load mysql now:</p> <pre><code>launchctl load ~/Library/LaunchAgents/homebrew.mxcl.mysql.plist </code></pre> <p>Set up the root mysql account. Pick a better password than I have.</p> <pre><code>/usr/local/opt/mysql/bin/mysqladmin -u root password 123456 </code></pre> <h4>Install PHP 5.4 (optional)</h4> <p>PHP is not in the main homebrew packages, so you'll need to 'tap' some extra repos before you can install it.</p> <pre><code>brew tap homebrew/dupes brew tap josegonzalez/homebrew-php </code></pre> <p>Update LoadModule directive in Apache config file to point to new PHP version</p> <pre><code># /etc/apache2/httpd.conf # Apple default: # LoadModule php5_module libexec/apache2/libphp5.so # This should be equivalent to $(brew --prefix php54)/libexec/apache2/libphp5.so LoadModule php5_module /usr/local/opt/php54/libexec/apache2/libphp5.so </code></pre> <p>Add updated PHP version to the PATH in <code>.bash_profile</code></p> <pre><code># ~/.bash_profile export PATH="$(brew --prefix php54)/bin:$PATH" </code></pre> <h4>MySQL socket location &amp; PHP PDO extension</h4> <p>When first running a PHP application which connects to MySQL, you may run in to errors relating to the socket through which PHP's PDO extension is attempting to connect to MySQL. This is because PDO is looking for the MySQL socket at <code>/var/mysql/mysql.sock</code>, but it's probably at <code>/tmp/mysql.sock</code> instead.</p> <p>Some options you have include setting up a php.ini file:</p> <pre><code>sudo cp /etc/php.ini.default /etc/php.ini sudo vi /etc/php.ini </code></pre> <p>and setting <code>pdo_mysql.default_socket=/tmp/mysql.sock</code> therein. If you go for the php.ini approach it might also be worth setting <code>display_errors = On</code> while you're there.</p> <p>Or you could use a symbolic link to make PHP happy:</p> <pre><code>sudo mkdir /var/mysql sudo ln -s /tmp/mysql.sock /var/mysql/mysql.sock </code></pre> <p>I'm not really sure which solution is best, but both seem to do the job.</p> <h4>Set up .dev TLD for local development using dnsmasq</h4> <p>No one likes having to go and edit your hosts file, or something similarly tedious, every single time they create a new local project. By setting up a special TLD and dynamic vhosts, we can avoid ever having to waste time with this stuff again.</p> <pre><code>brew install dnsmasq mkdir -pv $(brew --prefix)/etc/ </code></pre> <p>Configure dnsmasq</p> <pre><code>echo 'address=/.dev/127.0.0.1' &gt; $(brew --prefix)/etc/dnsmasq.conf </code></pre> <p>Create LaunchDaemon so dnsmasq runs at startup</p> <pre><code>sudo cp -v $(brew --prefix dnsmasq)/homebrew.mxcl.dnsmasq.plist /Library/LaunchDaemons sudo launchctl load -w /Library/LaunchDaemons/homebrew.mxcl.dnsmasq.plist </code></pre> <p>Set up a custom resolver to delegate resolution for .dev domains to dnsmasq</p> <pre><code>sudo mkdir -v /etc/resolver sudo bash -c 'echo "nameserver 127.0.0.1" &gt; /etc/resolver/dev' </code></pre> <h4>Set up dynamically configured mass virtual hosting in Apache</h4> <p>Ideally, all you should need to do to spool up a new site for development is create a directory under <code>/srv/www</code>. This Apache config should let you do just that. Create /etc/apache2/other/mass-vhost.conf:</p> <pre><code>&lt;Directory /srv/www&gt; Order Deny,Allow Allow from all AllowOverride All &lt;/Directory&gt; NameVirtualHost *:80 &lt;VirtualHost *:80&gt; # get the server name from the Host: header UseCanonicalName Off # include the server name in the filenames used to satisfy requests VirtualDocumentRoot /srv/www/%1 &lt;/VirtualHost&gt; </code></pre> <h4>Create your sites directory</h4> <pre><code>sudo mkdir -p /srv/www sudo chown -R _www:_www /srv/www sudo chmod -R g+w /srv/www </code></pre> <p>Add your user account to the _www group so you can write to <code>/srv/www</code></p> <pre><code>sudo dseditgroup -o edit -a `whoami` -t user _www </code></pre> <h4>Start Apache (and configure it to start automatically at startup)</h4> <pre><code>sudo apachectl start </code></pre> <h4>Install Drush</h4> <pre><code>brew install drush </code></pre> <h4>Set up a test Drupal site</h4> <p>Create a database in MySQL</p> <pre><code>cd /srv/www drush dl drupal mv drupal-* dtest </code></pre> <p>Create a Drupal settings.php file and fill it out with your database credentials.</p> <p>In the site .htaccess file uncomment the directive:</p> <pre><code># RewriteBase / </code></pre> <p>Navigate to <a href="http://dtest.dev/install.php">http://dtest.dev/install.php</a> Once installed, you should also be able to enable Clean URLs.</p> <p>And you're done!</p> </div></div></div><div class="field field-name-field-tags field-type-taxonomy-term-reference field-label-above"><div class="field-label">Tags:&nbsp;</div><div class="field-items"><div class="field-item even" rel="dc:subject"><a href="/tags/mac" typeof="skos:Concept" property="rdfs:label skos:prefLabel" datatype="">mac</a></div><div class="field-item odd" rel="dc:subject"><a href="/tags/osx" typeof="skos:Concept" property="rdfs:label skos:prefLabel" datatype="">osx</a></div><div class="field-item even" rel="dc:subject"><a href="/tags/nodejs" typeof="skos:Concept" property="rdfs:label skos:prefLabel" datatype="">nodejs</a></div><div class="field-item odd" rel="dc:subject"><a href="/tags/drupal" typeof="skos:Concept" property="rdfs:label skos:prefLabel" datatype="">drupal</a></div><div class="field-item even" rel="dc:subject"><a href="/tags/dnsmasq" typeof="skos:Concept" property="rdfs:label skos:prefLabel" datatype="">dnsmasq</a></div><div class="field-item odd" rel="dc:subject"><a href="/tags/php" typeof="skos:Concept" property="rdfs:label skos:prefLabel" datatype="">php</a></div><div class="field-item even" rel="dc:subject"><a href="/tags/apache" typeof="skos:Concept" property="rdfs:label skos:prefLabel" datatype="">apache</a></div></div></div> Tue, 26 Mar 2013 14:00:01 +0000 James 3 at https://jamesfriend.com.au