More MacPorts tricks
Sat 10 Mar 07 15:00 | Tags: Command Line, Guides, Mac, MacPorts
It's been a while since I wrote anything about MacPorts, so let's revisit it, shall we? Those of you who are entirely unfamiliar with MacPorts may wish to skip this article; it won't be of much use to you.
As I mentioned earlier, I was recently toying with the idea of switching to WordPress for this blog. In order to test WordPress out, I installed it on my own computer before installing it "live" on the public web server.
WordPress requires MySQL. I installed PHP without MySQL on my computer because I personally don't use MySQL; I prefer to use SQLite whenever I write programs that need to use a database. So I needed to add MySQL support to my PHP installation before I could use WordPress. Fortunately, I'm using MacPorts, so that's going to be pretty easy to do. There's a few different ways to go about it, though.
As I said in that earlier article, I could have used a "variant" trigger when I first installed PHP to add MySQL support. A list of possible variants for a port can be found by typing port variants portname:
Monster:~ Albright$ port variants php5
php5 has the variants:
universal
darwin_6
darwin_7
darwin_8
freebsd
macosx
apache
apache2
fastcgi
snmp
macports_snmp
mysql3
mysql4
mysql5
postgresql
ipc
pcntl
pear
Monster:~ Albright$
And you can see what variants your currently-installed port is using (as well as the version of that program) by using port installed portname:
Monster:~ Albright$ port installed php5
The following ports are currently installed:
php5 @5.2.0_0+darwin_8+fastcgi+macosx (active)
Monster:~ Albright$
(Just typing port installed will list everything that MacPorts has installed on your system and their versions. It may be a pretty long list.)
So I want the mysql5 variant on my PHP 5 installation. How should I go about that? It depends on circumstances.
Adding variants through upgrades
If the version of the port you currently have installed is out of date, you can choose to change the variants of the installation when you upgrade it. To check if an installed package is outdated, use port outdated portname. In this example, my SQLite installation is out of date.
Monster:~ Albright$ port outdated sqlite3
The following installed ports are outdated:
sqlite3 3.3.11_0 < 3.3.13_0
Monster:~ Albright$
But if you just get a blank line after the first line, then the package is up-to-date. In this example, my MySQL installation is current.
Monster:~ Albright$ port outdated mysql5
The following installed ports are outdated:
Monster:~ Albright$
In the above example, I had PHP5 version 5.2.0_0 installed. But it looks like it's out of date!
Monster:~ Albright$ port outdated php5
The following installed ports are outdated:
php5 5.2.0_0 < 5.2.1_1
Monster:~ Albright$
So I can upgrade it using the upgrade command, and specify my desired variants. MacPorts will respect those variants as it installs the new version of the package. (Remember that we need to prepend commands with the sudo command and type our password whenever we run a command that will install or remove files.)
Monster:~ Albright$ sudo port upgrade php5 +fastcgi +mysql5
Password:
---> Fetching php5
[...]
---> Activating php5 5.2.1_1+darwin_8+fastcgi+macosx+mysql5
Monster:~ Albright$ port installed php5
The following ports are currently installed:
php5 @5.2.1_1+darwin_8+fastcgi+macosx+mysql5 (active)
(You can get a list of all out-of-date installations by using just port outdated (with no port name), and upgrade all outdated ports at once using port upgrade installed. Be sure not to use port upgrade all, as that will try to install every single port available on your system! I found out that little fubar by personal experience, needless to say…)
Brute force reinstallation (avoid it!)
But what if your installed version of the package was already up-to-date? MacPorts won't let you upgrade an up-to-date installation, even if you try to specify different variants; it will fail silently.
Monster:~ Albright$ port installed php5
The following ports are currently installed:
php5 @5.2.1_1+darwin_8+fastcgi+macosx (active)
Monster:~ Albright$ sudo port upgrade php5 +fastcgi +mysql5
Monster:~ Albright$ port installed php5
The following ports are currently installed:
php5 @5.2.1_1+darwin_8+fastcgi+macosx (active)
How can we force MacPorts to rebuild the package with the new variants? Well, we can make like Luke and use the force… flag. Adding -f to a command forces MacPorts to follow a command, even if it doesn't think it's a good idea to do so. I generally recommend you never use this flag with MacPorts; it can make for some unpleasantness. However, using it with upgrade won't foul things up too much. It's a bit slow, because it seems to also fetch and reinstall all of the dependencies for the package you're trying to reinstall, but it works eventually.
Monster:~ Albright$ sudo port upgrade -f php5 +mysql5 +fastcgi
Note that you'll probably see some dire-looking warnings stating "Warning: Uninstall forced. Proceeding despite dependencies." Don't worry; all of the dependencies that it's uninstalling are being immediately re-installed anyway. Like I said, it's not pretty, but it works.
Cohabitation
But here's the coolest and most pleasant way to get around this problem. MacPorts is smart enough that it can handle multiple installations of the same package at the same time, so long as each installation has a different version number or different variants. Watch this:
Monster:~ Albright$ port installed php5
The following ports are currently installed:
php5 @5.2.1_1+darwin_8+fastcgi+macosx (active)
Monster:~ Albright$ sudo port install php5 +fastcgi +mysql5
Password:
---> Fetching php5
[...]
---> Activating php5 5.2.1_1+darwin_8+fastcgi+macosx+mysql5
Error: Target com.apple.activate returned: Image error: Another version of php5 (5.2.1_1+darwin_8+fastcgi+macosx) is already active.
Error: Status 1 encountered during processing.
Uh oh, but it caused an error! No problem, really.
Monster:~ Albright$ port installed php5
The following ports are currently installed:
php5 @5.2.1_1+darwin_8+fastcgi+macosx (active)
php5 @5.2.1_1+darwin_8+fastcgi+macosx+mysql5
See? I've got two installations of PHP now. However, the one without MySQL support is not "active;" it's not the PHP installation that the computer is going to use. MacPorts gives us two commands to change this; deactivate and activate. deactivate essentially "turns off" an installation without uninstalling it.
Monster:~ Albright$ sqlite3 -version
3.3.11
Monster:~ Albright$ sudo port deactivate sqlite3
---> Deactivating sqlite3
Monster:~ Albright$ sqlite3 -version
-bash: /opt/local/bin/sqlite3: No such file or directory
Monster:~ Albright$ sudo port activate sqlite3
---> Activating sqlite3
Monster:~ Albright$ sqlite3 -version
3.3.11
Monster:~ Albright$
As you can see, the usage is pretty simple: activate portname and deactivate portname. However, if you try to use it like that on a package you have multiple installations of, MacPorts rightly becomes confused. So to use these commands (as well as others, such as uninstall) on packages with multiple versions, you have to specify the version you wish to act on exactly as port installed labels them. You may find it helpful to do as I do and copy-and-paste the version numbers. So here, I'll disable the PHP installation without MySQL, and enable the one with MySQL:
Monster:~ Albright$ port installed php5
The following ports are currently installed:
php5 @5.2.1_1+darwin_8+fastcgi+macosx (active)
php5 @5.2.1_1+darwin_8+fastcgi+macosx+mysql5
Monster:~ Albright$ sudo port deactivate php5 @5.2.1_1+darwin_8+fastcgi+macosx
---> Deactivating php5 5.2.1_1+darwin_8+fastcgi+macosx
Monster:~ Albright$ sudo port activate php5 @5.2.1_1+darwin_8+fastcgi+macosx+mysql5
---> Activating php5 5.2.1_1+darwin_8+fastcgi+macosx+mysql5
Monster:~ Albright$ port installed php5
The following ports are currently installed:
php5 @5.2.1_1+darwin_8+fastcgi+macosx
php5 @5.2.1_1+darwin_8+fastcgi+macosx+mysql5 (active)
Monster:~ Albright$
Now, if I wish, I can uninstall the non-MySQL installation without any whining about dependencies or anything like that, because I already have another current and working installation on my machine.
Monster:~ Albright$ sudo port uninstall php5 @5.2.1_1+darwin_8+fastcgi+macosx
---> Uninstalling php5 5.2.1_1+darwin_8+fastcgi+macosx
Monster:~ Albright$ port installed php5
The following ports are currently installed:
php5 @5.2.1_1+darwin_8+fastcgi+macosx+mysql5 (active)
Monster:~ Albright$
This is a much prettier solution than the brute force method.
This "cohabitation" of installations can also work with two different versions of the same package. So, for example, if you're using version X of a vitally important application, and version Y has come out, you can install and activate version Y and test it out without uninstalling or installing over version X; just remember to use install portname instead of upgrade portname. If it turns out version Y is buggy or faulty, you can the very easily deactivate it and reactivate version X without causing too much downtime.
MacPorts is a powerful program for managing the more Unix-y software on your computer. It can be a bit tough to use at first, but as the above shows, its learning curve allows for some pretty convenient functionality -- and it's definitely still easier to use than compiling and installing applications and dependencies "by hand." If you have any questions about MacPorts, please post them in the comments or email me, and I'll try to help you out.
Get more great Ray Gun Robot content sent directly to your feed reader or email inbox! Subscribe today!
Articles & Links — Via Email
Articles Only — Via Email
2 Comments | 0 Trackbacks |
| ![]()
Trackbacks
Comments
#1 | screenmates | 2 Jan 08 09:28
Hi,
Great article!
I used MacPorts 1.6 on Lepoard and installed apache2, php5 and mysql as under:
sudo port install apache2 (this runs into errors at ncurses*, etc. with the error "No awk found!" and so I had to run it again)
sudo port install apache2 (this resumes at ncurses* and goes well) sudo launchctl load -w /Library/LaunchDaemons/org.macports.apache2.plist
sudo port install mysql5 +server sudo launchctl load -w /Library/LaunchDaemons/org.macports.mysql5.plist sudo -u mysql mysql_install_db5 (NEW INSTALL)
sudo port install php5 +macosx +apache2 +php5-memcache +memcached +pear +phpmyadmin +gd2 +ipc
sudo cp /opt/local/apache2/conf/httpd.conf.sample /opt/local/apache2/conf/httpd.conf
cd /opt/local/apache2/modules sudo /opt/local/apache2/bin/apxs -a -e -n "php5" libphp5.so
sudo cp /opt/local/etc/php.ini-dist /opt/local/etc/php.ini
And then, I modify the httpd.conf to AddType, DocumentRoot, etc.
Although the following command says "Mysql is alive!", it does not appear on phpinfo() output:
mysqladmin5 -u root -p ping
Another problem is that my application uses sqlite but after the above installation, my app bombs saying "SQLite extension can't be loaded!" although sqlite3 is active and does not show up in phpinfo() output either. I saw sqlite3 being installed automatically with apache2. Here is the output of installed ports and their statuses which are all active:
sudo port installed apache2 The following ports are currently installed: apache2 @2.2.6_0+darwin_9 (active)
sudo port installed php5 The following ports are currently installed: php5 @5.2.5_1+apache2+ipc+macosx+pear (active)
sudo port installed mysql5 The following ports are currently installed: mysql5 @5.0.51_0+server (active)
sudo port installed The following ports are currently installed: apache2 @2.2.6_0+darwin_9 (active) apr @1.2.12_0+darwin_9 (active) apr-util @1.2.12_0 (active) bzip2 @1.0.4_1 (active) curl @7.17.1_0 (active) db44 @4.4.20_1 (active) expat @2.0.1_0 (active) freetype @2.3.5_1 (active) gawk @3.1.5_2 (active) gettext @0.17_2 (active) jpeg @6b_2 (active) libevent @1.3e_0 (active) libiconv @1.12_0 (active) libmcrypt @2.5.8_0 (active) libpng @1.2.24_0 (active) libxml2 @2.6.30_0 (active) libxslt @1.1.22_0 (active) memcached @1.2.4_1 (active) mhash @0.9.9_0 (active) mysql5 @5.0.51_0+server (active) ncurses @5.6_0 (active) ncursesw @5.6_0+darwin_9 (active) openssl @0.9.8g_0 (active) pcre @7.4_0 (active) php5 @5.2.5_1+apache2+ipc+macosx+pear (active) php5-memcache @2.1.2_0 (active) pkgconfig @0.22_0 (active) readline @5.2.007_0+darwin_9 (active) sqlite3 @3.5.4_0 (active) tiff @3.8.2_1+macosx (active) zlib @1.2.3_1 (active)
Upgrading a port with additional variants does not work. For instance, sudo port upgrade php5 +tidy does not work!
Can you please help?
TIA


#2 | Garrett Albright | 2 Jan 08 09:59
With so many errors like that, I can't help but wonder if the error is beyond MacPorts. Do you have disk errors in other applications too?